Search code examples
datebatch-filetimedayofweek

Batch Script For File Date/Time


I know there are similar questions but I have not been able to make any work. I need to check a particular file date and time against the current date and time. So far I have

 Set cdate=%date%
 Set filename="c:\myfile"
 If Not Exist %filename% GOTO CREATEFILE
 For %%f In(%filename%) DoSet filedatetime=%%~tf
 If %filedatetime:~0,-9%" == "%cdate% GOTO SHOFILE

My problem is that the cdate returned has the day of the week included in the date but the file date does not. Example cdate= Thur 1/01/2015. How can I get the cdate not to have the day of the week? Thanks


Solution

  •  For %%f In (%filename%) Do Set "filedatetime=%%~tf"
     If "%filedatetime:~0,-9%"=="%cdate:~4%" GOTO SHOFILE
    

    Note the required space after in and do

    The set "var=value" syntax ensures that any trailing spaces on the batch line are not included in the value assigned to var.

    if /i "%var%"=="value" performs a comparison on variables/values containing separators (eg spaces) The '/i' make the comparison case-insensitive if required.