Solved Comparing two file dates in CMD

June 13, 2012 at 06:13:12
Specs: Windows 7

Hello all

hope you can help.

Using a batch file, I have recently been asked to compare two file dates in a particular folder. Lets say the files names are F1.txt and mail.log.

If mail.log date is prior to F1.txt date then I want the batch file command to run another bat file, wait 20 seconds and then compare two other files within another folder...

____________________________________
The logics is like this:

IF mail.log date < F1.txt date in C:\TEST1 then run Run.bat ELSE
Wait 20 seconds
IF mail.log date < F2.txt date in C:\TEST2 then run Run.bat ELSE
Wait 20 seconds....
____________________________________

There are about 10 of these folders.
-I also do not want to run the Run.bat if the file dates in that folder are the -same, only if the mail.log file less than the F. files
-I also want to remove the time portion of the date

I have looked at some code like this:

SET FILE1=mail.log
SET FILE2=F1.txt
FOR /F %%i IN ('DIR /B /O:D %FILE1% %FILE2%') DO SET NEWEST=%%i
ECHO %NEWEST%

but I cannot fit what I have together
Please can someone help

Thankyou


See More: Comparing two file dates in CMD

Report •


✔ Best Answer
June 13, 2012 at 10:52:17
Wow, I just tested it and there were several problems. That's what I get for trying to write it in my head.

Try this:

@echo off & setlocal enabledelayedexpansion
set #=0
set $=mail.log
set _=F
:_loop
set /a #+=1
pushd c:\TEST%#%
for /f "tokens=*" %%i in (
'2^>nul dir /b/od !$! !_!%#%.txt'
) do (
if {%%i}=={mail.log} (
call run.bat
popd
goto :_loop
) else (
timeout /t 20 /nobreak
popd
goto :_loop
)
)

Run it from the root of the C: drive.

Tony



#1
June 13, 2012 at 07:50:43
Untested. Probably will need some tweaking

@echo off & setlocal enabledelayedexpansion
set #=0
set $=mail.log
set _=F
:_loop
set /a #+=1
pushd c:\TEST!#!
for /f "tokens=*" %%i in (
'2>nul dir /b/od !$! !_%#%!.txt'
) do (
if {%%i}=={F!#!} (
popd
call run.bat
goto :_loop
) else (
popd
timeout /t /nobreak 20
goto :_loop
)
)

Tony


Report •

#2
June 13, 2012 at 09:56:46
Thanks for responding, I appreciate it.

I had a look and thought I would make it as simple as I could and took out the looping


@echo off
set F= F1.txt
set M=Mail.log
pushd C:\TEST!#!
for /f %%i IN ('dir /b/od !#! F M') DO
(if {%%i}=={F} (
popd
call run.bat
) else (
popd
call OtherRun.bat
)
)
end

I've tried this with a couple of variations but can't get it to work. Without the looping, am I close?

I have two conditions to run bat files but I only put in the OtherRun.bat (that has dir > text file) to see if the command worked.

Many thanks


Report •

#3
June 13, 2012 at 10:32:38
You took too much out. !#! is a variable so you need to add
set #=1
back in.

CMD.EXE uses spaces literally, so

set F= F1.txt
is different from
set F=F1.txt

Comment out the @echo off so you can see the full output to troubleshoot what is going on.

I will test my code and see what is going on with it.

Tony


Report •

Related Solutions

#4
June 13, 2012 at 10:46:09
Thanks Tony, I'll have another look and get back to you

Report •

#5
June 13, 2012 at 10:52:17
✔ Best Answer
Wow, I just tested it and there were several problems. That's what I get for trying to write it in my head.

Try this:

@echo off & setlocal enabledelayedexpansion
set #=0
set $=mail.log
set _=F
:_loop
set /a #+=1
pushd c:\TEST%#%
for /f "tokens=*" %%i in (
'2^>nul dir /b/od !$! !_!%#%.txt'
) do (
if {%%i}=={mail.log} (
call run.bat
popd
goto :_loop
) else (
timeout /t 20 /nobreak
popd
goto :_loop
)
)

Run it from the root of the C: drive.

Tony


Report •

#6
June 13, 2012 at 22:39:12
:: ===== script starts here ===============
::
:: CIDR.bat Thu 14-06-2012 9:46:00.57
@echo off & setLocal enableDELAYedeXpansioN

for /f "tokens=* delims= " %%a in ('dir/b/od ONE TWO') do (
set newer=%%a
)
call :!newer!
goto :eof

:ONE
echo blabla
goto :eof

:TWO
echo more bla
goto :eof
::====== script ends here =================


=====================================
Life is too important to be taken seriously.

M2


Report •

#7
June 14, 2012 at 05:34:33
I really appreciate all the help I'm receiving here as I'd be really stuck.

I think, however, I need to simplify it as my understanding of Dos is fairly bare; I'm not getting the results I want, if any.

If I create a similar scenario by starting with removing the loop. I'll explain the following purely because it's easier for me:)

___________________________________________
-If the calling Batch file is placed in a folder in Path
C:\test
-I have the two files - BroadCast.xlxs and Mail.log - located in C:\test\BroadCastReport\
-I want to compare the Date part of each
--If Mail.log Date is less then run C:\test\BroadCastReport\Run.Bat and wait 20 seconds
--If Mail.log Date is the same (or not less) then finish
___________________________________________

I think if I can grasp this and have the calling bat placed in C:\test then I can work out the looping

Again, many thanks for anymore help I receive


Report •

#8
June 14, 2012 at 13:40:21
Ok I managed to get this to work (touch wood). I was having issues with the order of things in the code. for example, if I switched the txt and log file names around (keeping the modified date as I'm only changing the file names) it would not work.

Here is what I ended up with but I don't think it's bullet proof. Thanks for the help

@echo off & setlocal enabledelayedexpansion
set #=F1.txt
set $=MailSame.log
pushd c:\TEST1
for /f "tokens=*" %%i in (
'2^>nul dir /b/od !$! !#!'
) do (
if {%%i}=={F1.txt} (
echo sub1 same
timeout /t 2 /nobreak
goto sub2
popd
) else (
echo sub1 not same
goto sub2
popd
)
)

:sub2
echo starting sub2
set #=F1.txt
set $=MailSame.log
pushd c:\TEST3
for /f "tokens=*" %%i in (
'2^>nul dir /b/od !$! !#!'
) do (
if {%%i}=={F1.txt} (
echo sub2 same
timeout /t 2 /nobreak
goto sub3
popd
) else (
echo sub2 not same
goto sub3
popd
)
)


Report •

Ask Question