JSI Tip 5100. How do I compare the size of two files?

Comparing the size of two files turns out to be non-trivial as the the maximum size of a numeric variable is 11 digits and a character compare requires that the two fields be of equal size.

I have scripted CompSize.bat to circumvent these issues.

The syntax for using CompSize.bat is:

call CompSize File1 File2

where File1 and File2 are the fully qualified paths to the files to be compared.

Samples

Call CompSize c:\pagefile.sys \\<Computername>\c$\pagefile.sys

Call CompSize "C:\Documents and Settings\%UserName%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\backup01.log" "C:\Documents and Settings\%UserName%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\backup02.log"

CompSize.bat returns a $Size environment variable with the results of the compare:

LSS File1 is Less Than File2.
EQU File1 is Equal To File2.
GTR File1 is Greater Than File2.
ERR Syntax Error.

In addition to testing %$Size% with an IF statement (if "%$Size%" EQU "GTR" goto greater), you can also build a GOTO:

call CompSize c:\hiberfil.sys c:\pagefile.sys
goto lbl%$Size%
:lblERR
@echo ERR
goto :EOF
:lblLSS
@echo LSS
goto :EOF
:lblEQU
@echo EQU
goto :EOF
:lblGTR
@echo GTR

CompSize.bat contains:

@echo off
setlocal
if \{%2\}==\{\} goto syntax
if not exist %1 goto syntax
if not exist %2 goto syntax
for /f "Skip=6 Tokens=2,3" %%s in ('dir %1') do if /i "%%s" EQU "File(s)" set sz1=%%t
set sz1=%sz1:,=%
set sz1=%sz1% 000000000000000000
for /f "tokens=1,2" %%s in ("%sz1:~,18%") do set sz1=%%t%%s
for /f "Skip=6 Tokens=2,3" %%s in ('dir %2') do if /i "%%s" EQU "File(s)" set sz2=%%t
set sz2=%sz2:,=%
set sz2=%sz2% 000000000000000000
for /f "tokens=1,2" %%s in ("%sz2:~,18%") do set sz2=%%t%%s
if "%sz1%" LSS "%sz2%" set $size=LSS&goto return
if "%sz1%" EQU "%sz2%" set $size=EQU&goto return
set $size=GTR
:return
endlocal&set $size=%$size%
goto :EOF
:syntax
@echo CompSize File1 File2
set $size=ERR
endlocal&set $size=%$size%

NOTE: See General purpose zero fill (padding) routine.



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish