Solved

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

Posted on 2010-09-01
8
745 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 55

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 55

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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 55

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 55

Accepted Solution

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

%~tA

to

%%~tA

~bp
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Simple Linear Regression

707 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