我想在命令提示符下键入%x%的输入后将其保存到批处理文件中。请参见下面。
@ECHO OFF
TITLE Access to OST Documents for this project
ECHO Please enter the name of the OST Documents folder you would like to acces. (Spelling Sensitive)
set /p x= "P:\OST Documents\"
if not exist "P:\OST Documents\%x%" goto :try_again
if exist "P:\OST Documents\%x%" Goto :Continue
:Try_again
Echo Location not found, please try again.
set /p x= "P:\OST Documents\"
:Continue
TITLE Access to OST Documents for this project
:choice
set /P c=Are you sure you want to continue[Y/N]?
if /I "%c%" EQU "Y" goto :OST_Documents_File
if /I "%c%" EQU "N" goto :end
goto :choice
:OST_Documents_File
Start "" "P:\OST Documents\%x%"
End
:end
Fail
End
答案 0 :(得分:0)
@echo off
call :getvar
if not "%x%" == "" goto :continue
REM var not set; ask user:
REM insert your input code and verification here
REM write it to the end of this batch:
echo set "x=%x%">>"~f0"
:continue
REM rest of your code
REM the following line should be the very last line in this batch (but WITH a CRLF):
:getvar
call :getvar
尝试设置变量(第一次运行时变空)
if
行检查是否为空(然后询问用户)
echo set "x=%x%">>"~f0"
是这里的关键。它将set command
添加到批处理文件中(%~f0
是批处理文件的全名),因此
call :getvar
返回先前设置的变量%x%