0

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
    do you have delayed expansion enabled? See for instance: ss64.com/nt/delayedexpansion.html
    – Yorik
    Nov 22, 2019 at 21:16
  • No I don't. I didn't think it would do anything in this instance. But I could be wrong as I rarely use delayedexpansion in my scripts...
    – Flaver-D
    Nov 23, 2019 at 15:57
  • I tried the new code but still get the same error. It seems it's closely related to the delayedexpansion.
    – Flaver-D
    Nov 24, 2019 at 19:33
  • cmd.exe in Windows is not MS-DOS. They're very different. There are no set /a, setlocal, functions, code blocks or delayed expansion in DOS
    – phuclv
    Nov 26, 2019 at 4:50

2 Answers 2

0

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.

0

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
  • Pretty darn good. I get the title to show the correct progress. But now I'm having problems with my conversion string. In it I use both %%~nx and %%x and for some reason the converter now gives me an error...
    – Flaver-D
    Nov 23, 2019 at 16:45
  • @fla comment to me when you have done the following: put the conversion string, and anything it needs, in your question.
    – somebadhat
    Nov 23, 2019 at 17:25
  • I've now put everything that 's in my script in the question. The C1541.exe file comes from the Vice64 emulator.
    – Flaver-D
    Nov 23, 2019 at 21:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.