Marky
Marky

Reputation: 13

Batch: Compare File Sizes

I have seen a few different ways to compare files, using COMP, FC and a few others, but what I need to do is a basic file size compare, and then do a local copy of the file if it is larger, to the network file.

The files could be anything from 200kb and 5gb. So I am really only interested in the file size and not content.

I would prefer a basic Dos script to run through a login script.

Check local file A
Check remote file B
If file A is bigger than file B
  Copy B to A

Upvotes: 1

Views: 6707

Answers (1)

mojo
mojo

Reputation: 4132

The FOR loop expansion z will return the size of a file (e.g. %~zl). Delayed variable expansion is a requirement for this script. This particular script only copies if the file is larger, but you could easily change it to copy the file if the size is at all different by changing GTR to NEQ.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET REMOTE_DIR=\\SERVER\share\path\to\files
SET LOCAL_DIR=.

FOR /F %%f IN (files_to_compare.txt) DO (
    SET "REMOTE_FILE=%REMOTE_DIR%\%%~nxf"
    SET "LOCAL_FILE=%LOCAL_DIR%\%%~nxf"
    FOR %%r IN (!REMOTE_FILE!) DO (
        @ECHO [REMOTE] %%~nxr: %%~zr
        FOR %%l IN (!LOCAL_FILE!) DO (
            @ECHO [LOCAL] %%~nxl: %%~zl
            IF %%~zr GTR %%~zl (
                @ECHO %%~nxr: Remote file IS larger [%%~zr] ^> [%%~zl]
                COPY /Y "%%~r" "%%~l"
            ) ELSE (
                @ECHO %%~nxr: Remote file is not larger [%%~zr] ^<= [%%~zl]
            )
        )
    )
)

Upvotes: 1

Related Questions