Adding variables in a batch file in Windows

I'm trying to fix something that's simple but yet can't seem to put my finger on it.

I have this very simple batch file that when you drop a folder on it it converts a bunch of files to another format. I'm trying to add a progress indicator to the title bar but for some reason I can't get the set /a to add a value and return anything other than the value I initially put in. here's the code

set Count=0 set NumFiles=0 set oldpct=-1 for %%A in (*.t64) do set /A NumFiles += 1 echo File count = %NumFiles% echo. echo Converting... rem Parse every file in the folder and perform the magic. for %%x in (*.t64) do ( set /A "Count += 1" echo count: %Count% pause set /A pct=100*%Count%/%NumFiles% %C1541_PATH%\c1541.exe -verbose off -silent on -format "fromt64,01" d64 "%%~nx.d64" -tape "%%x" rem calculate progress if "%pct%" gtr "%oldPct%" ( set oldPct=%pct% echo %pct% title Progress: %pct%/100 ) ) pause exit :F_Error cls echo FILE ERROR echo. echo This script only works on folders, not individual files. pause exit :Error cls echo ---------------------- ERROR! ---------------------- echo. echo Please drag and drop a folder on this batch file! echo Please make sure C1541.exe is in the echo correct folder and that the path value in echo this file points to it. echo Look near the top of the script, it is identified. echo. pause exit 

I deliberately omitted a part of the code at the start and the end in order to keep things short.

For some reason, the %count% and the %pct% refuses to be calculated...

When I try using delayedexpansion, I get this message

invalid filename error

But since it's a script that doesn't rely on the WOW64 or wmic, it shouldn't really matter the bit size I'm using (the command prompt smooths out everything)

4

2 Answers

I finally cracked it!

It appears you can't reliably do SET /A functions reliably within a FOR IN loop. But if you create a separate subroutine that you call from within the FOR IN loop, then it works.

So the main part of the script looks like that

for %%A in (*.t64) do set /A NumFiles += 1 echo File count = %NumFiles% echo. echo Converting... rem Parse every file in the folder and perform the magic. for %%x in (*.t64) do ( call :progress %C1541_PATH%\c1541.exe -verbose off -silent on -format "fromt64,01" d64 "%%~nx.d64" -tape "%%x" ) exit :progress set /A Count += 1 set /A pct=100 * Count / NumFiles title Progress: %pct%/100 exit /b 

No DELAYEDEXPANSION or SETLOCAL required like I thought.

Windows 10 64-bit

Yet another way to display / track progress with cmd.

Replace ping -n 2 8.8.8.8 >NUL w/ your command

Using two for loops, delayed expansion, echo, title and redirection display / track progress.

I prefer to know how long a command takes to complete.

If you don't know how to use delayed expansion in a for loop read robvanderwoude.com

Begin CMD:

Make file.txt:

1.t64 2.t64 3.t64 4.t64 5.t64 

Create test files from file.txt:

for /f %a in (file.txt) do cd.> %a 

Track and Display

cmd /v /e /q cd.> %temp%\track.txt for %a in (*.t64) do set /a NumFiles +=1 for /l %a in (1,1,%NumFiles%) Do ( title Start command #%a of %NumFiles% at !time! echo Start command #%a of %NumFiles% at !time! rem Replace ping -n 2 8.8.8.8 >NUL w/ your command ping -n 2 8.8.8.8 >NUL echo End command #%a of %NumFiles% at !time! )>> %temp%\track.txt exit 

End CMD:

Begin Script:

Create test files from files.txt:

for /f %%a in (file.txt) do cd.> %%a 

Track and Display

@rem Track / Display progress with cmd @rem Windows 10 64-bit @echo off setlocal enableextensions enabledelayedexpansion cd.> %temp%\track.txt for %%a in (*.t64) do set /a NumFiles +=1 for /l %%a in (1,1,%NumFiles%) Do ( title Start command #%%a of %NumFiles% at !time! echo Start command #%%a of %NumFiles% at !time! rem Replace ping -n 2 8.8.8.8>NUL with your command ping -n 2 8.8.8.8>NUL echo End command #%%a of %NumFiles% at !time! )>> %temp%\track.txt exit /b 

End Script:

Begin As a for loop is running calculate what percent of the loop has completed and display the percentage with cmd title

How to properly implement delayed expansion in a cmd for loop and eliminate a call, subroutine, and variable.

CMD:

cmd /e /v /q for %a in (*.t64) do set /a NumFiles +=1 for /l %a in (1,1,%NumFiles%) do ( set /a pct=100 * %a / %NumFiles% title Progress: Running command #%a of %NumFiles% at !time! !pct!% of %Numfiles% commands rem Replace ping -n 2 8.8.8.8>NUL w/ your command ping -n 2 8.8.8.8> NUL ) exit 

Script:

@rem As a for loop is running calculate what percent of the loop has completed and display the percentage with cmd title @rem Windows 10 64-bit @echo off setlocal enableextensions enabledelayedexpansion for %%a in (*.t64) do set /a NumFiles +=1 for /l %%a in (1,1,%NumFiles%) do ( set /a pct=100 * %%a / %NumFiles% rem title requires %% to display % in script title Progress: Running command #%%a of %NumFiles% at !time! !pct!%% of %Numfiles% commands rem Replace ping -n 2 8.8.8.8 >NUL w/ your command ping -n 2 8.8.8.8>NUL ) exit /b 

End As a for loop is running calculate what percent of the loop has completed and display the percentage with cmd title

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like