"Offering free computer help, hints, and tips to the Internet populace." Now with over 3,820 tips!

Windows XP and DOS

Place User Input into an Environment Variable

SUMMARY: Allow a batch file in Windows XP to accept user input and place the results in an environment variable.


 

There may be instances where a batch file in Windows XP needs to accept user input. Examples include asking for the name of a file to copy, text file to read, computer's IP address to ping, or text to search.

To accept user input and place the results in an environment variable you can use the set command. The following is the syntax:

set /p VARIABLE=PROMPT

VARIABLE is the name of an environment variable to set

PROMPT is a displayed prompt to the user. Note that you should place a colon, greater than sign, or other input marker after the prompt to alert the user that input is requested.

For example:

set /p FILENAME=Enter filename:

The following batch file will prompt the user for a filename to copy to USB media assumed to be mounted as the E: drive. Then the file will be copied. Note that this batch file only performs basic error checking:

1) A filename must be entered.

2) The entered filename must exist.

A blank line has been inserted after each line of this batch file as some lines wrap onscreen here.




@echo off

set /p FILENAME=Enter the filename to copy to USB media on the E: drive:

if not defined FILENAME goto error_no_file

if not exist %FILENAME% goto error_not_exist

copy %FILENAME% e:\%FILENAME%

pause

exit

:error_no_file

echo You did not enter a filename. This program will now exit.

pause

exit

:error_not_exist

echo The filename you entered %FILENAME% does not exist. This program will now exit.

pause

exit



Print This Tip

Get E-Mail When New Tips are Online

Return to the Windows XP and DOS page.

 


New in MalekTips:

RSS Feeds- Subscribe!

You want the latest tech tips and tricks in your e-mail Inbox - FREE? Type your e-mail address below and click 'Get Tips!'.
 


( sample / details )
( opt-out instructions )