2

I need some help making a service application batch file.

In short, the application checks if there are any new files in a folder, if so it writes to a log file (Results.txt).

The problem is that if there are no "results" in the folder in 24 hours there is usually something wrong. (Either the service has crashed or there is a network problem.) So I need to write a batch script to check if the Results.txt file has been altered in the last 24 hours.

My plan is to have a batch file run every 24 hours that checks the md5sum of Results.txt to see if it has changed. However, I have no idea how to go about this. In pseudocode, it would look like this:

if not exist old.txt echo. > old.txt & fc "md5.txt" "old.txt"
%md5sum% Results.txt > md5.txt
set equal=no
if md5.txt==old.txt set equal=yes
if equal=no echo No results found in 24 hours >> log.txt
john_science
  • 6,325
  • 6
  • 43
  • 60

2 Answers2

0

How about going simple? No fancy MD5, just the output of the dir command! This works because the dir command output will be exactly the same no matter when it is run if nothing has changed in the directory it is checking.

For Help and More Options see:

dir /?
fc /?

Example:

@echo off
if not exist old_fingerprint.txt echo. > old_fingerprint.txt
:: List the directory information, recursive, last write timestamps, 4 digit date
dir /n /s /t:w /4 > new_fingerprint.txt
:: Check out fc /? for all of the comparison options.
fc new_fingerprint.txt old_fingerprint.txt
:: fc sets errorlevel to 0 when the files match and not 0 when the files do not match.
if %ErrorLevel% NEQ 0 echo.No results found in 24 hours>> log.txt

If you want to hide all the output of the batch script just throw in a > nul behind the fc line.

Update: In light of the new information provided.

So you just need to check the last write time stamp of the Results.txt file. As long as you do not write to the file when there has been no changes and update the time stamp.

Or even better yet, if all you care about is whether or not a folder has had any activity inside of it, just check that folder's 'last write' time stamp. If the time stamp is older than 24 hours you can process your no results found in 24 hours.

Use the /t:w option with the dir command and you can parse the time stamp that you need.

TLDR: KISS

Update 2:

Here is an example script showing how to get a pure breakdown of the directory information.

@echo off
setlocal

for /f "usebackq tokens=1,2,3,4,*" %%A in (`dir /a:d /l /n /t:w /4`) do (
    echo.
    echo.Date = %%A
    for /f "usebackq tokens=1,2,3 delims=/" %%X in ('%%A') do (
        echo.Month = %%X
        echo.Day = %%Y
        echo.Year = %%Z
    )
    echo.Time = %%B
    for /f "usebackq tokens=1,2 delims=:" %%X in ('%%B') do (
        echo.Hour = %%X
        echo.Minute = %%Y
    )
    echo.Meridiem = %%C
    echo.Type Raw = %%D
    for /f "usebackq tokens=1 delims=<>" %%X in ('%%D') do (
        echo.Type = %%X
    )
    echo.Name = %%E
    echo.
)

endlocal

Answer: Here is a script illustrating an easy way to get and compare the time stamps. Setup a scheduled task to run this script every 24 hours and if the time stamp has not changed since the last run, it will print out a message to the log file.

setlocal enabledelayedexpansion

for /f "usebackq tokens=1,2,3,4,*" %%A in (`dir /a:d /l /n /t:w /4`) do (
    if /i "%%~E"=="Folder" (
        if exist "LastTimeStamp.txt" find /c /i "%%A %%B %%C %%E" LastTimeStamp.txt > nul
        if !ErrorLevel! EQU 0 echo.The folder time stamp has not changed since last checked. >> log.txt
        echo.%%A %%B %%C %%E > LastTimeStamp.txt
    )
)

endlocal
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • Not sure if that is quite what I want. I need it to check the file Results.txt to see if that has changed in 24 hours rather than the directory. – Alex Powell Dec 14 '12 at 05:22
  • This is because files can be constantly added and removed from the folder. If the script discovers that there are no files at the start of the day and files are added and removed all day but the folder is empty by the next day then it will show that nothing changed in 24 hours. This is why I have one script to check every 5 minutes (which uses dir) and writes the time and date when there is a change made and need another script to check if this file has changed in 24 hours – Alex Powell Dec 14 '12 at 05:37
  • Thanks David, this seems perfect for what I want. I don't know how to parse this information and check it though. Just to clarify, I need to check the last modified time/date against the current time/date and if it's more than 24 hours difference I need it to echo into the log file. I played around with dir /t:w but I can't figure out how to select just the time and date for the folder I want or how to check it against the current date! – Alex Powell Dec 16 '12 at 23:39
  • @AlexPowell I have added a second update to provide an example of how to parse the information and a simple answer script utilizing the parsing. – David Ruhmann Dec 17 '12 at 02:30
  • Thank you! This is great. Thanks for showing how it works as well, very useful for future projects – Alex Powell Dec 17 '12 at 02:47
0

This will compare the files in your directory to the current date, if they are older than the current date then it will tell you which one's are new (not necessarily needed), if there are no new ones then it will log it.

If you run this with a scheduled task at the end of the day then this should work well.

cd yourdir
forfiles /d +%date% /s /c "cmd /c echo @file is new" 2>nul
if %errorlevel%==1 echo No results found in 24 hours >>log.txt
Bali C
  • 30,582
  • 35
  • 123
  • 152