简体   繁体   English

批处理脚本不回显变量的内容并且暂停不起作用

[英]Batch script does not echo contents of a variable and pause not working

I am working on a script which iterates over every file in a specific folder and reads some information from, and numbers each.我正在编写一个脚本,它遍历特定文件夹中的每个文件,并从中读取一些信息,并为每个文件编号。

So I am running over the files with a and that is working correctly.所以我正在使用运行文件,并且工作正常。 Now I added a variable i which should increment on each iteration of the loop.现在我添加了一个变量i ,它应该在循环的每次迭代中递增。

I used set /ai=0 and inside the for-loop set /a i+=1 and this Set command does print the number to console.我使用了set /ai=0并在 for-loop set /a i+=1内部使用了这个Set命令确实将数字打印到控制台。 My problem now is that the set command prints the number, but when I echo the number with echo %i% it will always print 0 and not the increasing value.我现在的问题是set命令打印数字,但是当我用echo %i%回显数字时,它总是会打印0而不是增加的值。 I also tried echo !i!我也试过echo !i! but that does not work at all.但这根本行不通。 It just prints !i!它只是打印!i! in the console.在控制台中。

I also added a pause command to the end of the script, but that gets ignored entirely.我还在脚本末尾添加了一个pause命令,但它被完全忽略了。

This is my batch script:这是我的批处理脚本:

@echo off
setlocal EnableDelayedExpansion

set /a i=0

for /r %%n in (Links\*.lnk) do (

    set /a i+=1
    echo.
    echo [Button!i!Back]
    get.bat "%%n"

)

pause

This is an example of the output:这是 output 的示例:

45
[Button!i!Back]
#@#HudIcons\VLC media player.ico
D:\Programme\VideoLAN\VLC\vlc.exe

I also just realized, that for the first time the loop runs, the !i!我也刚刚意识到,循环第一次运行, !i! does work correctly and prints the number, but not afterwards.确实可以正常工作并打印数字,但之后不会。

I know that I should probably not be calling the other batch file like this, but that is temporary.我知道我可能不应该像这样调用另一个批处理文件,但那是暂时的。

Any ideas why this is behaving so weird?任何想法为什么这会表现得如此奇怪?

Perhaps it would be easier for you without the Set /A incrementing method, and therefore no need for delayed expansion.如果没有Set /A递增方法,也许对您来说会更容易,因此不需要延迟扩展。 The alternative methodology could involve using findstr.exe to provide the counting:替代方法可能涉及使用findstr.exe来提供计数:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Tokens=1,* Delims=:" %%G In ('Dir /B /S /A:-D "Links\*.lnk" ^
 2^> NUL ^| %SystemRoot%\System32\findstr.exe /EILN ".lnk"') Do (Echo=
    Echo [Button%%GBack]
    Call "get.bat" "%%H")
Pause

You use percentages symbol to call a variable %Variable% and to echo it echo %variable% to set one set variable=value Hope this helps you.您使用百分比符号来调用变量%Variable%并回显它echo %variable%以设置一set variable=value希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM