Question

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
Was it helpful?

Solution

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

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top