"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

Return to the Windows XP and DOS page.

Print this tip


Get the Newsletter

Thanks For Sharing!



Newest Tips:

Microsoft Word 2010
Make Sure Others Can See Your Fonts
Microsoft PowerPoint 2010
Animations or Video Displaying Poorly?
Google Chrome
[MODIFIED] Report a Malfunctioning Web Page, Crash, or Other Issue
Google Gmail
Show Unread Message Count as an Icon

Follow Us!

About MalekTips and the Author

The MalekTips website was created in 1998 by Andrew Malek of Envision Programming. The page's goal is to freely disperse computer-related tips, hints, and informative articles. Tips are organized to be easy to find, and are presented clearly, in easy-to-understand language. MalekTips also provides information and links to public-domain, open source, freeware, shareware, and commercial software available for download. < more >