?
Solved

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

Posted on 2010-09-01
8
Medium Priority
?
748 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 56

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 56

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
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!

 
LVL 56

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
 
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 56

Accepted Solution

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

%~tA

to

%%~tA

~bp
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

777 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