0

I need to branch a batch files execution based on the datestamp of a file. Basically, there is a file that must be generated once per day, any time that day is fine.

pseudo code

if (last modified date stamp of c:\temp\file1.txt) equals (current date) then goto TaskSucceeded else goto TaskFailed :TaskFailed echo "Task Failed" exit /B :TaskSucceeded echo "Task Succeeded" exit /B

Any help would be appreciated.

getut
  • 11
  • 2

2 Answers2

1

This .bat waits for 1 min then checks the day, and then performs an action I got it from HERE

You can change the time to once every 24h and replace the CALL files with the ones you want to call

@ECHO OFF

:LOOP
ECHO Waiting for 1 minute...
PING -n 60 127.0.0.1>nul
IF %DATE:~0,3%==Mon CALL SomeOtherFile.cmd
IF %DATE:~0,3%==Tue CALL SomeOtherFile.cmd
IF %DATE:~0,3%==Wed CALL SomeOtherFile.cmd
IF %DATE:~0,3%==Thu CALL WootSomeOtherFile.cmd
IF %DATE:~0,3%==Fri CALL SomeOtherFile.cmd
IF %DATE:~0,3%==Sat ECHO Saturday...nothing to do.
IF %DATE:~0,3%==Sun ECHO Sunday...nothing to do.
GOTO LOOP

Read about the date function in .bat

Community
  • 1
  • 1
Jonas
  • 1,105
  • 1
  • 15
  • 21
  • I just noticed it took out some of my pseudo code. I actually need it to compare to TODAY in some (any) numerical date format. If filedate = today then do something, else do something else. if datestamp of file1 = todays date etc. – getut Apr 08 '16 at 12:39
0
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q36498808.txt"
FOR %%a IN ("%filename1%") DO ECHO %date%|FINDSTR /L "%%~ta" 2>NUL >nul
IF ERRORLEVEL 1 (ECHO failed) ELSE (ECHO succeeded)
SET "filename1=my.7z"
FOR %%a IN ("%filename1%") DO ECHO %date%|FINDSTR /L "%%~ta" 2>NUL >nul
IF ERRORLEVEL 1 (ECHO failed) ELSE (ECHO succeeded)
SET "filename1=missing.7z"
FOR %%a IN ("%filename1%") DO ECHO %date%|FINDSTR /L "%%~ta" 2>NUL >nul
IF ERRORLEVEL 1 (ECHO failed) ELSE (ECHO succeeded)

GOTO :EOF

Since you've not shown us the format in which you display dates - the date in a dir listing or the date in %date% then the above is simply a suggestion.

I simply used convenient filenames for testing. The routine works for me.

The crux of the method is that if %date% is Fri 08-04-2016 and the filetime is 08-04-2016 10:43am then findstr will find a match as if either the date-string or the time-string is supplied by the echo then findstr will set errorlevel 0 and will set errorlevel 1 or 2 if the date-string doesn't match or the file is missing.

You would need to examine the precise minutae of your date displays to determine whether it is reliable in your situation.

Magoo
  • 77,302
  • 8
  • 62
  • 84