使用批处理文件连续检查已修改的文件

时间:2016-04-07 15:29:06

标签: batch-file

我正在尝试创建一个批处理文件,用于持续检查文件的修改日期是否已更改。如果更改,则显示“文件已被修改”的消息框。我有一个简单的代码,但它在执行中显示一些错误..

@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

2 个答案:

答案 0 :(得分:3)

而不是文件时间,而是使用Archive属性(历史上用于确定文件是否需要归档,因为它已被更改;每次,OS都会写入文件,它也会重置此属性):

@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

答案 1 :(得分:1)

我的最终代码。感谢您的支持。

 @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