Cleaner way to get date modified and compare to current date?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Cleaner way to get date modified and compare to current date?

#1 Post by SIMMS7400 » 14 Oct 2017 04:03

Hi Folks -

Here is my method to get the file's date modified and compare it to today's date. I was wondering if there is a cleaner way?

Code: Select all

:CHK_FILE

SET "CFLAG="

PUSHD "%LOCALEXPORTPATH%"

FOR /F "delims=" %%A IN ('DIR "%NAME2:~0,-25%*.csv" /B /OD /A-D') DO SET "NAME=%%A"
FOR %%A IN (%NAME%) DO SET "FDATE=%%~tA"
ECHO %FDATE%>temp.out
FOR /F "tokens=1 delims= " %%A IN ( temp.out ) DO SET "DMOD=%%A"

IF "%DMOD%"=="%date:~-10,2%/%date:~-7,2%/%date:~-4,4%" (
   SET "CFLAG=T"
) ELSE (
   ECHO %NAME2:~0,-14% not updated - will not transfer to AWS >>%LOGFILE%
DEL /F /Q "%NAME%"
)
IF EXIST "temp.out" DEL /F Q "temp.out"

POPD
GOTO :EOF

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Cleaner way to get date modified and compare to current date?

#2 Post by Compo » 14 Oct 2017 04:49

First things first… We have no idea of the content or layout of variables, %LOCALEXPORTPATH%, %NAME2%, %date% and %LOGFILE% nor of the content of the file temp.out.

In scripting there are tons of ways of performing essentially the same task, some script for efficiency, some for robustness, some for speed etc. That said, instead of expecting us to improve your code, how about you drill down it yourself and tell us which parts you feel require cleaning up and why.

You can go through the many pieces of code which have been written for you here and other related questions using the search function or third party engine. Then perhaps show us your progression by posting your updates in this topic, so it is clear that you are learning something instead of just taking others work.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Cleaner way to get date modified and compare to current date?

#3 Post by SIMMS7400 » 14 Oct 2017 04:52

Hi Compo -

My thoughts are instead of spooling the date modified to a text file, I can capture that in a for loop.

I will take a stab at it and report back.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Cleaner way to get date modified and compare to current date?

#4 Post by Squashman » 14 Oct 2017 10:07

I suggest you read the very last section of the help file for the FOR command.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Cleaner way to get date modified and compare to current date?

#5 Post by SIMMS7400 » 14 Oct 2017 18:45

Well - I was able to neaten it up quite a bit. Would there anything people do differently? I can't seem to get away with only 1 for loop.

Code: Select all

FOR /F "delims=" %%A IN ('DIR "%NAME2:~0,-25%*.csv" /B /OD /A-D') DO SET "NAME=%%A"
FOR /F "tokens=2" %%A IN ('WHERE /T "%NAME%"') DO SET "DMOD=%%A"

IF "%DMOD%"=="%date:~-10,2%/%date:~-7,2%/%date:~-4,4%" (
   SET "CFLAG=T"
) ELSE (
   ECHO %NAME2:~0,-14% not updated - will not transfer to AWS >>%LOGFILE%
DEL /F /Q "%NAME%"
)

Post Reply