0

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

  • See [this answer](http://stackoverflow.com/a/27714991/1683264) for a way to scrape MM/DD/YYYY from both `%date%` and `%%~tX`. You can also `set "cdate=%date:* =%"` – rojo Jan 02 '15 at 04:11

2 Answers2

1

Your cdate can be set like this:

SET cdate=%date:~4%

This has the following output:

echo %cdate%
01/01/2015
Andy
  • 49,085
  • 60
  • 166
  • 233
1
 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.

rojo
  • 24,000
  • 5
  • 55
  • 101
Magoo
  • 77,302
  • 8
  • 62
  • 84