简体   繁体   中英

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 used set /ai=0 and inside the for-loop set /a i+=1 and this Set command does print the number to console. 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. I also tried echo !i! but that does not work at all. It just prints !i! in the console.

I also added a pause command to the end of the script, but that gets ignored entirely.

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:

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! 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. The alternative methodology could involve using findstr.exe to provide the counting:

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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