79575451

Date: 2025-04-15 14:48:43
Score: 0.5
Natty:
Report link

The OP's own answer turned out to be such a useful little script that I thought I'd share a few teeks I made for myself, after stealing it! :)

    REM Written by Michael Hutter / February 2024
    REM Optional variables & English translations added by @RussBroom / April 2025
    REM Usage: DetectFileChange.bat C:\FileToMonitor.txt C:\Temp\CopyOfMonitoredFile.txt
    REM https://stackoverflow.com/questions/77986309/how-to-detect-a-file-change-using-windows-batch
::
:: 
TITLE %~nx0
@echo off
CLS
:: 
    :: **************************************** USER SETTINGS ****************************************
        :: Set these variables to pre-define file to monitor, or leave empty to use CMD line.
    set "OriginalFile="
    set "CopyFile="
    :: **************************************** USER SETTINGS ****************************************
:: 
    If Not "%OriginalFile%"=="" If Not "%CopyFile%"=="" goto :START
::  
    if "%1"=="" goto Syntax
    if "%2"=="" goto Syntax
    if not exist "%1" goto Syntax
::  
    set "OriginalFile=%1"
    set "CopyFile=%2"
    ::  
    :START
    rem Check if the copy exists
    if not exist "%CopyFile%" (
        echo Copy of file does not exist. Create a new copy...
        copy "%OriginalFile%" "%CopyFile%"
    )
:: 
    :Restart
    rem Reading out the timestamps
    for %%A in ("%OriginalFile%") do set "originalTimeStamp=%%~tA"
    for %%B in ("%CopyFile%") do set "copyTimeStamp=%%~tB"
:: 
    rem Compare the timestamps
    if "%originalTimeStamp%" neq "%copyTimeStamp%" (
        echo The file has been changed!
        call :TheFileWasChanged %OriginalFile% %CopyFile% "%originalTimeStamp%" "%copyTimeStamp%" TempAlertFile
        copy /Y "%OriginalFile%" "%CopyFile%"
        del %TempAlertFile%
    ) else (
        echo The file has not been changed. %originalTimeStamp% even %copyTimeStamp%
    )
    rem Uncomment the following two lines if you want to run this file in a loop
    REM timeout /t 30 > nul
    REM goto Restart
    echo End
    exit /b
:: 
:: 
    :Syntax
    echo. & echo    Detect file changes (by file timestamp)
    echo    Syntax:
    echo        %0 ^<FileToMonitor^> ^<CopyOfMonitoredFile^>
    echo        %0 C:\FileToMonitor.txt C:\Temp\CopyOfMonitoredFile.txt
    echo. & echo    Or edit theis script to define set variables in User Settings
    echo. & echo. & echo. & Pause
    exit /b
:: 
:: 
    :TheFileWasChanged
    setlocal enableDelayedExpansion
    set sChangeAlertFile=C:\Temp\ChangeAlert.txt
    set sFileNameNow=%1
    set sFileNameBefore=%2
    set sTimestampNow=%3
    set sTimestampBefore=%4
    echo The file !sFileNameNow! has changed: (!sTimestampBefore! to !sTimestampNow!) > !sChangeAlertFile!
    echo. >> !sChangeAlertFile!
    echo New Content: >> !sChangeAlertFile!
    echo ============ >> !sChangeAlertFile!
    type !sFileNameNow! >> !sChangeAlertFile!
    for %%a in (1 2) do echo. >> !sChangeAlertFile!
    echo Old Content: >> !sChangeAlertFile!
    echo ============ >> !sChangeAlertFile!
    type !sFileNameBefore! >> !sChangeAlertFile!
    start notepad !sChangeAlertFile!
    Timeout /t 2 > nul
    (endlocal & set %5=%sChangeAlertFile%)
    goto :eof
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: RussBroom