Working with large file sizes is often awkward with batch because of the 32 bit signed integer limitation. Even the IF GEQ comparison is limited to 32 bit signed integers. Values greater than 2147483647 (0x7FFFFFFF) are all treated as equal to 2147483647. So your script would fail if your threshold were something like 3 GB instead of 1 GB. See
http://stackoverflow.com/q/9116365/1012053 for more information.
Then you have the problem you have already discovered with trying to convert numbers into GB or MB using batch. There are simple ways to approximate the computation, as well as multiple simple (but relatively slow) ways to use hybrid scripting to do decimal math with large numbers. Then you have the complex batch scripting that Squashman points out to do precise decimal arithmetic.
But I recently learned that ROBOCOPY can solve this problem very easily. The /MIN:size option handles large numbers, and the displayed size is automatically converted into MB or GB if the file exceeds 1 MB. You can add the /BYTES option to display all sizes as bytes, without any conversion. But it cannot list both bytes and gigabytes as you have in your script.
Code: Select all
robocopy c:\ c:\ /l /s /is /njh /njs /ndl /nc /min:1073741824 /xj
The output is not exactly formatted as you want, but it probably will work for you. For example, here is a partial listing of the output from my C: drive:
Code: Select all
2.9 g c:\hiberfil.sys
5.7 g c:\pagefile.sys
3.4 g c:\test\huge.txt
1.7 g c:\test\testbig.txt
You could use FOR /F to easily parse the output if you want to take control of the formatting.
Code: Select all
@echo off
for /f "tokens=1,2*" %%A in (
'robocopy c:\ c:\ /l /s /is /njh /njs /ndl /nc /min:1073741824 /xj'
) do echo %%A GB - %%C
--OUTPUT--
Code: Select all
2.9 GB - c:\hiberfil.sys
5.7 GB - c:\pagefile.sys
3.4 GB - c:\test\huge.txt
1.7 GB - c:\test\testbig.txt
EDIT - Added /XJ option to prevent possibility of endless cycling due to junction pointsDave Benham