Solved

DOS Batch file: Script to display message based on file date

Posted on 2010-09-01
8
721 Views
Last Modified: 2012-08-13
Using a DOS batch file (.bat) I am looking to display a message on the screen (or execute a program of my choosing) if a file, located on a network share, was modified earlier that today's date.

Here is what's going on:  
Every night, at midnight, a server outputs data to a text file in a network share; for example \\server\share\output.txt
Other users access this file to retrieve needed data
To ensure they get the data they need, I have to ensure the text file was created at, or after, midnight.
I do this by looking at the file's Modified Date.  If it's the same as today's date, I know it has current data.

In very loose programming syntax, I would like it to do this:
If \\server\share\output.txt = todays date
    do nothing
Else
    display "Error" on screen (or execute a program of my choosing)

Thank you.
0
Comment
Question by:dandickens
  • 4
  • 2
  • 2
8 Comments
 
LVL 52

Expert Comment

by:Bill Prew
ID: 33577844
Can you tell me what the two commands below display if you execute them at a command prompt:

ECHO %DATE%

DIR \\SERVER\SHARE\OUTPUT.TXT

(where the last one is the actual file involved)

~bp
0
 
LVL 2

Author Comment

by:dandickens
ID: 33577930
ECHO %DATE%:
Wed 09/01/2010

DIR \\SERVER\SHARE\OUTPUT.TXT:
 Volume in drive \\SERVER\SHARE is New Volume
 Volume Serial Number is C840-B990

 Directory of \\SERVER|\SHARE

09/01/2010  12:07 AM        27,369,774 OUTPUT.txt
               1 File(s)     27,369,774 bytes
               0 Dir(s)  56,456,040,448 bytes free

0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 33578172
Okay, I think this BAT file should do the job.

~bp
@echo off
set CurrentDate=%DATE:~0,10%
for %%A in ("\\server\share\filename.txt") do for /F "tokens=1" %%B in ("%~tA") do set FileDate=%%B
if "%FileDate%" NEQ "%CurrentDate%" (
  echo File was not updated today!
  REM Run any program you desire here
)

Open in new window

0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 33578184
Whoops, hold on, need to adjust that, to this.

~bp
@echo off
set CurrentDate=%DATE:~-10%
for %%A in ("\\server\share\filename.txt") do for /F "tokens=1" %%B in ("%~tA") do set FileDate=%%B
if "%FileDate%" NEQ "%CurrentDate%" (
  echo File was not updated today!
  REM Run any program you desire here
)

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 33578327
You could also just do:
@echo off
dir \\server\share\yourfile.txt | find "%date:~-10%"  || (
  echo Is NOT todays date
) || (
  echo It is OK
)

i.e. use Find on the output of dir command of the relevant file and if it finds a match with the current date then run the bit after the &&

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33578360
Sorry Bill, hadn't refreshed there.  My second || should be && - I think I have that the right way around, i.e. if find does not find the date then it is an error and the || bit is run... been a long day and was only passing!
0
 
LVL 2

Author Comment

by:dandickens
ID: 33579089
Thanks to each of you for assisting me with this.   billprew:

running that batch produces this output -

The following usage of the path operator in batch-parameter
substitution is invalid: %~tA") do set FileDate=%%B

Any thoughts?
0
 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 33579800
Sorry, small typo there, change:

%~tA

to

%%~tA

~bp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now