问题
I would like to save my input for %x% to the Batch file once I type it in the command prompt. Please see below.
@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
回答1:
@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
The call :getvar
tries to set the variable (comes back empty for the first run)
The if
line checks, if empty (then the user gets asked)echo set "x=%x%">>"~f0"
is the key here. It adds the set command
to the batch file (%~f0
is the batch file's full name), socall :getvar
comes back with the previously set variable %x%
来源:https://stackoverflow.com/questions/55694519/how-to-save-my-variable-input-to-the-original-batch-file-after-i-have-typed-it-i