Patrick Prescott
Patrick Prescott

Reputation: 3

repeating variable issue in windows batch file

I have the following code in a batch file:

    set /p user="Enter username: " %=%
    cd w:\Files
    @echo off 
    setLocal EnableDelayedExpansion 
    set /a value=0 
    set /a sum=0 
    set /a valA=0
    FOR /R %1 %%I IN (*) DO ( 
    set /a value=%%~zI/1024 
    set /a sum=!sum!+!value! 
    set /a sum=!sum!/1024
    ) 
    @echo Size of "FILES" is: !sum! MB
    @echo off
    FOR /R %1 %%I IN (*) DO ( 
    set /a sum=!sum!/1024
    set /a valA=!sum!
    )
    @echo Size of "FILES" is: !sum! GB


    cd W:\Documents and Settings\%user%\Desktop
    @echo off 
    setLocal EnableDelayedExpansion 
    set /a value=0 
    set /a sum=0 
    set /a valB=0
    FOR /R %1 %%I IN (*) DO ( 
    set /a value=%%~zI/1024 
    set /a sum=!sum!+!value! 
    set /a sum=!sum!/1024
    ) 
    @echo Size of Desktop is: !sum! MB
    @echo off
    FOR /R %1 %%I IN (*) DO ( 
    set /a sum=!sum!/1024
    set /a valB=!sum!
    )
    @echo Size of Desktop is: !sum! GB

There are a few other folders it checks, but you should get the idea.

I get this output:

C:\Users\pprescott\Desktop>cd w:\Files
Size of "FILES" is: 215 MB
Size of "FILES" is: 0 GB
Size of Desktop is: 215 MB
Size of Desktop is: 0 GB
Size of Favorites is: 215 MB
Size of Favorites is: 0 GB
Size of Documents is: 215 MB
Size of Documents is: 0 GB
Total size is: 0 MB
Total size is: 0 GB
Press any key to continue . . .

This is designed to count folder size on an old xp machine to prepare for a data transfer. The xp machine is mapped to drive W.

Upvotes: 0

Views: 234

Answers (3)

foxidrive
foxidrive

Reputation: 41244

This should report the size of the trees in bytes up to 999 GB.

The lower answer uses a VBS script to convert the numbers to GB (dropping decimals)

@echo off
call :size "w:\Files"
call :size "W:\Documents and Settings\%user%\Desktop" 
pause
goto :eof
:size
for /f "tokens=3" %%b in ('dir /s "%~1" 2^>nul ^|find " File(s) "') do set "n=%%b"
for /f "tokens=1-4 delims=," %%c in ("%n%") do (
echo %%c%%d%%e%%f "%~1"
)
)

Solution to return approx GB figures

@echo off
call :size "w:\Files"
call :size "W:\Documents and Settings\%user%\Desktop" 
pause
goto :eof
:size
for /f "tokens=3" %%b in ('dir /s "%~1" 2^>nul ^|find " File(s) "') do set "n=%%b"
for /f "tokens=1-4 delims=," %%c in ("%n%") do (

>"%temp%\VBS.vbs" echo Set fso = CreateObject^("Scripting.FileSystemObject"^) : Wscript.echo ^(%%c%%d%%e%%f/1024/1024^)
for /f "tokens=1 delims=." %%z in ('cscript /nologo "%temp%\VBS.vbs"') do echo %%z GB "%~1"
del "%temp%\VBS.vbs"

)
)

Upvotes: 1

David Ruhmann
David Ruhmann

Reputation: 11367

Use the dir command output. See this example

This will take the folder size and convert it into the different values. Note that the conversion only works for numeric values up to 2 gigabytes.

@echo off
for /f "tokens=3" %%A IN ('dir /s /-c ^| find /i "bytes" ^| find /v /i "free"') do set T=%%A

echo Bytes = %T%
set /a T/=1024
echo Kilobytes = %T%
set /a T/=1024
echo Megabytes = %T%
set /a T/=1024
echo Gigabytes = %T%

Output:

C:\Users\User\Desktop>Test.bat
Bytes = 18280552
Kilobytes = 17852
Megabytes = 17
Gigabytes = 0

Upvotes: 1

Endoro
Endoro

Reputation: 37569

try to remove the for /r parameter %1:

FOR /R %%I IN (*) DO (

Try this code:

@ECHO OFF &SETLOCAL
FOR /R "w:\Files" %%I IN (*) DO set /a asum+=%%~zI
SET /a sum=asum/1048576
echo Size of "FILES" is: %sum% MB
set /a sum=asum/1073741824
echo Size of "FILES" is: %sum% GB
FOR /R "W:\Documents and Settings\%user%\Desktop" %%I IN (*) DO set /a asum+=%%~zI
SET /a sum=asum/1048576
echo Size of "DESKTOP" is: %sum% MB
set /a sum=asum/1073741824
echo Size of "DESKTOP" is: %sum% GB

Upvotes: 2

Related Questions