TechsRx Logo Top Slice TechsRx Computer Services - Business Stong :: People Friendly
Follow TechsRx on Twitter      TechsRx Facebook Fan Page
TechsRx Logo Bottom Slice Local: 505-897-2996 Toll Free 1.877.TECHS1X (832-4719)
  TechsRx HOME     SUPPORT FORUM        Friday, July 30, 2010 

Windows batch file to check if a file has changed and if it has do something

I needed a Windows batch file to check if a file has changed and if it has do something.
What I was trying to accomplish is I have a file written to the c:\ drive by a web application as users interact with it.
It rewrites the file every couple of minutes or so. When the file is rewritten I want it to display the content to the window or log it into a file.
I didn’t want to display it or log it unless the info has actually changed.

Here was some advice I got

You can check if a file has changed quite easily using the archive bit:

dir /b /aa whatever.txt

Does that report your file as changed?

then do

attrib -a whatever.txt
and
dir /b /aa whatever.txt
will not find it until it has changed again

OR you could use xcopy.

1. if it uses xcopy /M then it will copy if the file has changed and reset the
archive bit for you.
2. Use /d and it will copy the file if newer.

For the xcopy way

@echo off
:start
del "%temp%\file.txt"
xcopy /M C:\file.txt "%temp%"
if exist "%temp%\file.txt" echo File has changed
ping 127.0.0.1 -n 5
goto start

or

@echo off
:start
xcopy /Y /D C:\file.txt "%temp%" | find /i "1 File(s) copied" && echo File has changed
ping 127.0.0.1 -n 5
goto start

To get rid of the ” 1 files copied” message on the screen with the last one:

@echo off
:start
xcopy /Y /D C:\file.txt "%temp%" | find /i "1 File(s) copied" >NUL && echo File has changed
ping 127.0.0.1 -n 5
goto start

How big is this file though?

@echo off
dir /aa /b c:\file.txt >NUL 2>&1 && echo Has changed & attrib -a c:\file.txt

Here was some more advice given

:loop
dir /aa c:\file.txt >nul 2>&1
if %errorlevel% equ 0 (
attrib -a c:\file.txt
::File has changed. Do something
)

ping -n 5 localhost >nul 2>&1
goto loop

Popularity: 31% [?]

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • Add to favorites
  • MySpace
  • StumbleUpon
  • Twitter

Leave a Reply

By submitting a comment here you grant TechsRx a perpetual license to reproduce your words and name/web site in attribution.

transparent pixel spacer
BUY NOW   |    LEARN NOW   |    FIX NOW   |    Web Services & Domains   |    4 Color Printing
transparent pixel spacer