Batch question
Ok, I have been working a lot with batch lately but there is one thing that i can't get a grasp on. I don't understand variables like %1 or %2. If someone could either pm me or just post a good explanation of it that would be great. The articles either don't explain it enough or they skip it all together. I understand where you put them in the code but I don't know how they get a value to them. Do you have to put it in later on or do you have to put it in the cmd prompt after the batch has started? Please help me or point me to a good site that explains it. Thanks a lot
See if this helps you. http://www.maem.umr.edu/batch/variable.htm
ok, im starting to get it. But now I to ask something else…
I made a .bat file with "%username%" in it, instead of my username, so it would work on different computers. This worked when i tested it on my computer. So how does this work if I didn't set the variable?
Also, would there be a way to have the user set one of the variables after they have activated the batch file. So, after he started the program, it would ask him to put in a value for one of the variables in the command prompt. (I was thinking this could work for backing up things to jumpdrives)
shadow31 wrote: ok, im starting to get it. But now I to ask something else…
I made a .bat file with "%username%" in it, instead of my username, so it would work on different computers. This worked when i tested it on my computer. So how does this work if I didn't set the variable?
Also, would there be a way to have the user set one of the variables after they have activated the batch file. So, after he started the program, it would ask him to put in a value for one of the variables in the command prompt. (I was thinking this could work for backing up things to jumpdrives)
i'm guessing that username is predefined in windows, but i'm not sure.
edit: sorry for the repeated data, skunfoot replied as i was typing
as for input during the execution, use this format:
set /p variable=prompt for user
About the %0 and %1 variables: All command line arguments passed to a batch file are stored in this kind of variables.
%0 = name of batch file %1 = first argument %2 = second argument %3 = third argument etc etc
So, if you made this batch file for copying batch files (just as an easy example)
ECHO THIS FILE IS CALLED %0
COPY %1 %2
PAUSE```
If the file was called TEST.bat you could run this in cmd:
*TEST.bat C:\test\inputfile.txt C:\outputfile.txt*
This would first output "**THIS FILE IS CALLED TEST.bat**"
Then it would copy the file **C:\test\inputfile.txt** to **C:\outputfile.txt**
thanks uberon i just learned something. (sorry to steal someone's thread) i have a question of my own:
i was trying to increment a variable like this: (x is initially set equal to 1 so i was expecting 2) set x=%x%+1
but it just outputs '1+1' without the quotes. i must be doing something wrong. any ideas?
ynori7 wrote: so you can't increment a variable in batch programming?
Ok, I got everything working right with the variable but now I was wondering if you guys would check over some code I made.
xcopy "Z:\" "%destination%\documents\school" /M /E /Y /H /G
pause
xcopy "%destination%\documents\school" "Z:\" /M /E /Y /H /G
pause
exit```
Ok, if you haven't guessed, this is to back up my school files from the server to my jumpdrive. The reason for the variable is that the drive location (H: or F: i think) changes when it goes in a laptop and a desktop computer. The first part of the code is verily normal, but the second part is special. Since I do work at home, I want to be able to transfer stuff from jumpdrive to server as well. my hope is that after it transfers the material one way, it will send it all back like a normal back_up but the new things will go back with it. I just wanted to see what you guys thought of it. If it will work or not? or any good addition to it that you think would help.
p.s. the /h and /g copy system files, hidden files, and encrypted files as well :)