Link to home
Start Free TrialLog in
Avatar of DarchVader
DarchVaderFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How do I find if a file does not exists in a particular directory with today's date

Hi,

I have a requirement to be able to run a batch job depending on the existence of a file in a particular directory, i.e. if there is no file in the given directory with todays date, then run the required batch job.

Many thanks,

Avatar of Neil Russell
Neil Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

Easiest cheat to this..... IF You dont need the previuos days file....

Set up a scheduled task to run at Midnight that deletes the named file Then you can just test for the existance of the file at all, can ONLY be todays date now. You can just use...

IF EXIST c:\mydir\myfile.ext (
do stuff here
)
ELSE
(
file did not exist
)
If you label your files in the form MM/DD/YY, you can use the following:

SET curdate = %DATE:~4%
IF EXIST C:\mydir\%curdate% (
do something
) ELSE (
do something else
)

Open in new window

Avatar of Steve Knight
a) offer more than 50 points!
B) can you tell us the format of the filename or how we know it is todays.... Is it the modified date or the filename?
C) a good cheat is to take the newest file in the dir that matches a description, e.g.

@echo off
For /f "delims=" %%a in ('dir /b /od *.txt') do set filename=%%a

remove one of each % in the %%a bits to do from command line.

Steve
How about...

   DIR /A:-D *.* | FIND "%DATE%" >NUL || RequiredBatchJob


You can change *.* to a specific file e.g., '*.EXT' or even 'FILE.EXT'.

If file(s) of *.* (or '*.EXT' or 'FILE.EXT') with today's date is not present then the batch file 'RequiredBatchJob' will execute.

Avatar of Bill Prew
Bill Prew

@paultomasi

I was thinking something similar but waiting to see more info from the poster.  I think you'd want to use %DATE:~-10% though, since on some systems (like mine) %Date will have the day name first, and then a space, followed by the date.  Whereas DIR just has the 10 character date.

~bp
Can also test for changed files today...

   DIR /A:-D /T:W *.* | FIND "%DATE%" >NUL || RequiredBatchJob

(See further details above comment).
billprew

The asker may need to tweak this if his %DATE% and DIR shows his date differently....



DarchVader

   DIR /A:-D /T:W "C:\Directory\*.*" | FIND "%DATE%" >NUL || "C:\Batch\RequiredBatchJob"

Will run the batch file 'RequiredBatchJob.bat' (or '.cmd') in the 'C:\Batch\' directory if there are no files with today's date in the directory 'C:\Directory\'.

(For further details see above comment)
Avatar of DarchVader

ASKER

Paul,

Thanks for the reply. This may help, this is a listing of the directory I am checking. I have run the first part of your suggestion to see if I get any results. Apologies if I have missed the point but I would expect to get something back from this as there are 2 files with creation date of today.


Volume Serial Number is 7038-607D

 Directory of G:\Backup

10/13/2011  05:22 AM         7,045,120 C-3655877639-20111013-00
10/14/2011  05:21 AM         7,045,120 C-3655877639-20111014-00
10/13/2011  05:21 AM     4,738,801,664 D-2CMOVJ79_1_1
10/14/2011  05:21 AM     4,739,964,928 D-2EMP27J7_1_1
              15 File(s) 33,180,041,216 bytes
               0 Dir(s)  38,395,588,608 bytes free

G:\Backup>dir /A:-D /T:W | FIND "%DATE%"

G:\Backup>
Paul,

I see why as this is my DATE ev

G:\Backup>echo %DATE%
Fri 10/14/2011

Regards.
Paul,

I aded the first line and your command now works

set DATEA=%date:~4,2%/%date:~7,2%/%date:~10,4%
dir /A:-D /T:W | FIND "%DATEA%"

Thanks for your help
I've requested that this question be closed as follows:

Accepted answer: 0 points for DarchVader's comment http:/Q_27395182.html#36967249
Assisted answer: 50 points for paultomasi's comment http:/Q_27395182.html#36963805

for the following reason:

I completed the solution with information I found elsewhere on the web
Please try:

   dir /a:-d /t:w | find "%date:~-10%"


Notice the MINUS (-10)...


If that last line works then you can shorten it to:

   dir /a-d /tw | find "%date:~-10%"



If you don't want the output then you can tack a '>nul' onto the end like this:

   dir /a-d /tw | find "%date:~-10%" >nul


You can then test the result (of both the above) like this:

  if %errorlevel% equ 1 (
      echo No 'today' file were found
   )

Or alternatively:

  if %errorlevel% equ 0 (
      echo 'Today' file(s) found
   )


Also, you can combine them like this:

  if %errorlevel equ 1 (
      echo No 'today' files found
   ) else (
      echo 'Today' file(s) found
   )

Pretty nifty, eh?

But remember, these IF-conditional statements need to be done immediately after the DIR command.


   
ASKER CERTIFIED SOLUTION
Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
36967274
36967300
36967321



DarchVader

Would you consider changing your mind and ACCEPTING either of the following (especially the last one)?

   http:#36967274
   http:#36967300
   http:#36967321

Thank you.


PS. I accidentally pressed the ENTER key in the incompleted comment above and have subsequently requested help from a moderator.
and bump him some decent points!
Thank you dragon-it.

I didn't like to ask myself even though the amount of points awarded does not match the QUALITY and the VOLUME of EXPERT assistance given.

Now that you've asked, I do hope DarchVader notices this imbalance when reconsidering my proposal.
DarchVader

Oops! Apologies. I meant the first one...

   http:#36967274
   http:#36967300
   http:#36967321
Did suggest in http:#36963288

Anyways .... more important things to do like puzzel quesitons and developing "games".... oh yeah and paid work maybe.
Paul,

Apologies, didn't see anywhere to increase the points awarded, how do I do this.
No need to increase points.....

Thank you
Steve

   >> "Did suggest in http:#36963288"

Surely you're NOT suggesting http:#36963288 provides a solution to this question?

Sure, %%a would be set to the last file's name in date order but let's pause for a moment's thought here...

If %%a returns nothing then we can confirm no 'today' file(s) exist.

However, if %%a does returns a filename, then there is no certainty it's datestamp is 'today's'.

I hope that makes sense to you.

:)

Paid work?.... Oh yeah, that!

PS. I thought you might have picked up on the number sequence thing as there's been a recent breakthrough on that. Check it out!
Nope Paul, that I said he needed to add more points before I bothered any more with it!!

Have looked at it but got 3-4 jobs on the go at the moment and wife to take out to lunch shortly..
@Steve,

I vote for lunch with the bride as a top priority!

(especially if it means one less MS-DOS question you beat me to :-) )

~bp

The lunch sound very tempting... The bride sound even more tempting...

You'd be daft to let a lady down over DOS-based program and 50 points!

Now, if we were talking sophistication here... perhaps a GUI front-end, a mainframe and 10,000 points, then I'd probably send my good lady a text... and ask her to kindly order ahead and turn up with a bunch of roses!

And remember, anticipation can sometimes make the heart grow fonder...

:)


.....says a lonely single man!