Solved

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

Posted on 2010-09-01
8
716 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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
Comment Utility
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
Comment Utility
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 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
Sorry, small typo there, change:

%~tA

to

%%~tA

~bp
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Enhanced For loop vs ForEach Loop 4 54
strCopies  challenge 17 73
move one pixel 4 41
C# DataGridView_RowsAdded event not firing 1 36
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

14 Experts available now in Live!

Get 1:1 Help Now