Matt G
Matt G

Reputation: 23

Variable not incrementing in Windows Batch file recursive loop

I am an inexperienced working with batch scripts, and was tasked with appending an incrementing number to a log name. This script uses 7zip's CLI tool to scan zip files, and produce a log. Here is the relevant bit that I am having trouble with (the "SETLOCAL EnableDelayedExpansion","SET count=1", "_%count%" and "set /a count+=1 bits were added by me):

SETLOCAL EnableDelayedExpansion
SET count=1
for /R G: %%g in (*.zip) DO %rundir%\\7za.exe t -r -p"<zip password goes here>" "%%~fg" 1> "%logfolder%\\%%~nxg.log_%count%.txt" set /a count+=1 2>&1

The count stays at one, and I am not quite sure how to get it to move. I primarily write code in Python, and at this point, I am tempted to burn it all down and re-write everything with Python. I have no idea what the "2>&1" bit of the code even does. Any help on solving this is much appreciated!

Upvotes: 1

Views: 233

Answers (1)

user7818749
user7818749

Reputation:

Running set /a count+=1 in the same line does not make it a new command. Also you are not using the delayedexpansion you've set. See how I used !count! instead of %count%

@echo off
setlocal enabledelayedexpansion
set count=0
for /R G: %%g in (*.zip) DO (
    set /a count+=1
    %rundir%\7za.exe t -r -p"<zip password goes here>" "%%~fg" >>"%logfolder%\%%~nxg.log_!count!.txt" 2>&1
 )

As for 2>&1 it simply redirects stderr to stdout I am however not sure why you would require that for set /a count+=1 So do not add it, we can add it though for the 7za command in order to log stdout and stderr to the log files(which I already did)

Upvotes: 1

Related Questions