# Solved: How to compare 2 file date time stamps in batch file



## srini_vk

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

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

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

what about just doing a directory listing on that folder from the command prompt? Date and time should be listed automatically


----------



## srini_vk

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

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:


 Created
 Modified
 Last Accessed


----------



## ghostdog74

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


----------



## TheOutcaste

ghostdog74 said:


> 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:up:

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.


----------



## ghostdog74

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 "%[email protected]:%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


----------



## srini_vk

Thank you.


----------

