I've been racking my head over my script on why the section for finding out the directory size in bytes does not work but after a long time of debugging i've encountered the following scenario:
- if I run the for loop inside an IF statement it doesn't work
- if I run the for loop outside the IF statement it does work
- if I run the for loop outside the IF statement and then run it inside afterwards, it does work
Is the for loop written incorrectly or am I misunderstanding or missing something?
The following does NOT work
@ECHO OFF
set "test=1"
if %test% GTR 0 (
echo Scenario A
pushd "%~dp0"
set dir="C:\temp"
for /f "tokens=3" %%i in ('robocopy /l /e /bytes %dir% %dir% ^| findstr Bytes') do set size=%%i
echo Size = %size%
pause
) else (
echo Scenario B
pause
)
But this DOES work for both runs
@ECHO OFF
set "test=1"
echo Method a
pushd "%~dp0"
set dir="C:\temp""
for /f "tokens=3" %%i in ('robocopy /l /e /bytes %dir% %dir% ^| findstr Bytes') do set size=%%i
echo Size = %size%
echo Now Method A in IF statement
if %test% GTR 0 (
echo Scenario A
pushd "%~dp0"
set dir="C:\temp"
for /f "tokens=3" %%i in ('robocopy /l /e /bytes %dir% %dir% ^| findstr Bytes') do set size=%%i
echo Size = %size%
pause
) else (
echo Scenario B
pause
)