
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.
@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
Get E-Mail When New Tips are Online
Return to the Windows XP and DOS page.
New in MalekTips: