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
script to count number of files and compare source and dest
Moderator: DosItHelp
Re: script to count number of files and compare source and d
Are you able to use third party tools?
Re: script to count number of files and compare source and d
foxidrive wrote:Are you able to use third party tools?
No
Re: script to count number of files and compare source and d
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.
Of course you may not have meant that you want to compare the files themselves - your question is not all that clear.
Re: script to count number of files and compare source and d
If you just want to know which files are different without seeing the differences you can do something like this.
You could expand on this to make it dynamic and you could have it execute any command when the files are not the same.
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.
Re: script to count number of files and compare source and d
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.
It's clumsy but handles the two directory list comparison at a more detailed level.
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.
Reason: MOD EDIT: Please use CODE TAGS.