首页 > 解决方案 > 在命令提示符中键入变量输入后,如何将变量输入保存到原始批处理文件中?

问题描述

在命令提示符下键入后,我想将 %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

标签: batch-file

解决方案


@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%


推荐阅读