How to make a variable minus from a number. Batch File - variables

Basically I want to use 2020 minus a variable to make another variable.
This is the code I have at the moment.
echo Type Your Year, then press enter.
set/p "myear=>"
echo Type the year of the person's stuff you want to copy, then press enter.
set/p "yearofv=>"
cls
set/p "myyear="2020-%myear%"
set/p year="2020-%yearofv%"
md "c:\%myyear%\%myuser%\My Documents\File Copier\%user%'s Data\Documents"
copy "c:\%year%\%user%\My Documents" "c:\%myyear%\%myuser%\My Documents\File Copier\%user%'s Data\Document
can anybody run make any suggestions as how to help.
Thanks
Sam Inc.

A quick search yielded the following relevant answer. Use "set /a".
Calculating the sum of two variables in a batch script

It would be easier to identify your question if your provided code example was legible.
You can make it legible by highlighting it and clicking on the brackets.
I'm sure I'll be able to provide what you need if I could see what you're working with.
Thanks for taking the time to make your example more legible.
I believe your confusion is located where I have placed a "rem". That may be where you want to perform your calculation (or some other variation similar).
I placed an example for performing the calculation, then placed a goto statement so that you can look at the results.
This might help you to get a grip of resolving your issue:
#echo off
echo Type Your Year, then press enter.
rem set/p "myear=>"
set /p myyear="Enter myyear: " %=%
echo Type the year of the person's stuff you want to copy, then press enter.
rem set/p "yearofv=>"
set /p yearofv="Year of v: " %=%
cls
echo This is the difference between 2020 and "myyear..."
set/a difference=2020-%myyear%
echo %difference%
goto skip
set/p year="2020-%yearofv%"
md "c:\%myyear%\%myuser%\My Documents\File Copier\%user%'s Data\Documents"
copy "c:\%year%\%user%\My Documents" "c:\%myyear%\%myuser%\My Documents\File Copier\%user%'s Data\Document
:skip
By the way, I placed an "#echo off" at the top to make it easier to read the flow when executing the script.
Also, you might consider removing the "cls" that you have until you have it debugged. While debugging you need to be able to view the history of what has happened.

Related

storing current path in variable and reusing it

I know similar questions have already been asked, but somehow I am unable to figure out the mistake in my code.
I'm making a .bat file with the following code
echo off
echo %cd%
set curr_directory = "%cd%"
echo $curr_directory
pause
OUTPUT is :
C:\Users\MyDesktop>echo off
C:\Users\MyDesktop>
$curr_directory
Press any key to continue . . .
So what I dont get is why the value of variable curr_directory is not being printed.
What i eventually want to do is use the variable to change the directory something like this: cd $curr_directory
Thanks
I don't know where to start. EVERYTHING about your code is wrong. This should work:
#echo off
echo %cd%
set curr_directory=%cd%
echo %curr_directory%
pause
In batch you access variables via %var% and not $var. Further, DO NOT PUT SPACES behind =. SET x=123 will store 123 in x but SET x= 123 will store _123 (_ means space) in x.
EDIT: As SomethingDark stated, the first line should be #echo off except you actually want the message echo off to be printed. And yes, SET x = 123 means %x % is 123
use %curr_directory% instead of $curr_directory
Avoid spaces inbetween like the below one
"set curr_directory = %cd%"
below should work
echo off
echo %cd%
set curr_directory=%cd%
echo curr_directory is %curr_directory%
pause

How can I save variables longterm in Batch?

I've seen it done before on someone else's Batch program, so I'm sure it's possible. Every time I launch my program I run a series of lines of code such as:
set/a num=0
set/a tog=1
set/a ran=%random% %%10 +1
However, I would like for certain variables to stay around, even after the program is closed. I need scripts that can:
Write in a separate document the values of certain variables
Check that document for values at any point in any usage session
Thanks in advance!
You need a save system like so:
(
echo %VARIABLE1%
echo %VARIABLE2%
echo %VARIABLE3%
echo %VARIABLE4%
echo %VARIABLE5%
) > LOGS.prgmsav
The file extension can be .txt or something weird like .nerfguns, to load LOGS.prgmsav:
< LOGS.prgmsav (
set /p VARIABLE1=
set /p VARIABLE2=
set /p VARIABLE3=
set /p VARIABLE4=
set /p VARIABLE5=
)
The VARIABLE is any variable, you can save as many as you want! I hope I helped!

checking the value of a variables variable in Windows batch

