我想创建自己的“stringLength”脚本并且我设法做到了,但我不明白我从 https://ss64.com/nt/syntax-substring.html 借来的一行代码(查看页面底部附近)
我的脚本是:
@echo off
::initializing variables
SET strlen=0
SET "string=stringToBeMeasured"
:: adding } at the end of the string to signal the end of the count
set "string=%string%}"
:main
call SET chosenchar=%%string:~%strlen%,1%%
:: if the character is not } then add string lenght +1 until...
if "%chosenchar%" neq "}" (
set /a strlen=%strlen%+1
goto :main
)
:: when you see the designated } character, then print the result and exit
if "%chosenchar%" == "}" ( goto :exit )
:exit
echo Your string length is: %strlen%
pause
我不明白到底是如何工作的:
call SET chosenchar=%%string:~%strlen%,1%%
为什么我必须调用它而不只是设置它?你能指导我很多细节吗?
提前致谢!
最佳答案
它强制变量进行第二次扩展。第一个用于命令本身,第二个用于执行调用时。当不使用延迟扩展时,这是导致变量嵌套扩展的一种方法。以下是 SS64 ( https://ss64.com/nt/call.html#advanced ) 的示例:
@Echo off
SETLOCAL
set _server=frodo
set _var=_server
CALL SET _result=%%%_var%%%
echo %_result%
在本例中,它用于扩展子字符串表达式中的%strlen%:https://ss64.com/nt/syntax-substring.html
关于batch-file - 这条线如何批量工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76591640/