Computer Hope
Microsoft => Microsoft DOS => Topic started by: x3g on September 21, 2010, 05:16:52 PM
-
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.
-
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.
@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
local date format [22/09/2010]
file date format [22/09/2010 07:46 AM]
But I am worried about this in the question
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
-
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.
-
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.
-
@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.
-
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 . . .
-
Are you sure, as you showed before, that there is a leading zero in the file date?
What does this show?
@echo off
setlocal enabledelayedexpansion
set foldername="Your folder"
REM get file date of all files in folder
for /f "delims=" %%A in ( ' dir /b /a-d /od %foldername% ' ) do (
set fdate=%%~tA
echo !fdate!
)
pause
-
no hidden files. No file dated today.
-
Are you sure, as you showed before, that there is a leading zero in the file date?
What does this show?
@echo off
setlocal enabledelayedexpansion
set foldername="Your folder"
REM get file date of all files in folder
for /f "delims=" %%A in ( ' dir /b /a-d /od %foldername% ' ) do (
set fdate=%%~tA
echo !fdate!
)
pause
-
windows does not display the leading 0 but DOS does.
-
so what does my test script show?
-
ECHO is off.
09/22/2010 09:46 AM
09/22/2010 01:11 PM
Press any key to continue . . .
that's weird cause these files are dated 9/21 if I do a DIR
C:\test\1>dir
Volume in drive C has no label.
Volume Serial Number is E40F-A8C4
Directory of C:\test\1
09/21/2010 05:04 PM <DIR> .
09/21/2010 05:04 PM <DIR> ..
09/21/2010 05:05 PM 359 CurrDate.bat
09/21/2010 05:05 PM 17 notepad.bat
09/21/2010 05:02 PM 716 test.txt
3 File(s) 1,092 bytes
2 Dir(s) 116,920,819,712 bytes free
C:\test\1>
-
Salmon,
I think I've found the culprit. I made a copy of a file dated 9/21 and updated it in another folder but somehow the original file which is dated 9/21 is still linked to the copy and the copied file is dated 9/22. Even though they are in 2 separate locations the script still picks up the date of the copy.
I created a whole new folder and copied 2 files from 9/21 and the script works like a charm.
thank you very much for your help. You're a hero.
-
possibly the script is picking up the creation date and DIR is picking up the modified date i.e.when they were moved.
try adding the /tC or /tA or /tW switches (only one at a time) both in the script and with DIR at the prompt
eg DIR /tC
or else...
1. when did you last do a disk check?
2. Is this a network folder?
-
Our posts crossed... glad it's OK now... I'm here if it goes wrong...