How to save my variable input to the original batch file after I have typed it into the command prompt?

牧云@^-^@ 提交于 2020-11-25 03:33:28

问题


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), so
call :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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!