3

I have to check in an external directory if a single file (example.avi) is modified/create today and if the answer is yes start another batch or quit.

I've found several posts but I'm not be able to make this batch file from myself.

I hope someone can help me. Thank you in advance.

JRL
  • 76,767
  • 18
  • 98
  • 146
Todd
  • 45
  • 1
  • 6

3 Answers3

1

I've found this but does not work on my Win XP.

@echo off
for %%F in (C:\TEST\myfile.avi) do (for /F %%D in ("%%~tF") do (set mdate=%%D))
for /F "tokens=2" %%D in ('date/t') do set cdate=%%D

if "%cdate%"=="%mdate%" start myprogram.bat
Todd
  • 45
  • 1
  • 6
1

The script you found seems valid to me, if you start new lines at the correct position. You can add an echo statement before the if statement to see if cdate and mdate have a correct value. And check if myprogram.bat exist in the directory. Here is a modified version which shows this. Can you try this, and post the output if it does not work?

@echo off 
for %%F in (C:\TEST\myfile.avi) do (for /F %%D in ("%%~tF") do (set mdate=%%D)) 
for /F "tokens=2" %%D in ('date/t') do set cdate=%%D
echo cdate="%cdate%"  mdate="%mdate%" current dir=%cd%
if "%cdate%"=="%mdate%" going to start myprogram.bat
if "%cdate%"=="%mdate%" start myprogram.bat
pause

edit Here is a version which works independent of the regional settings. It is based on this solution.

@echo off 
reg copy "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f > NUL
reg add "HKCU\Control Panel\International" /v sShortDate /d "yyMMdd" /f > NUL
for %%F in (C:\TEST\myfile.avi) do (for /F %%D in ("%%~tF") do (set mdate=%%D)) 
for /F "tokens=1" %%D in ("%date%") do set cdate=%%D
echo cdate="%cdate%"  mdate="%mdate%" current dir=%cd% date="%date%"
reg copy "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f > NUL
if "%cdate%"=="%mdate%" start myprogram.bat

It first makes a backup of the shortdate format in the registry. Then it replaces it with yyMMdd. Now it looks for the modification date of the file and the current date. before comparing the dates, it restores the shortdate format.

Community
  • 1
  • 1
wimh
  • 15,072
  • 6
  • 47
  • 98
  • the output is: cdate="" mdate="" current dir=C:\ and myprogram.bat starts also if the file is old – Todd Aug 01 '10 at 18:18
  • probably you have different regional settings then I have. can you enter `echo "%date%"` in a command prompt, and post the result here? – wimh Aug 01 '10 at 20:09
  • Ok, on my pc I have the day of the week in front of the date. One fix you need to make is changing `"tokens=2"` in the third line to `"tokens=1"`. But that does not explain why getting the file date does not work. Can you also enter `for %F in (C:\TEST\myfile.avi) do echo "%~tF"` in a commandprompt and show the output? In the mean time I had added a solution which should always work, but I am not sure now. – wimh Aug 01 '10 at 20:56
  • the result: echo "31/07/2010 15.32" "31/07/2010 15.32" – Todd Aug 01 '10 at 21:09
  • the solution that makes a backup of the shortdate format in the registry now works on my system... Thanks so much for the assistance. – Todd Aug 01 '10 at 21:23
  • I've changed "tokens=2" to "tokens=1" and it seems work also the other solution. – Todd Aug 01 '10 at 21:31
  • Beware that if you change the date format this way, it may affect other running processes during that short period of time. – RealHowTo Aug 02 '10 at 03:26
  • Ok, I will use the first solution where I've changed "tokens=2" to "tokens=1". – Todd Aug 02 '10 at 09:14
0
DIR %1 | FIND /I "%1" > ~ISMODIF.TMP
ECHO.>> ~ISMODIF.TMP
TYPE ~ISMODIF.TMP | TIME | FIND /I "%1" > ~ISMODIF.BAT
ECHO SET CHKDATE=%%4> ENTER.BAT
CALL ~ISMODIF.BAT
DIR ~ISMODIF.BAT | FIND /I "~ISMODIF.BAT" > ~ISMODIF.TMP
ECHO.>> ~ISMODIF.TMP
TYPE ~ISMODIF.TMP | TIME | FIND /I "~ISMODIF.BAT" > ~ISMODIF.BAT
ECHO SET NOWDATE=%%4> ENTER.BAT
CALL ~ISMODIF.BAT
IF "%NOWDATE%"=="%CHKDATE%" ECHO %1 was created or modified today

From http://www.robvanderwoude.com/bht.php.

JRL
  • 76,767
  • 18
  • 98
  • 146
  • Thank You, but unfortunately I'm not a programmer and I don't know where to put the filename to check and then start another program. – Todd Jul 30 '10 at 16:06