Click here for important information about

Decide action upon file age

Microsoft Windows xp professional w/serv...
April 22, 2010 at 02:37:16
Specs: Windows XP
I wish to create a batch file that compares 3 different files and decides the action upon which file is the oldest. Very simple, but not for me. I'm not expecting someone to write the file for me, but maybe someone can point me in a direction as for which commands to use?!

What I want to do is the following:

if "file1" is older than "file2" or "file3" goto backup1 else goto 2

if "file2" is older than "file3" goto backup2 else goto backup3

"execute program1"

"execute program2"

"execute program3"

See More: Decide action upon file age

April 22, 2010 at 11:35:08
are the files in different directories?
do they all have the same name?

Report •

April 23, 2010 at 05:18:55
@echo off

for /F %%f in ('dir file1 file2 file3 /B /O-D') do (
echo set oldestfile=%%f> c:\temp.cmd

call c:\temp.cmd
del c:\temp.cmd

echo oldestfile=%oldestfile%

Report •

May 5, 2010 at 12:35:24
Hi nbrane and tvc

Thank you for responding. I'm sorry I haven't responded before, but I've been busy trying to figure it out myself, and so I began (and is at lesson 12 in this batch file course:

Anyways. The files are in different directories and they have the same name. So with the knowledge I got now, I don't think that nbrane's script will do the job. At least not without some modification.

Suggestions are still of interest, since I still can't figure it out myself.


Report •

Related Solutions

May 5, 2010 at 14:58:41
In that case, I suggest you pick up VBScript.

Report •

May 5, 2010 at 16:19:22
I still haven't figured out what the task is.

Helping others achieve escape felicity


Report •

May 5, 2010 at 17:17:13
i would use xcopy /d to determine the date to avoid all the date-confusion. First, collect the filenames:
set fname=test
set c=0
for /f "tokens=*" %%a in ('dir /b /a-d %fname%') do (
set /a c+=1
set x!c!=%%a
:: now do the testing (only set up for 3)
set a=1
set b=2
call :test
if %z% equ 1 goto :13
set a=2
set b=3
call :test
if %z% equ 1 goto :TWO
goto :THREE
set a=1
set b=3
call :test
if %z%=1 goto :ONE
goto :THREE

xcopy /d /l /y !x%a%! !x%b%! | find "1 File" 2>nul
set z=%errorlevel%

echo %x1% is oldest
goto :eof
echo %x2% is oldest
goto :eof
echo %x3% is oldest

Report •

Ask Question