问题
I am trying to create a batch file which is to continuously check the modified date of a file is changed or not. And if it is changed, then display a message box that "File has been modified". i have a simple code and but it showing some error in the execution..
@echo off
setlocal
set FileName=F:\test\tester.txt
set FileTime=0
:loop
for %%X in (%FileName%) do (
if %FileTime% NEQ %%~tX (
echo file modified
) else (
echo no change )
set FileTime=%%~tX
)
ping -n 10 localhost >nul 2>nul
goto :loop
pause
回答1:
Instead of the file time, rather use the Archive attribute (historically used to determined if a file needs to be archived because it was changed; Every time, the OS does a write to the file, it also resets this attribute):
@echo off
set filename=tester.txt
:loop
for %%i in (%filename%) do (
echo %%~ai|find "a" >nul && (
echo %filename% was changed %time%
attrib -a %%i
) || (
rem echo no change
)
)
timeout 1 >nul
goto :loop
回答2:
My final Code. Thanks for your supports.
@echo off
setlocal enableextensions disabledelayedexpansion
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do
set "dt=%%a"
set "td.YY=%dt:~2,2%"
set "td.YYYY=%dt:~0,4%"
set "td.MM=%dt:~4,2%"
set "td.DD=%dt:~6,2%"
set "filename=%td.MM%%td.DD%%td.YY%.txt
:loop
for %%i in (%filename%) do (
echo %%~ai|find "a" >nul && (
cscript MessageBox.vbs "Today's Schedule Changed. Modified Time: %time%. Please Check AVScheduler ON or OFF."
attrib -a %%i
) || (
rem echo no change
)
)
timeout 1 >nul
goto :loop
来源:https://stackoverflow.com/questions/36480736/continuously-check-a-file-modified-or-not-using-batch-file