Daniel Bartley
Daniel Bartley

Reputation: 11

Windows Batch File Nested For Loops

Having a midlife crisis with this one. I have a txt file with a list of files and filesize that I am looping through and comparing to what is actually found on disk. The list of files is a remote file on another server.

Data:

\videos\20201103-0_FastestLap2017WorldTimeAttackMCAS13Silvia1.20.9.mp4|72184632
\videos\20201103-0_WTAC2015SteveKasWTACR34GTRSkylinePreWTACTestingatSydneyMotorsportPark.mp4|402572676
\videos\20201103-0_WTAC2016MCASuspensionHammerheadS13TimSlade1.22.19.mp4|353526548
\images\20201103-0_FeatDOP_4283.jpg|421791
\images\20201103-0_battles4.jpg|835931
\images\20201103-0_3N8A3099copy.jpg|1696647
\videos\20201103-0_ATTACKbestonboardlapsofWTAC2018RP968HammerheadXtremeGTR.TRB03andmore.mp4|406519905

Code:

@ECHO OFF

SET TestDir=S:\Documents\testplatform\data\
@ECHO Checking %TestDir%    
    
SETLOCAL EnableDelayedExpansion

SET ActualSize=0

for /F "tokens=1,2 delims=|" %%i in (%TestDir%\files.txt) do (

    for %%A in (%TestDir%%%i) do (
        set ActualSize=%%~zA
    )

    @ECHO File: %%i, Server Size: %%j, ActualSize: !ActualSize!
)

PAUSE
SETLOCAL DisableDelayedExpansion

The issue is, no matter how I try and use %% or !!, I cant get ActualSize to display the Actual Size. I know its something to do with set ActualSize=%%~zA not having the correct scoping within the batch file.

Checking S:\Documents\testplatform\data\
File: \videos\20201103-0_FastestLap2017WorldTimeAttackMCAS13Silvia1.20.9.mp4, Server Size: 72184632, ActualSize:
File: \videos\20201103-0_WTAC2015SteveKasWTACR34GTRSkylinePreWTACTestingatSydneyMotorsportPark.mp4, Server Size: 402572676, ActualSize:
File: \videos\20201103-0_WTAC2016MCASuspensionHammerheadS13TimSlade1.22.19.mp4, Server Size: 353526548, ActualSize:
File: \images\20201103-0_FeatDOP_4283.jpg, Server Size: 421791, ActualSize:
File: \images\20201103-0_battles4.jpg, Server Size: 835931, ActualSize:
File: \images\20201103-0_3N8A3099copy.jpg, Server Size: 1696647, ActualSize:
File: \videos\20201103-0_ATTACKbestonboardlapsofWTAC2018RP968HammerheadXtremeGTR.TRB03andmore.mp4, Server Size: 406519905, ActualSize:

any help would be much appreciated.

Upvotes: 0

Views: 92

Answers (2)

Daniel Bartley
Daniel Bartley

Reputation: 11

working code:

@ECHO OFF

SET TestDir=S:\Documents\testplatform\data\
SET MaxDifference=256
@ECHO Checking %TestDir%

    
SETLOCAL EnableDelayedExpansion

SET ActualSize=0

:: loop over the test data file
for /F "tokens=1,2 delims=|" %%i in (%TestDir%\files.txt) do (

    :: get the filesize of each file and save it in ActualSize
    for %%A in (%TestDir%%%i) do (
        SET /A ActualSize=0
    SET /A ActualSize=%%~zA
    )

    
    SET /A Difference=!ActualSize!-%%j
    IF !Difference! LSS 0 SET /A Difference=!Difference!*-1
    IF !Difference! GTR !MaxDifference! (
        @ECHO File: %%i, Server Size: %%j, ActualSize: !ActualSize!, Difference: !Difference!
    )
)
SETLOCAL DisableDelayedExpansion

PAUSE

Outputs:

Checking S:\Documents\testplatform\data\
File: /playlist-1.html, Server Size: 6360, ActualSize: 6581, Difference: 221
File: /playlist-2.html, Server Size: 6340, ActualSize: 6561, Difference: 221
File: /playlist-3.html, Server Size: 6396, ActualSize: 6617, Difference: 221
File: /playlist-6.html, Server Size: 6385, ActualSize: 6606, Difference: 221
Press any key to continue . . .

Upvotes: 0

user14122392
user14122392

Reputation: 79

:: get the filesize of each file and save it in ActualSize is an illegal statement. Remove it or convert it to a Remark (aka comment) - REM get the filesize of each file and save it in ActualSize.

You are posting illegal code that you found and asking why illegal code doesn't work. Because it is illegal.

: is a Goto destination known as a label in Windows command line interface.

Type rem /? and goto /?.

Upvotes: 1

Related Questions