Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2011-10-13
28
Medium Priority
?
229 Views
Last Modified: 2012-06-27
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,

0
Comment
Question by:DarchVader
  • 14
  • 5
  • 5
  • +3
28 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 36963208
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
)
0
 
LVL 1

Expert Comment

by:mc10
ID: 36963285
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

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36963288
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
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 11

Expert Comment

by:paultomasi
ID: 36963629
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.

0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36963645
@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
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36963693
Can also test for changed files today...

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

(See further details above comment).
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36963805
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)
0
 

Author Comment

by:DarchVader
ID: 36967188
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>
0
 

Author Comment

by:DarchVader
ID: 36967200
Paul,

I see why as this is my DATE ev

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

Regards.
0
 

Author Comment

by:DarchVader
ID: 36967249
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
0
 

Author Comment

by:DarchVader
ID: 36967342
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
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36967274
Please try:

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


Notice the MINUS (-10)...


0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36967300
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.


   
0
 
LVL 11

Accepted Solution

by:
paultomasi earned 400 total points
ID: 36967321
If you look at your statement:

   set DATEA=%date:~4,2%/%date:~7,2%/%date:~10,4%


Then, if you execute this command (your line) in DOS:

   echo %date:~4,2%/%date:~7,2%/%date:~10,4%

you should get the same as either:

   echo %date:~-10%

(which is what I used).... or,

   echo %date:~4%


Try them and see. If they ARE the same then you don't need to use the long complex line you put together yourself however, it does show you're understanding of how it works... You just need to learn some shortcuts...
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36967343
36967274
36967300
36967321



0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36967362
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.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36967367
and bump him some decent points!
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36967392
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.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36967402
DarchVader

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

   http:#36967274
   http:#36967300
   http:#36967321
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36967482
Did suggest in http:#36963288

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

Author Comment

by:DarchVader
ID: 36967492
Paul,

Apologies, didn't see anywhere to increase the points awarded, how do I do this.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36967502
No need to increase points.....

Thank you
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36967725
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!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36967767
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..
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36967775
@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

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36967875
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...

:)


0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36967883
.....says a lonely single man!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36967896
:-)
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question