batch-file - 这条线如何批量工作

标签 batch-file cmd

我想创建自己的“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/

相关文章:

powershell - 如何更改 Windows 的默认控制台主机应用程序?

windows - 批处理文件以检查文件列表是否存在。不适用于空格

batch-file - 使用 ffmpeg 将 youtube-dl webm 文件转换为 mp3 的批处理脚本

powershell - 如何重命名目录中的多个文件?

windows - 如何从文件打开命令提示符

python - 如何在cmd中运行不同的python版本

python - Powershell 和 Python - 如何以管理员身份运行命令

windows - 捕获输出命令CMD

batch-file - 如何在 for/F 循环内设置变量

sql - 如何显示文本文件sql中受影响的行数