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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch File Variables

Reply to Message Icon

Name: BatchHelp
Date: June 18, 2001 at 18:50:33 Pacific
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



Sponsored Link
Ads by Google

Response Number 1
Name: PeterK
Date: June 19, 2001 at 02:05:26 Pacific
Reply:

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.


0

Response Number 2
Name: PeterK
Date: June 19, 2001 at 02:08:27 Pacific
Reply:

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.


0

Response Number 3
Name: Secret_Doom
Date: June 19, 2001 at 17:15:56 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: Batch File Variables

Batch File Variables Troubles www.computing.net/answers/dos/batch-file-variables-troubles/14028.html

Batch File Variables www.computing.net/answers/dos/batch-file-variables/14203.html

Batch file variables www.computing.net/answers/dos/batch-file-variables/14215.html