# Solved: batch to compare file modified date with current date



## dampyr (Nov 25, 2011)

hello to everybody

I need batch code to check if date is modified in three files and that date should be compared with current date (%date%).If so,do some job,otherwise quit (exit /b)

I found a bunch of solutions but what troubles me is date format.
On my machine it is dd.mm.yyyy

Thanks in advance.


----------



## Intel_Xtreme (Jan 7, 2012)

Depends on what your input program is.
Most of the time for C++, C+, C* and LUA it's:
local Clock = "os.date("%d.%m.%Y")" : "os.date("%s.%m.%H")"
In code that the date and timestamp is needed, you would write "..Clock..".
os.date("%d.%m.%Y")" is Date, Month, Year.
os.date("%s.%m.%H")" is Seconds, Minutes, Hours.

Hope this helps!


----------



## dampyr (Nov 25, 2011)

Simply DOS batch file.What really confuses me is,for example:

```
For /F "Skip=1 Delims=" %%I In ('dir /b /OD file1.txt file2.txt')
or:for /f "skip=5 tokens=1-5" %%i in ('dir /tc /a-d') do echo %%i %%m
```
What i need to type to get data in desired format?


----------



## Intel_Xtreme (Jan 7, 2012)

dampyr said:


> Simply DOS batch file.What really confuses me is,for example:
> 
> ```
> For /F "Skip=1 Delims=" %%I In ('dir /b /OD file1.txt file2.txt')
> ...


Using the simple DIRLIST (in capitals) command may help.
I haven't got much experience with cmd but I remember a couple of months ago I made a batch file that listed all the directories and files in the given folder- there's a function (think it might be /a or /e) that writes the date each file was modified- you could tell it to search the output text file and make it compare the dates.

BTW- That doesn't look like DOS code to me- it looks like LUA (LUA is one of the few programming languages which has "do" as a function.)


----------



## dampyr (Nov 25, 2011)

```
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
for /D %%Q IN (*.*) DO (
  set FILETIME=%%~tQ
  if "!FILETIME:~0,10!"=="%DATE:~4%" echo %%Q
)
```
It seems that i found what i need.Thanks everyone for help !


----------



## Squashman (Apr 4, 2003)

dampyr said:


> ```
> @echo off
> setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
> for /D %%Q IN (*.*) DO (
> ...


You said you wanted to match the date of files. This only works for Directories.
If you read the help for the FOR command.


> FOR /D %variable IN (set) DO command [command-parameters]
> 
> *If set contains wildcards, then specifies to match against directory
> names instead of file names.*


----------



## dampyr (Nov 25, 2011)

As always,Squashman was right,but finally,after some googling and try&errors i finally got working code:

```
@echo off
set currentDate=%date%
echo %date%>"C:\@IT\Izvestaji\Horeca & Traders to PDF\date.txt"
SET filename="C:\@IT\Izvestaji\Horeca & Traders to PDF\horeca.pdf"
FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
echo %filedatetime:~0, 10%>"C:\@IT\Izvestaji\Horeca & Traders to PDF\date1.txt"
fc "C:\@IT\Izvestaji\Horeca & Traders to PDF\date.txt" "C:\@IT\Izvestaji\Horeca & Traders to PDF\date1.txt" > nul
IF %ERRORLEVEL% NEQ 0 goto notsame else goto same
:same
echo Dates the same, do some code here
 
:notsame
echo Dates NOT the same, do some code here
```


----------



## Squashman (Apr 4, 2003)

Kind of over kill using those temporary files for the date. You should be able to hold dates in two variables and compare them just like you did in your initial script.


----------



## dampyr (Nov 25, 2011)

All right,I will do that-thanks.


----------

