批处理文件调用%〜1并获取变量的当前值/字符串 [英] batch files calling %~1 and getting the variable's current value/string
问题描述
下面的代码与批处理文件有关(命令提示符).我的问题是代码中说当前值为%〜1
的部分实际上没有显示%〜1
的值(我想说是string01或string02)不太确定如何执行此操作.我环顾四周,但无法解决这个简单的问题.
The code below is related to batch files (command prompt). My problem is the part of the code that says current value is %~1
doesn't actually display the value of %~1
(I would like it to say string01 or string02) am not too sure how to do this. I have looked around but cannot wrap my head around this simple problem.
@echo off
goto :MainFunction
:Func01
echo.
echo Running Func01
echo Variable %~1 current value is %~1
echo.
echo Set new value for Variable %~1:
set /p %~1=
goto :eof
:MainFunction
echo This is the main function!
set Var01=string01
set var02=string02
echo Var01 is equal to %Var01%
echo Var02 is equal to %Var02%
call :Func01 Var01
call :Func01 Var02
echo Var01 is now equal to %Var01%
echo Var02 is now equal to %Var02%
goto :eof
推荐答案
另一种方法是使用命令 CALL 进行命令行的双重解析,如下所示:
Another method is using command CALL to get a double parsing of a command line as shown below:
@echo off
goto :MainFunction
:Func01
echo/
echo Running Func01
call echo Variable %~1 current value is %%%~1%%
echo/
set /P "%~1=Set new value for Variable %~1: "
goto :EOF
:MainFunction
echo This is the main function!
set "Var01=string01"
set "var02=string02"
echo Var01 is equal to %Var01%
echo Var02 is equal to %Var02%
call :Func01 Var01
call :Func01 Var02
echo Var01 is now equal to %Var01%
echo Var02 is now equal to %Var02%
goto :EOF
命令行
call echo Variable %~1 current value is %%%~1%%
首先在Windows命令解释器运行 CALL 之前先修改
,例如在第一次执行子程序 Func01
到
is first modified before running CALL by Windows command interpreter for example on first execution of subroutine Func01
to
call echo Variable Var01 current value is %Var01%
在运行 CALL 时再次分析此命令行时,命令行更改为:
On parsing this command line a second time on running CALL the command line changes to:
echo Variable Var01 current value is string01
字符%
必须再转义一个%
,才能将其解释为批处理文件中的文字字符,这就是语法%奇怪的原因%%〜1 %%
.
The character %
must be escaped with one more %
to be interpreted as literal character in a batch file which is the reason for the strange looking syntax %%%~1%%
.
并查看DosTips论坛主题 ECHO.不能输入文本或空白行-而是使用ECHO/来解释使用 echo/
而不是 echo.
输出空行.
And take a look on DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ for the explanation on using echo/
instead of echo.
to output an empty line.
这篇关于批处理文件调用%〜1并获取变量的当前值/字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!