问题
I'm trying to run a batch file in vDOS (DOS emulator), where I want the user to input two variables. Every time I run the file, it doesn't let me enter the variables.
SET /P in=Input:
SET /P out=Output:
What I expected to happen was that It'd let me enter the input for the variables. Instead, it executes both of the commands as they are (without letting me enter the input).
回答1:
Windows cmd and MS-DOS are very different things and one of the differences is the set
command. In MS-DOS the only form of set is set variable=value
. There are neither set /A
, set "variable=value"
nor set /P
.
set /P
is a feature of Windows NT's cmd.exe. In DOS you must use 3rd party software to get user input and store in a variable. Here are some solutions
SENVAR.COM
SENVAR INPUT Input string:
EDITVAR and CHOOSE
editvar -p "Input string: " INPUT
FC.COM
@echo off :: based on batch from PC Magazine June 27, 1995 page 248 :: this version puts temps in C:\DOS dir and shortens var names :: User input is returned in variable STR :input > C:\DOS\en#er.bat fc con nul /lb1 /n|date|find " 1: " > C:\DOS\enter.bat echo set str= >>C:\DOS\enter.bat echo :loop >>C:\DOS\enter.bat echo if not '%%str%%==' set str=%%str%% %%5 >>C:\DOS\enter.bat echo if '%%str%%==' set str=%%5 >>C:\DOS\enter.bat echo shift >>C:\DOS\enter.bat echo if not '%%5==' goto loop call en#er.bat del C:\DOS\enter.bat del C:\DOS\en#er.bat
ANSI.SYS
@ECHO OFF REM * Ask for USeR INPUT and store it in variable USRINPUT REM * Assumes ANSI.SYS is loaded REM * Written by Rob van der Woude SET USRINPUT= REM * Turn on ANSI key translation (translate Enter REM * key to F6+Enter sequence) and ask for input: ECHO ←[13;0;64;13pEnter one word only . . . REM * Copy entered text to temporary file: COPY CON %TEMP%.\~USRINP.TMP REM * Turn off ANSI key translation and clear irrelevant screen output: ECHO ←[13;13p←[3A←[K←[1B←[K←[1B←[K←[2A REM * Add empty line to temporary file. The empty line REM * will be used to stop DATE asking for new date. ECHO.>> %TEMP%.\~USRINP.TMP ECHO.>> %TEMP%.\~USRINP.TMP REM * Create a temporary batch file that will store the REM * entered text into the environment variable USRINPUT: TYPE %TEMP%.\~USRINP.TMP | DATE | FIND "):" > %TEMP%.\~USRINP.BAT REM * Create more temporary batch files. Add REM * more command line parameters if necessary, REM * as in: ECHO SET USRINPUT=%%3 %%4 %%5 %%6 %%7 %%8 %%9>CURRENT.BAT ECHO SET USRINPUT=%%3>CURRENT.BAT REM * VOER.BAT and TYP.BAT are replacements for CURRENT.BAT for Dutch REM * DOS versions; add your own language versions if necessary: ECHO SET USRINPUT=%%6>VOER.BAT ECHO SET USRINPUT=%%4>TYP.BAT REM * This temporary batch file now sets the variable USRINPUT: CALL %TEMP%.\~USRINP.BAT REM * Display the result: ECHO You typed: ←[1m%USRINPUT%←[0m ECHO. PAUSE REM * Finally, clean up the mess of temporary files: FOR %%A IN (%TEMP%.\~USRINP.BAT %TEMP%.\~USRINP.TMP VOER.BAT TYP.BAT CURRENT.BAT) DO DEL %%A
The ← is the escape character (27h)
In case you just want to get simple answers like Y/N then CHOICE.COM
is designed for that purpose
See also
- MS-DOS 6.22 Batch File User Input to Environment Variable
- Prompt for variable in DOS 7.1
回答2:
/P
didn't get introduced until Windows 2000 or NT. Legacy MS-DOS or equivalent won't have it. https://www.computerhope.com/sethlp.htm
回答3:
Use editvar & choose
editvar -p "Input filename: " FILENAME
You can easily create variables & inputs
来源:https://stackoverflow.com/questions/58641122/cant-enter-a-variable-in-dos