1.

Solve : datestamp help?

Answer»

hi all - I need a batch file to search in a directory. If all the files have the current date stamp then execute command1 if files are not current then execute command2.
thank you.This can be done via Perl or a number of other languages which have abilities to compare STRINGS, but I cant see doing this in raw batch. Quote from: DaveLembke on September 21, 2010, 10:10:00 PM

This can be done via Perl or a number of other languages which have abilities to compare strings, but I cant see doing this in raw batch.

It is a commonplace everyday task for batch. It can easily be done... using FOR and the ~t variable modifier you can get the "date" (created or last modified?) of each file, and it is trivially easy to get today's date.

However we do not know the OP's local date format or the format returned on his system by ~t

The output of this script will TELL us those things.

Code: [Select]echo off
echo local date format [%date%]
echo.>temp$$$
for /f %%A in ("temp$$$") do echo file date  format [%%~tA]
del temp$$$
pause


For example, I get this

Code: [Select]local date format [22/09/2010]
file date  format [22/09/2010 07:46 AM]
But I am worried about this in the question

Quote
I need a batch file to search in a directory. If all the files have the current date stamp then execute command1 if files are not current then execute command2.
thank you.

All the files? What if 99 do and 1 does not? Likewise for the converse situation.

these files will ALWAYS have the same date. I guess we can look in one particular file to check for the date.
thanks Quote from: x3g on September 22, 2010, 07:58:01 AM
these files will always have the same date. I guess we can look in one particular file to check for the date.
thanks

So did you run my script?
your script works good but it is missing the logic I need.
Quote from: x3g on September 22, 2010, 09:44:19 AM
your script works good but it is missing the logic I need.


I asked you to run the script so I could see your local date format. Then I can make a script for you with the logic that you need.
ah here it is
local date format [Wed 09/22/2010]
file date  format [09/22/2010 11:14 AM]

thank you. Code: [Select]echo off
setlocal enabledelayedexpansion

REM Of course you UNDERSTAND you need to edit this
set foldername="S:\Test\Batch\After 03-07-2010\test-file-date"

set pdate=%date%
set today=%pdate:~4,10%

REM get file date of latest file in folder
for /f "delims=" %%A in ( ' dir /b /a-d /od %foldername% ' ) do (
set fdate=%%~tA
set fdate=!fdate:~0,10!
)

if "%fdate%"=="%today%" (
goto yes
) else (
goto no
)

:yes
echo YES!!!
rem Code to execute if file date is today
goto next

:no
rem Code to execute if file date is not today
echo NO!!!

:next
REM whatever happens next...

pause





this looks very promising. I'll let you know my feedback once I've tested.
Thank you.
the YES condition works great but the NO logic only works for files that are older than 1 day. If the latest files are from yesterday it will still run the YES logic. Quote from: x3g on September 22, 2010, 01:13:29 PM
the YES condition works great but the NO logic only works for files that are older than 1 day. If the latest files are from yesterday it will still run the YES logic.

are you quite sure about this? Because the test would be

if "09/21/2010"=="09/22/2010"

which should fail and therefore the else CLAUSE gets triggered.

Could you insert these diagnostic lines shown in red?


echo off
setlocal enabledelayedexpansion

REM Of course you understand you need to edit this
set foldername="S:\Test\Batch\After 03-07-2010\test-file-date"

set pdate=%date%
set today=%pdate:~4,10%

REM get file date of latest file in folder
for /f "delims=" %%A in ( ' dir /b /a-d /od %foldername% ' ) do (
        set fdate=%%~tA
        set fdate=!fdate:~0,10!
        )




echo today is %today%
echo fdate is %fdate%
echo comparing [%fdate%] with [%today%]


if "%fdate%"=="%today%" (
        goto yes
) else (
        goto no
        )

:yes
echo YES!!!
rem Code to execute if file date is today
goto next

:no
rem Code to execute if file date is not today
echo NO!!!

:next
REM whatever happens next...

pause


even though the files are dated 9/21/2010 fdate still shows 9/22

today is 09/22/2010
fdate is 09/22/2010
comparing [09/22/2010] with [09/22/2010]
YES
Press any key to continue . . .

are you quite sure there is no file dated 09/22/2010 in that folder, including hidden files?
I replaced the 9/21 files with some 9/14 & 9/20 files and get this

today is 09/22/2010
fdate is ~0,10
comparing [~0,10] with [09/22/2010]
NO
Press any key to continue . . .


Discussion

No Comment Found