batch-file - 在命令提示符中键入变量输入后,如何将变量输入保存到原始批处理文件中?
问题描述
在命令提示符下键入后,我想将 %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
解决方案
@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%
推荐阅读
- powershell - 从服务器获取奇怪的错误 - PowerShell
- flutter - Flutter Navigator.of(context).pop() in showDialog, close full app in ios
- python - 无法将符号张量 (sequential/lstm/strided_slice:0) 转换为 numpy 数组。在 MacBook Pro M1 上使用 TensorFlow 时出现问题
- python - Python 中的 switch-case 语句
- html - HTML 和 CSS 图像不在盒子内
- azure-devops - 使用打包生成和发布配置文件的 Azure DevOps 发布管道
- python - 如何在 Python 中将用户输入格式化为变量
- odoo - Odoo - 不按 _rec_name 过滤 Many2one
- go - Kubernetes Operator Wide Objects 和 Instance Wide Objects
- sparql - 在 SPARQL 中过滤!= NULL