[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1297
  • Last Modified:

batch script find specific file newer than X date

Hi All.  After tinkering with various scripts out on the internet I can't find a workable solution to my problem.
I have a directory named d:\data\
I'm need to search for 2 specific files in d:\data\ let's say a.log and b.log
I want to know if a.log or b.log exists and if either is newer than Jan 1, 2011.
I'm going to take some action if one or both of those files exist with the date newer than the one I mentioned

I am looking for the file on many machines, various OS (2003, 2008, 2008 R2).
I'd like to keep it a batch script if possible. I'm okay with vbscript but this code is just a piece of more scripting so I'd like to keep it all batch, not have just one piece in VB. Thanks!
0
ZabagaR
Asked:
ZabagaR
  • 9
  • 8
1 Solution
 
Bill PrewCommented:
Yes, this can be done in BAT.  You can access the last change date of a file, and then break apart the month, day and year, and then do some checking on that.  I will try and work up an example shortly...

Will the date format vary between machines?

~bp
0
 
knightEknightCommented:
@echo off
 setlocal enabledelayedexpansion

REM set filespec=%*
set filespec=D:\data\*.log

 for /f "delims=" %%F in ('dir/a-d/b/od %filespec%') do (
   set filedate=%%~tF
   set filedate=!filedate:~6,4!!filedate:~0,2!!filedate:~3,2!

@echo %%F - !filedate!

   if !filedate! GTR 20110101 goto TakeAction
)

@echo Nothing to do!

exit/b

:TakeAction

@echo Jackpot!

exit/b
0
 
ZabagaRAuthor Commented:
knightEknight, how can I change your script for the filespec variable. You set filespec=d:\data\*.log
That's because I simplified my question. The extensions of the 2 files are not both .log.
One actually ends in .trn and the other filename ends in .ckp

Is it possible to check subdirectories for the 2 files? In case one is in d:\data\temp\ for instance? At this point I'd probably accept a solution without the subdirectory check, but I do have to do that...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
knightEknightCommented:
Don't bother with the filespec variable at all, instead change the "for" statement like this:

cd/d D:\data
for /f "delims=" %%F in ('dir/a-d/b/od  filename1.trn  filename2.ckp') do (


(obviously use your actual file names)
0
 
ZabagaRAuthor Commented:
I guess I can just run your code twice...once for each file I'm looking for.
Then to also search subdirs, I'd change your line above by adding /S:

for /f "delims=" %%F in ('dir /s /a-d/b/od %filespec%') do (
0
 
ZabagaRAuthor Commented:
Oh okay. I was just typing and didn't get your first response!
0
 
knightEknightCommented:
To recurse the sub-directories, do this:

cd/d D:\data
for /f "delims=" %%F in ('dir/a-d/b/od/s  filename1.trn  filename2.ckp') do (
0
 
knightEknightCommented:
Also, if you need to act on each file that matches your criteria, change the "goto" statement to a "call", and pass the for-loop variable as an argument:

if !filedate! GTR 20110101 call TakeAction  "%%F"
0
 
knightEknightCommented:
... then you can use the filename at the TakeAction label:

:TakeAction

@echo Jackpot filename is: %1
0
 
ZabagaRAuthor Commented:
I didn't get this change to work: for /f "delims=" %%F in ('dir/a-d/b/od  filename1.trn  filename2.ckp') do (

But - I don't mind.  Because your original post works if I just add the /S for subdirs.

When I used your modified code, it would only ever find 1 of the 2 files but never both. Weird.

I just run your original code twice. Once for my first file, again for my second.  Yeah, that's kind of heavy handed but it works! I'm still tweaking/testing....
0
 
knightEknightCommented:
That is strange.  If you reverse the order of the filenames in the "for" statement, does it only find the first one?

btw - you can remove the /od as it doesn't really do anything in this context.  (or replace it with /s).
0
 
ZabagaRAuthor Commented:
In the for statement, where it would only find 1 of the 2 files, I couldn't find a pattern. It didn't always take the first one listed. For my testing purposes I gave it 2 files in a single folder to find: bradley.txt and imaging.zip.
It kept finding only bradley.txt whether I put it first in the search or second. So I removed imaging.zip and replaced it with msizap.exe. Now it would only find msizap.exe no matter where in the search it was. I though maybe it was what came up first with an alphabetic listing but that test killed the theory.

I'm fine with what I have right now. You've been a HUGE help! Thanks.
0
 
knightEknightCommented:
Ah, that's because of the GOTO ... bad on me for that one.   Change it to CALL instead and it will work on both (see details in my previous post on this).
0
 
ZabagaRAuthor Commented:
No problem. Hey - what can I do if one of those files doesn't exist? Because one or both may not.  That produces problems with the script. If one of those files don't exist this line comes up true: if !filedate! GTR 20110101 goto TakeAction
So it takes action when it shouldn't. I'll try a few things out on my own in the meantime...maybe I'll find the workaround!

I thought this may work but it didn't:
if exist %%F if !filedate! GTR 20020101 goto found
0
 
knightEknightCommented:
Let's add (append) a zero to both sides:

   if !filedate!0 GTR 201101010

That way if the filedate variable is empty, it will be treated like a zero.
0
 
ZabagaRAuthor Commented:
Okay, that works. Thanks again.
I could always check for the file first, right after setting filespec too!
dir /s %filespec% > nul
if not %errorlevel%==0 goto next

So I just skip over the whole thing if no file is found.
0
 
ZabagaRAuthor Commented:
Finished the rest of the script and all works well. One of the temp files made during my script run is called knight.txt.....anybody who sees the code will be wondering why I named a temp file that....ha.
0
 
knightEknightCommented:
I'm honored, and glad it all works!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now