[SOLVED] Batch File Comp command ~ Short Report on Differences

btcomp

Distinguished
Jun 27, 2012
5
1
18,510
I have written a 'COMP' line, COMP d:\osas80\data c:\open_sys_acc_soft_80_backup /d > osas_compare2.txt

The files have been backed up from my D drive to my C drive. The above line compares the files and I can see in the .txt file which ones are the same and which are different. What I really want is just a small report (on screen preferably) that will just tell me the number of files on the source and the number of files on the destination. I know I can tell the process to stop after so many wrong files, but the script needs to only tell the user that the backup is perfect, by letting them know that both directories have the same number of files and that they are all the same. My program does tell in the .txt file, but my example has 155 files and you have to scroll through the whole listing to see if they are all the same. Brevity is important, if successful the user can go on and do their processing, with a backup that can be restored if they have to.
 
Solution
This is ugly, but it works. I'm sure you could clean it up a bit. You use need to use the errorlevel which is set by the COMP command to determine if all files match or not. The ECHO N| automatically sends the "N" key to say "No" to the COMP command that you don't want to compare additional files.
@Echo off
echo n|COMP C:\users\yourname\desktop\1 c:\users\yourname\desktop\2 > results.txt
echo.

if ErrorLevel 0 (set answer=0)
if ErrorLevel 1 (set answer=1)
if ErrorLevel 2 (set answer=2)

if %answer% EQU 0 goto SAME
if %answer% EQU 1 goto ADIFF
if %answer% EQU 2 goto ERR
goto END

:SAME
echo Same
goto END

:ADIFF
echo Different
goto END

:ERR
echo Error: Can't open 1 or more files.
goto END

:END
pause
This is ugly, but it works. I'm sure you could clean it up a bit. You use need to use the errorlevel which is set by the COMP command to determine if all files match or not. The ECHO N| automatically sends the "N" key to say "No" to the COMP command that you don't want to compare additional files.
@Echo off
echo n|COMP C:\users\yourname\desktop\1 c:\users\yourname\desktop\2 > results.txt
echo.

if ErrorLevel 0 (set answer=0)
if ErrorLevel 1 (set answer=1)
if ErrorLevel 2 (set answer=2)

if %answer% EQU 0 goto SAME
if %answer% EQU 1 goto ADIFF
if %answer% EQU 2 goto ERR
goto END

:SAME
echo Same
goto END

:ADIFF
echo Different
goto END

:ERR
echo Error: Can't open 1 or more files.
goto END

:END
pause
 
Solution
Since you didn't provide the source to "COMP" there is no way to know how to (of if possible to) add the functionality you desire.
kanefolf, the source is d:\osas80\data (actually will be a server such as \\server_name\data...) . I guess my source and destination aren't separated by my paste. I reposted it with extra space.
COMP d:\osas80\data c:\open_sys_acc_soft_80_backup /d > osas_compare2.txt

I did get the program to just give me the summation of the two directories so that the client can see if they are identical.
 
This is ugly, but it works. I'm sure you could clean it up a bit. You use need to use the errorlevel which is set by the COMP command to determine if all files match or not. The ECHO N| automatically sends the "N" key to say "No" to the COMP command that you don't want to compare additional files.
@Echo off
echo n|COMP C:\users\yourname\desktop\1 c:\users\yourname\desktop\2 > results.txt
echo.

if ErrorLevel 0 (set answer=0)
if ErrorLevel 1 (set answer=1)
if ErrorLevel 2 (set answer=2)

if %answer% EQU 0 goto SAME
if %answer% EQU 1 goto ADIFF
if %answer% EQU 2 goto ERR
goto END

:SAME
echo Same
goto END

:ADIFF
echo Different
goto END

:ERR
echo Error: Can't open 1 or more files.
goto END

:END
pause


I have put a summation in to show that the total file size is the same & the number of files is the same. I just played around with ERRORLEVEL, and got a mixed result. So I will look at your code and try. Looks good! Thank you very much, gardenman.
 
I have put a summation in to show that the total file size is the same & the number of files is the same. I just played around with ERRORLEVEL, and got a mixed result. So I will look at your code and try. Looks good! Thank you very much, gardenman.

This is ugly, but it works. I'm sure you could clean it up a bit. You use need to use the errorlevel which is set by the COMP command to determine if all files match or not. The ECHO N| automatically sends the "N" key to say "No" to the COMP command that you don't want to compare additional files.
@Echo off
echo n|COMP C:\users\yourname\desktop\1 c:\users\yourname\desktop\2 > results.txt
echo.

if ErrorLevel 0 (set answer=0)
if ErrorLevel 1 (set answer=1)
if ErrorLevel 2 (set answer=2)

if %answer% EQU 0 goto SAME
if %answer% EQU 1 goto ADIFF
if %answer% EQU 2 goto ERR
goto END

:SAME
echo Same
goto END

:ADIFF
echo Different
goto END

:ERR
echo Error: Can't open 1 or more files.
goto END

:END
pause


I was having a problem with ERRORLEVEL. It seems you have to store the error immediately, so that you don't send the wrong one. Using answer as a variable solved that for me. Thanks. Also, had to make sure my COMP was actually working. I put a parameter in the wrong spot, and didn't realize it aborted the command. I will be studying PowerShell in the future, but the program I had was written as Batch File years ago, it seemed easier to just use a Batch program entirely.
 
  • Like
Reactions: gardenman