Computing.Net > Forums > Disk Operating System > Batch File Variables

Batch File Variables

Reply to Message Icon

Original Message
Name: BatchHelp
Date: June 18, 2001 at 18:50:33 Pacific
Subject: Batch File Variables
Comment:

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


Report Offensive Message For Removal


Response Number 1
Name: PeterK
Date: June 19, 2001 at 02:05:26 Pacific
Subject: Batch File Variables
Reply: (edit)

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.


Report Offensive Follow Up For Removal

Response Number 2
Name: PeterK
Date: June 19, 2001 at 02:08:27 Pacific
Subject: Batch File Variables
Reply: (edit)

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.


Report Offensive Follow Up For Removal

Response Number 3
Name: Secret_Doom
Date: June 19, 2001 at 17:15:56 Pacific
Subject: Batch File Variables
Reply: (edit)

Hi.
1. To set a variable, use:
SET [VARIABLE]=[VALUE]
ie: SET USER=JOHN

2. 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=%2

then, you do: "sample abc xyz"
it will display
input1=abc
input2=xyz

3. Ok? So, for using the IF statement, just use the variable value

ie:
IF "%USER%"=="JOHN" goto john

The "" 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.bat

that 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


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch File Variables

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge