Page 1 of 1
compare files and echo message
Posted: 21 Jan 2010 08:27
by pink
How do I compare two files?
fc c:\data\myfile.txt c:\arch\myfile.txt
this is working fine. now i want to echo in log file if they are same. if they are not same i need to check the size of the file. if it is zero echo in log file.
I dont know how to check the file is same or different.
Any help is appreciated. i have ONLY 2 more days to get this done.
Thansk
Pink
Posted: 21 Jan 2010 11:25
by !k
pinkCode: Select all
@echo off
set "data=c:\data\myfile.txt"
set "arch=c:\arch\myfile.txt"
set "log=c:\logfile.txt"
rem echo N| comp "%data%" "%arch%" 2>nul 1>nul &&echo Equal> "%log%" ||(
fc "%data%" "%arch%" >nul &&echo Equal> "%log%" ||(
echo Not equal> "%log%"
for %%a in ("%data%" "%arch%") do if "%%~za"=="0" echo Zero sized %%a>> "%log%"
)
Note: COMP faster because they do not compare files of different sizes
Re: compare files and echo message
Posted: 28 Jan 2010 09:23
by pink
Thanks !k. it worked. God I spent so much time on that....
What is the equivalent of unix 'Sleep' command in windows? I need the program to sleep sometime when doing the file comparison assuming that I could get big files to compare.
Thanks
Pink
Re: compare files and echo message
Posted: 28 Jan 2010 09:59
by !k
pink wrote:I need the program to sleep sometime...
5 way in
Need to create a 60 minutes delay in my DOS script
Re: compare files and echo message
Posted: 01 Feb 2010 13:34
by pink
Thanks.
Now I have a different issue. If the two files are different but empty it is giving the wrong message. Is there a way I can check the timestamps of the files before I do the fc.
fc T:\data\test.dat T:\arch\test.dat >nul &&(echo File already processed.. >>T:\ftp\test.log
goto end) ||(echo New file found.. >>T:\ftp\test.log
ping localhost -n 9 >nul
for /f %%A in ("T:\data\test.dat") do if %%~zA equ 0 goto zerofile
Thanks
Pink
Re: compare files and echo message
Posted: 01 Feb 2010 14:37
by !k
Code: Select all
echo %date% %time% >>T:\ftp\test.log
for %%A in ("T:\data\test.dat" "T:\arch\test.dat") do echo %%A timestamp %%~tA, size %%~zA >>T:\ftp\test.log
Re: compare files and echo message
Posted: 01 Feb 2010 14:48
by aGerman
pinkI'm not sure about, but maybe you could use something like that.
Code: Select all
@echo off &setlocal
for /f "delims=" %%a in ("T:\data\test.dat") do (
for /f "delims=" %%b in ("T:\arch\test.dat") do (
if "%%~ta"=="%%~tb" (
if "%%~za"=="0" (
set option=1
) else (
set option=2
)
) else (
if "%%~za"=="0" (
set option=3
) else (
set option=4
)
)
)
)
set answer1=Timestamps are equal, file size is zero.
set answer2=Timestamps are equal, file size is greater than zero.
set answer3=Timestamps are different, file size is zero.
set answer4=Timestamps are different, file size is greater than zero.
call echo %%answer%option%%%>>T:\ftp\test.log
Regards
aGerman
Re: compare files and echo message
Posted: 07 Feb 2010 10:52
by SenHu
Nice site. Lots of helpful people. Lots of answers.
Anyway, back to the point. Being a teacher, I will take a theoretical approach. You have a few things going on here when comparing files.
- File content the same ?
- File exists or not ?
- File empty or not ? ( I will generalize - Size the same ? )
- File mod time the same ?
- File creation time the same ?
Here is a script in biterscripting (
http://www.biterscripting.com ). Can transfer to batch or use as is. I have added comments and spacing for ease of transfer.
Code: Select all
# Script CompareFiles.txt
var str path1, path2
var str content1, content2
var bool contentsame
# For each file, we will create an output string of form -
# exists,size,modtime,createtime
# If this string is same for both files and
# $contentsame is true, files are IDENTICAL.
var str output1, output2
# Check the content.
cat $path1 > $content1 ; cat $path2 > $content2
if ($content1==$content2)
set $contentsame = true
endif
# Get file info on file1.
set $fsize=0 ; set $fmtime="" ; set $fctime=""
af $path1
set $output1 = makestr(bool($fexists))+","+makestr(int($fsize))+","+$fmtime+","+$fctime
# Get file info on file2.
set $fsize=0 ; set $fmtime="" ; set $fctime=""
af $path2
set $output2 = makestr(bool($fexists))+","+makestr(int($fsize))+","+$fmtime+","+$fctime
# Show results to the user
if ($contentsame AND ($output1==$output2))
echo "FILES ARE IDENTICAL."
else
if ( NOT ($contentsame) )
echo "FILE CONTENTS ARE DIFFERENT."
else
echo "FILE ATTRIBUTES (Listed below) ARE DIFFERENT.\n" $output1 "\n" $output2
endif
endif
Save the script in file C:/Scripts/CompareFiles.txt, and call it.
Code: Select all
script "C:/Scripts/CompareFiles.txt" path1("c:\data\myfile.txt") path2("c:\arch\myfile.txt")
Nice challenge. Good question.