Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Batch question


shadow31's Avatar
Member
0 0

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


ynori7's Avatar
Future Emperor of Earth
0 0

you use the set keyword to assign a value to a variable name like this

set variable=value then you can refer to that variable as %variable%


shadow31's Avatar
Member
0 0

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)


ghost's Avatar
0 0

that article the minidist posted was good


ghost's Avatar
0 0

yes.

%username% won't work like that (at least I don't think so). It's a variable that you personally declared, unlike %systemroot%.

To get user input, do something like this:

set /p username=Enter Username:

see how that works? If you have anything else, hit me up on AIM or MSN (I love batch ^^ )


ynori7's Avatar
Future Emperor of Earth
0 0

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


ghost's Avatar
0 0

lol I got to it first xD


ghost's Avatar
0 0

cis_slayer wrote: that article the minidist posted was good

MinDistortion, close. Not really. lol :happy:


ghost's Avatar
0 0

maybe you need to make your name easier to comprehend. You shouldn't have to, it's not hard to read (or even just copy-paste), but no matter how idiot-proof you make something, there will always be someone to mess it up somehow. xD


ghost's Avatar
0 0

I know thats true. But really it isn't that hard to comprehend of a name.


ghost's Avatar
0 0

sigh I know, read my post in the Mentor thread…it all makes me sort of depressed.


shadow31's Avatar
Member
0 0

ok, i will try all that tomorrow


ynori7's Avatar
Future Emperor of Earth
0 0

try going back through some of the articles again. most of what i know about batch programming was expanded upon knowledge i gained from reading the articles here.


Uber0n's Avatar
Member
0 0

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**

ynori7's Avatar
Future Emperor of Earth
0 0

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?


ghost's Avatar
0 0

I don't think you can do it like that…

I think you have to declare the variable first. (set x=number)

then, you can increment it like this: %x% = %x% + 1

(maybe, I've never tried it..)


spyware's Avatar
Banned
0 0

Nah, the CMD refuses to actually calculate it. It's just a string.


ynori7's Avatar
Future Emperor of Earth
0 0

so you can't increment a variable in batch programming?


ynori7's Avatar
Future Emperor of Earth
0 0

thanks spyware. it seems tedious compared to what i'm used to, but at least it works.


spyware's Avatar
Banned
0 0

ynori7 wrote: thanks spyware. it seems tedious compared to what i'm used to, but at least it works.

Yah, pretty weird that Batch doesn't "know" math functions. Ah well, c'est Microsoft.


ghost's Avatar
0 0

echo off set /A a=5 set /A a = %a% + 2 echo %a%

edit:

set /? was a great help ;)


spyware's Avatar
Banned
0 0

:O

/Amazing!


ynori7's Avatar
Future Emperor of Earth
0 0

thanks swartmumba, that's what i was looking for.


shadow31's Avatar
Member
0 0

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 :)