Batch file check file get updated to today's date(System Date) -


i want create batch check if file have been modified today's date, did "bring in system's date , compare modified date, if match, trigger something. batch file works , displays 2 right dates, if statement saying date mismatch.

@echo off  /f "tokens=1,2,3,4 delims=. " %%i in ('date /t') set date=%%k%%j echo %date% pause   %%a in (d:\myfile.txt) set filedate=%%~ta set dateonly=%filedate:~0,10% echo %dateonly% pause    if dateonly==date ( echo date ok )   else ( cls echo     wrong )  pause 

there following problems:

  • do not use variable name date built-in variable containing current date (type set /? help);
  • the first for statement useless, because %date% available;
  • the strings dateonly , date compared literally in if statement, need state %dateonly%==%date% instead;
  • the else statement must in same line closing parenthesis of if body (type if /? help);

so try this:

@echo off  echo %date% pause  %%a in (d:\myfile.txt) set filedate=%%~ta set dateonly=%filedate:~0,10% echo %dateonly% pause  if %dateonly%==%date% ( echo date ok ) else ( echo     wrong ) pause 

note: regard dates in batch file locale-dependent.


Comments