script to count number of files and compare source and dest

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ngi
Posts: 2
Joined: 01 Apr 2015 00:16

script to count number of files and compare source and dest

#1 Post by ngi » 01 Apr 2015 00:36

Trying to create batch script which will count number of files on source and destination location along with it will compare files at both source and destination location.

I am able to create basic script to count number of files but unable to compare it source and destination.

Script is as below:

@echo off
for /f %%A in ('dir ^| find "File(s)"') do set cnt=%%A
echo File count = %cnt%

Note: Source files are copied to the destination with robocopy

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: script to count number of files and compare source and d

#2 Post by foxidrive » 01 Apr 2015 02:09

Are you able to use third party tools?

ngi
Posts: 2
Joined: 01 Apr 2015 00:16

Re: script to count number of files and compare source and d

#3 Post by ngi » 01 Apr 2015 04:02

foxidrive wrote:Are you able to use third party tools?


No

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: script to count number of files and compare source and d

#4 Post by foxidrive » 01 Apr 2015 04:16

Have fun with fc.exe

Of course you may not have meant that you want to compare the files themselves - your question is not all that clear.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: script to count number of files and compare source and d

#5 Post by Squashman » 01 Apr 2015 06:05

If you just want to know which files are different without seeing the differences you can do something like this.

Code: Select all

fc file1.txt file2.txt >nul 2>&1 ||echo file1.txt and file2.txt are different


You could expand on this to make it dynamic and you could have it execute any command when the files are not the same.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: script to count number of files and compare source and d

#6 Post by thefeduke » 05 Apr 2015 13:46

My first post may not be in the spirit of DOSTips in brevity nor elegance but here is an excerpt of some backup code that is similar in approach. You have to imagine values for some of the variables.

Code: Select all

    IF EXIST %temp%\'homepics.txt ERASE %temp%\'homepics.txt
    FOR /F "usebackq tokens=1-4,5* " %%i in (`DIR "%homedrive%%homepath%\%folder%\*.*" /A:-S-d /S`) do (
        IF "Directory"=="%%i" ECHO %%i %%j %%k %%l %%m %%n >>%temp%\'homepics.txt
        IF "File(s)"=="%%j" ECHO %%i %%j %%k %%l %%m %%n >>%temp%\'homepics.txt
        IF "File(s)"=="%%j" ((set phomeuse=%%k) & (set homefiles=%%i))
        IF "Dir(s)"=="%%j" ECHO %%i %%j %%k %%l %%m %%n >>%temp%\'homepics.txt
    )

    IF EXIST %temp%\'bKPpics.txt ERASE %temp%\'bKPpics.txt
    FOR /F "usebackq tokens=1-4,5* " %%i in (`DIR "%'BKPfldr%\*.*"                     /A:-S-d /S`) do (
        IF "Directory"=="%%i" ECHO %%i %%j %%k %%l %%m %%n >>%temp%\'Bkppics.txt
        IF "File(s)"=="%%j" ECHO %%i %%j %%k %%l %%m %%n >>%temp%\'Bkppics.txt
        IF "File(s)"=="%%j" ((set pBkpuse=%%k) & (set Bkpfiles=%%i))
        IF "Dir(s)"=="%%j" ECHO %%i %%j %%k %%l %%m %%n >>%temp%\'Bkppics.txt
    )

    set Bkpuse=%pBkpuse:,=%
    set homeuse=%phomeuse:,=%

    set muline=\f0\fs32 A Check of Picture Files against Backup reveals that
    CALL :muline

    set muline=\f1\fs36\b %homefiles%\f0\fs32\b0  Live Picture Files and \f1\fs36\b %Bkpfiles%\f0\fs32\b0  Backup files were found.
    CALL :muline

rem make some decisions about invoking an automatic backup using no delayed vars
    IF /I "%homefiles%" EQU "0" (
        set 'rst_SW=ON
        echo . Harmless restore list follows:
        xcopy  "%'Rdrv%%'BKPdir%\%username%\%'BKPtype%.bkp\*.*" "%homedrive%%homepath%\%folder%" /y /i /e /d /C /q /l
        set 'br_SW=ON
        set muline=No Live Picture Files imply a Windows 8 Refresh was done.
        CALL :muline
        set muline=Automatic Restore started based on the empty folder.
        CALL :muline
        IF NOT EXIST %'Rdrv%%'BKPdir%\"Picture Backup Management Facility - Shortcut.lnk" (
            echo . Creating Picture Backup Management Facility Desktop Shortc from drive %'rdrv%
            set 'sht_SW=ON
            set muline=Missing Shortcuts being re-created by %'Rdrv%Rdrv.bat.
            CALL :muline
        )
    ) ELSE (
        IF /I "%homefiles%" EQU "%bkpfiles%" (
            set muline=No Action is being taken based on the number of files.
            CALL :muline
            set muline=%phomeuse% Live bytes are backed up by %pbkpuse% bytes
            CALL :muline
            IF /I "%homeuse%" EQU "%bkpuse%" (
                set muline=No Action is being taken based on equal byte counts.
                CALL :muline
            ) ELSE (
                set 'bkp_SW=ON
                echo . Harmless backup list follows:
                xcopy "%homedrive%%homepath%\%folder%\*.*" "%'Rdrv%%'BKPdir%\%username%\%'BKPtype%.bkp" /y /i /e /d /C /q /l
                set 'br_SW=ON
                set muline=\b Activity since the last backup has been detected.\b0   .
                CALL :muline
                IF /I "%homeuse%" LSS "%bkpuse%" (
                    set muline=Automatic backup started in case of cropped %'BKPtype%.
                    CALL :muline
                ) ELSE (
                    set muline=Automatic backup started in case of magnified %'BKPtype%.
                    CALL :muline
                )
            )
        ) ELSE (
            set 'bkp_SW=ON
            echo . Harmless backup list follows:
            xcopy "%homedrive%%homepath%\%folder%\*.*" "%'Rdrv%%'BKPdir%\%username%\%'BKPtype%.bkp" /y /i /e /d /C /q /l
            set 'br_SW=ON
            IF /I %homefiles% GTR %bkpfiles% (
                set muline=Automatic backup started based on the number of files.
                CALL :muline
            ) ELSE (
                set muline=More files are backed up than still exist in %'BKPtype%.
                CALL :muline
                set muline=%phomeuse% Live bytes are backed up by %pbkpuse% bytes
                CALL :muline
                set muline=Automatic backup started because Files appear to have been deleted.
                CALL :muline
            )
        )
    )

It's clumsy but handles the two directory list comparison at a more detailed level.
Last edited by Squashman on 17 Apr 2015 08:21, edited 1 time in total.
Reason: MOD EDIT: Please use CODE TAGS.

Post Reply