David W.
David W.

Reputation: 107040

Finding File based upon time in batch file

We want to figure out if a file on a Windows system has been modified since the last time our script ran. In Unix we can do this:

 if [ "$file" -nt "$touch" ]
 then
      echo "$file has been changed!"
      touch "$touch"
 fi

I need to do the same thing with a Windows batch script. No, I can't use Perl, Python, Ruby, Cygwin, or PowerShell. They're not available on this particular server. In fact, I have no access to this server. I'm suppose to create a batch script and have someone else install it and hope it works.

Yes, work just gets funner and funner each day!

Upvotes: 2

Views: 205

Answers (1)

dbenham
dbenham

Reputation: 130819

This should work as long as the batch script and the test file are in the same directory and the batch script is not read only. It can be adapted to fire off if any one of many files has been updated by appending multiple file names to the file variable with space as a delimiter.

@echo off
setlocal
set file="test.txt"

pushd %~dp0
for /f %%F in ('dir /b /o-d "%~nx0" %file%') do (
  if "%%~F" neq "%~nx0" (
    echo %file% has changed
    rem Below is weird syntax for updating timestamp of this batch script
    copy "%~nx0"+,, >nul
  )
  goto :continue
)
:continue
popd

Upvotes: 2

Related Questions