Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am confused on how to create and use batch file variables. I would like to have the user input information (if possible) and assign that information to a variable. If this is not possible I would still like to know how to assign a variable so that I can use it in an IF statement. Any information would be appreciated

I hope this is what you mean, and it has been a while so may not be 100% correct
If you say want to copy a file from one location to another you can have the batch file as:
copy %1\%2 %3 (then save as test.bat)
then at the prompt type
test c:\ autoexec.bat c:\windows
This will insert the c:\ where %1 was and autoexec.bat where %2 was etc.

Sorry, I have noticed that I have put a \ in by mistake, it should say:
If you say want to copy a file from one location to another you can have the batch file as:
copy %1 %2 %3 (then save as test.bat)
then at the prompt type
test c:\ autoexec.bat c:\windows
This will insert the c:\ where %1 was and autoexec.bat where %2 was etc.

Hi.
1. To set a variable, use:
SET [VARIABLE]=[VALUE]
ie: SET USER=JOHN2. To use the variable's value:
%[VARIABLE]%
ie: if you do "ECHO Hi %USER%!" before setting the variable "user" to john, the result will be:
Hi John!Got it? As PeterK said, the %# (%1, %2 etc)variables are the variables that you enter when calling the batch:
ie: contents of a batch file:
::sample.bat
@echo input1=%1
@echo input2=%2then, you do: "sample abc xyz"
it will display
input1=abc
input2=xyz3. Ok? So, for using the IF statement, just use the variable value
ie:
IF "%USER%"=="JOHN" goto johnThe "" thing isn't necessary, but I recommend ya to use it. The statement IS CASE SENSITIVE (not the name of the variable, just the value)
4. And now, for getting user's input:
(this is kind of complicated, I'm not gonna explain how it works, ok?)
it will only work on an english dos.::input.bat
@echo off
echo Type input
fc con nul /lb1 /n |date |find "1:"> out.bat
echo set input=%%5> enter.bat
call out.bat
del out.bat
del enter.batthat will set the input from user to the variable INPUT.
Well, I hope I were as clear as possible
Any further questions, letme know-- Secret_Doom --
email: secret_doom@hotmail.com

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |