There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
DOS/Other
Tag Cloud
access acer asus bios bsod computer crash driver drivers error ethernet excel freeze gaming gpu hard drive hardware hdmi internet laptop malware memory monitor motherboard music network obp operating system printer problem ram registry router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/Other >
Solved: How to compare 2 file date time stamps in batch file

Reply  
Thread Tools
srini_vk's Avatar
Junior Member with 6 posts.
 
Join Date: Jun 2009
15-Jun-2009, 02:55 PM #1
Solved: How to compare 2 file date time stamps in batch file
Hi All,

I've 2 files a.c and b.c
How can I find which one is newer(datetime) among them using either batch file OR at command line

Thanks,
Srini
leitservices's Avatar
Junior Member with 11 posts.
 
Join Date: Jun 2009
Location: Ontario, Canada
Experience: Advanced
15-Jun-2009, 03:02 PM #2
At a command prompt try this:
From the directory where your file is stored,
type echo %date% %time% "filename" (no quotes)
Does that help?
srini_vk's Avatar
Junior Member with 6 posts.
 
Join Date: Jun 2009
15-Jun-2009, 03:18 PM #3
When I do this, it is returning "today's date, time and filename" NOT the date time of the file.

I'd like to compare and decide which one is newer.

For e.g., datetime(a.c) > datetime(b.c)
leitservices's Avatar
Junior Member with 11 posts.
 
Join Date: Jun 2009
Location: Ontario, Canada
Experience: Advanced
15-Jun-2009, 03:25 PM #4
what about just doing a directory listing on that folder from the command prompt? Date and time should be listed automatically
srini_vk's Avatar
Junior Member with 6 posts.
 
Join Date: Jun 2009
15-Jun-2009, 03:32 PM #5
Yes, it I list all in the directory it lists all files date tme. But I'm interested in specific files datetime info and I'd like to compare to find which one is newer. Later I'll use this in batch file.
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
15-Jun-2009, 09:07 PM #6
Do you just need to know which is newer, or will you also need to know the actual date/time stamp for each file?
Are both files in the same folder?
Also, which time stamp do you wish to compare:
  1. Created
  2. Modified
  3. Last Accessed
ghostdog74's Avatar
Member with 146 posts.
 
Join Date: Dec 2005
16-Jun-2009, 06:18 AM #7
this finds the latest
Code:
dir /b /OD file1.txt file2.txt | more +1
alternatively, you can download GNU tools for windows, eg you can use stat (fileutils)
Code:
BEGIN{ q="\047"}
{
 cmd="stat -c " q "%Y" q" "$0
 cmd |getline date
 files[date] = $0 
}END{
	n = asorti(files, f)
	print "latest file: " files[f[n]]
}
save the above as myscript.awk and one command line
Code:
C:\test>dir /b file.txt test.bat outfile.txt test.txt | gawk -f test.awk

Last edited by ghostdog74; 16-Jun-2009 at 06:35 AM..
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
16-Jun-2009, 12:52 PM #8
Quote:
Originally Posted by ghostdog74 View Post
this finds the latest
Code:
dir /b /OD file1.txt file2.txt | more +1
This only works if the files are in the same folder, and neither is hidden. (Limitation of the Dir command)
Compares Last Modified Date, but can easily compare Created Date or Last Accessed Date using /TC or /TA.
To capture the filename in a batch file you can use a For loop:
Code:
For /F "Delims=" %%I In ('dir /b /OD file1.txt file2.txt ^| more +1') Do Set _Newer=%%I
You can use the /AH switch if both files are hidden, but it won't work if only one is hidden.
This will also do the same:
Code:
For /F "Skip=1 Delims=" %%I In ('dir /b /OD file1.txt file2.txt') Do Set _Newer=%%I
You can use Xcopy if the files are in the same or different folders, hidden or not, but can only compare the Last Modified dates:
Code:
For /F "Delims=" %%I In ('xcopy /DHYL C:\Folder1\File1.txt D:\Backup\File2.txt ^|Findstr /I "File"') Do set /a _Newer=%%I 2>Nul
If File1 is newer, _Newer is set to 1. If same or older date, it's set to 0.
If you need the file name instead of a 1/0 answer, takes one more line:
Code:
For /F "Delims=" %%I In ('xcopy /DHYL C:\Folder1\File1.txt D:\Backup\File2.txt ^|Findstr /I "File"') Do set /a _Newer=%%I 2>Nul
If %_Newer==1 (Set _Newer=C:\Folder1\File1.txt) Else (Set _Newer=D:\Backup\File2.txt)
This will accept Fully qualified file names on the command line and output the name of the newest file:
Code:
Set _File1=%1
Set _File2=%2
For /F "Delims=" %%I In ('xcopy /DHYL %_File1% %_File2% ^|Findstr /I "File"') Do set /a _Newer=%%I 2>Nul
If %_Newer%==1 (Set _Newer=%_File1%) Else (Set _Newer=%_File2%)
Echo The newest file is %_Newer%
If you need to compare the Created Date or Last Accessed Date for files in different folders, you can move one of them, compare using the first option, then move the file back. Not a good option for larger files, or files on a flash drive. You'd have to parse the Date and time stamp from a Dir for each file, convert it to a number format that batch can handle, and then compare the timestamps. Doable, but much more involved using a batch file. The GNU utilities would be much easier if you need to compare Created/Accessed dates in different folders without moving either file. And it can do multiple files, not just two. Great suggestion ghostdog74


For the Awk script, test.awk should be myscript.awk.
Using Dir, the files have to be in the same folder and you can't mix hidden and non-hidden files.
You can put the filenames into a text file and type that to the gawk script and that will work for any folder/hidden file combination, and can easily check created/accessed dates instead of modified by changing the %Y in myscript.awk to %Z and %X
Code:
Echo C:\File1.ext>filelist.txt
Echo D:\File2.ext>>filelist.txt
Type filelist.txt |gawk -f myscript.awk
Downside is you need to download both gawk and the file utilities, and neither one adds itself to the path statement, so you have to do that manually, or specify the complete path to both gawk and stat. Once installed though, you can use them in a For loop just like Dir or Xcopy to capture the file name.
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

ghostdog74's Avatar
Member with 146 posts.
 
Join Date: Dec 2005
16-Jun-2009, 01:58 PM #9
thanks outcaste, but i am not interested in how to do it with batch and for loops so there's no need to explain it to me . maybe OP might find it interesting though. Personally , with all those GNU tools available, this is what i would do
eg
Code:
c:\test> find_gnu.exe "c:\test" ( -name a.txt -o -name b.txt ) -printf "%T@:%p\n" | sort_gnu -n|tail -1
if the requirement indeed wants to include hidden files, the ls command with -a will show you hidden files, much like dir /ah. after that, scripting is easy
__________________
gawk Win32 | GNU packages|Vbscript
srini_vk's Avatar
Junior Member with 6 posts.
 
Join Date: Jun 2009
16-Jun-2009, 06:53 PM #10
Thank you.
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 10:53 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.