如何使用 windows 批处理程序获取刚刚修改的文件名

How to get the file name which just got modified using windows batch program

我正在尝试解决监视特定目录的问题。我的批处理程序监视被修改的文件并将它们记录在一个 txt 文件中。到目前为止,我已经编写了可以检测文件何时更改并可以打印时间戳的代码。

但是现在我还需要它来记录刚刚修改的文件名。谁能帮我解决这个问题。这是我到目前为止所做的:

@Echo Off
Set _Delay=10
Set _Monitor=C:\Users\MMurshed\Desktop\New folder (3)
Set _Base=%temp%\BaselineState.dir
Set _Chck=%temp%\ChkState.dir
Set _OS=6
Ver|Findstr /I /C:"Version 5">Nul
If %Errorlevel%==0 Set _OS=5 & Set /A _Delay=_Delay*1000
:_StartMon
Call :_SetBaseline "%_Base%" "%_Monitor%"
:_MonLoop
If %_OS%==5 (Ping 1.0.0.0 -n 1 -w %_Delay%>Nul) Else Timeout %_Delay%>Nul
Call :_SetBaseline "%_Chck%" "%_Monitor%"
FC /A /L "%_Base%" "%_Chck%">Nul
If %ErrorLevel%==0 Goto _MonLoop
::
:: Insert code to run when a change occurs
::
Echo.Change Detected
Echo.Timestamp = %date:~0,2%%date:~3,2%%date:~6,8%-%time:~0,2%%time:~3,2%%time:~6,2%


Goto :_StartMon
:::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutine
:::::::::::::::::::::::::::::::::::::::::::::::::::
:_SetBaseline
If Exist "%temp%\tempfmstate.dir" Del "%temp%\tempfmstate.dir"
For /F "Tokens=* Delims=" %%I In ('Dir /S "%~2"') Do (
Set _Last=%%I
>>"%temp%\tempfmstate.dir" Echo.%%I
)
>"%~1" Findstr /V /C:"%_Last%" "%temp%\tempfmstate.dir"
Goto :EOF

您可以使用存档属性来检查新的或更改的文件(这就是它应该用来做的)

Windows 每次创建或更改文件时都会设置此属性,因此只需向 Windows 询问 "changed files" :)

@echo off
rem create some files
echo hello>a.txt
echo hello>b.txt
echo hello>c.txt
call :check
echo marking them by settting the "Archive" attribute:
attrib -a *.txt
call :check
echo changing a file
echo world>>b.txt
call :check
attrib -a *.txt
goto :eof

:check
echo ========================
echo new or changed files:
dir /b /aa-d *.txt
echo ------------------------
echo unchanged files:
dir /b /a-a-d *.txt
echo ========================
goto :eof

从批处理文件中嵌入的 VBScript 代码访问 WMI 以监视目录更改并在此代码中执行您需要的操作(当前它会将信息打印到控制台):

@echo off
set "vbsfile=%temp%\dirwatch%random%.vbs"
>"%vbsfile%" (
    for /f "delims=: tokens=* eol=" %%a in ('findstr /b ":::" "%~dpnx0"') do echo.%%a
)
cscript //nologo "%vbsfile%"
del "%vbsfile%"
exit /b

:::intInterval = "10"
:::strDrive = "c:"
:::strFolder = replace("\Users\MMurshed\Desktop\New folder (3)", "\", "\")
:::strComputer = "."
:::
:::Set objWMIService = GetObject( "winmgmts:{impersonationLevel=impersonate}!\" &_
::: strComputer & "\root\cimv2" )
:::
:::Set events = objWMIService.ExecNotificationQuery( _
::: "Select * From __InstanceOperationEvent" _
::: & " Within " & intInterval _
::: & " Where Targetinstance Isa 'CIM_DataFile'" _
::: & " And TargetInstance.Drive='" & strDrive & "'"_
::: & " And TargetInstance.Path='" & strFolder & "'")
:::
:::Do
::: Set fileEvent = events.NextEvent()
::: Set fileInfo = fileEvent.TargetInstance
:::
::: Select Case fileEvent.Path_.Class
:::     Case "__InstanceCreationEvent"
:::         WScript.Echo "Created: " & fileInfo.Name
:::     Case "__InstanceDeletionEvent"
:::         WScript.Echo "Deleted: " & fileInfo.Name
:::     Case "__InstanceModificationEvent"
:::         Set prevFile = fileEvent.PreviousInstance
:::         For Each prop In fileInfo.Properties_
:::             If prop.Value <> prevFile.Properties_(prop.Name) Then
:::                 WScript.Echo "Changed:        " & fileInfo.Name
:::                 WScript.Echo "Property:       " & prop.Name
:::                 WScript.Echo "Previous value: " & prevFile.Properties_(prop.Name)
:::                 WScript.Echo "New value:      " & prop.Value
:::                 WScript.Echo
:::             End If
:::         Next
::: End Select
:::Loop

P.S。由于 WMI 服务的配额限制,子文件夹不受监控

代码摘自 WMI and File System Monitoring Uros Calakovic 的文章。