Reputation: 321
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 for-loop and that is working correctly. Now I added a variable i
which should increment on each iteration of the loop.
I used set /a i=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?
Upvotes: 0
Views: 519
Reputation: 38589
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
Upvotes: 1
Reputation:
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.
Upvotes: 0