Well, I'm trying to make a basic RPG in batch (Don't judge me) and I'm in a bit of a predicament with equiping items. It's a bit hard to explain so here's some code:
set inv1=sword
...
set inv9=empty
echo which item do you want to equip?
echo.
echo 1) %inv1%
...
echo 9) %inv9%
echo.
set /p c="> "
set invt=inv%c%
set type=%invt%_type
if "%%type%%"=="equipable" (
set itemslot=%invt%
basically, I need to replace %%type%% with the value of the value of 'type'. Is there any way to do this? if so, it would save me many lines of code and make it FAR easier to implement new items. THANKYOU to anybody who can help. (and, yes, I have searched the internet, this forum and other forums but no help :( )
Is this going to work in your case?
call set "t=%%type%%"
if /i "%t%"=="equipable" (
set itemslot=%invt%
edit This is the same as the comment by halfbit

Using echo and read $variable not working in UNIX! (UNIX beginner!)

So I just started learning UNIX yesterday, and I'm trying to create a basic script that asks for your contact details (name, address, phone number), and then stores that into a file called details.out.
This is driving me NUTS! Its such an easy/basic thing, yet I cant do it, and I've been stuck on it for a solid hour now...
after much googling and searching, I still can't find the answer. So this is what I've done so far, and was wondering where I am going wrong!
echo Please type your first and last name
read $firstname $lastname
echo Please type in your address
read $address
echo Please type in your phone number
read $phone
echo Thank you very much!
echo The details have been stored in '"details.out"'
cat >> details.out <<EOF
Name: echo $firstname echo $lastname
Address: echo $address
Phone Number: echo $phone
EOF
When I read "details.out" it it displays as follows:
Name: echo
Address: echo
Phone Number: echo
ANY help would be appreciated! (and if you get try and point me in the right directions as opposed to straight up giving me the answer, I would appreciate that!)
P.S I'm using Putty if that helps!
when you use read (or declaring variables), don't put $ sigil on the variable names
when you display a variable, always put double quotes around : ex. echo "$var"
when you use here-doc, no need to put echo command
when you use echo, use quotes :
"Double quote" every expansion, and anything that could contain a special character, eg. "$var", "$#", "${array[#]}", "$(command)". Use 'single quotes' to make something literal, eg. 'Costs $5 USD'. See http://mywiki.wooledge.org/Quotes http://mywiki.wooledge.org/Arguments and http://wiki.bash-hackers.org/syntax/words
Whenever you put a $ before a variable name, you're retrieving the current value of that variable. You don't want to do that in your read command. The variables are empty when the script starts, the empty values are put in place of the $firstname and $lastname and read is called with no arguments, causing it to read a line and discard it.
Setting a variable with assignment:
var=value
Setinng a variable with read:
read var
Neither of them use $var because they don't want to look at the current value, they want to replace it.
There's no need for those echos in the heredoc either. They aren't in command position, so they'll just get copied as part of the input to cat.

Something wrong with some batch file stuff, doesn't do what it should do, cant spot syntax errors

So, I am writing a computer speeder in batch, and I am baffled by this problem:
#echo off
cls
color 02
:licence
title Computer Speeder 2012 (Free licence)
cls
echo This is the free version of Computer Speeder 2012.
echo.
echo Do you wish to license this copy? [Y]es or [N]o.
choice /c NY /n
if ERRORLEVEL 2 goto license
if ERRORLEVEL 1 goto startup1
:license
dir | find "validatecode.txt" > if exist "validatecode.txt"
goto bam
) else (
goto else
:bam
cls
echo Type in your lisencing code to validate this
echo copy to upgrade to the full version:
echo.
set /p vd=""
:else
cls
echo You do not have a validation file on your computer,
echo if you purchased the full version you probably
echo deleted the file. Check the recycle bin, check you
echo put it in the correct path or re-purchase the
echo software because it has an individual code.
pause
goto startup1
For some odd reason, it executes all of this code chronologically:
First it asks if you want to license the copy of the software. If you press Y it takes you to license. It then performs the line dir | find "validatecode.txt" >if exist "validatecode.txt"
so on and so forth. However, it goes to bam and THEN goes to else. There's no other flaw in the code, I cant find any syntax error, can you guys explain what I am doing wrong? Im a noob kinda :/
If you want the program to stop after executing :bam, you should put a
goto:EOF after set /p vd=".
:bam
cls
echo Type in your lisencing code to validate this
...
set /p vd=""
goto:EOF
:else
cls
...
Try replacing this:
:lisence
dir | find "validatecode.txt" > if exist "validatecode.txt"
goto bam
) else (
goto else
with this:
:lisence
if exist "validatecode.txt" goto bam
goto else
Explanation:
You would need to test an errorlevel of the Find process for your code to possibly work, and the redirector '>' only creates a file called 'if', containing, oddly, the words exist and vaildatecode.txt.
Your DIR command will only look in the execution directory, but your 'bam' subroutine implies you intend to search the hard drive? True? If so, my 'if exist' command should be changed to:
dir /s/b validatecode.txt && goto bam
You appear to not be using information inside the file, so if you need to search the entire hard drive this command will perform it, and will logically go to the correct subroutine.
The snippet provided tests for a file called validatecode.txt in the local directory, and executes a goto to the label bam if it is found. If the file does not exist, focus drops to the next line, which performs a goto to the label :else.
You will need to finish your script differently as well. As it stands, if the validatecode.txt is found, you will be executing both 'bam' and 'else'. You should place a final label like :exit (:eof is reserved) and make a goto :exit command as the last line of each subroutine.
Finally, your call to the subroutine Startup1 will always fail because you have no label called Startup1.
From a flow standpoint, it looks like you have no way to exit if the user does not want to license the copy they are using.