将新文件添加到文件夹时执行批处理文件

时间:2011-05-07 20:31:18

标签: batch-file

我正在使用下面从batch file to monitor additions to download folder获得的批处理文件示例。我想修改它,以便循环遍历所有子文件夹。

任何人对如何做到这一点都有任何想法?

@echo off
if not exist c:\OldDir.txt echo. > c:\OldDir.txt
dir /b "d:\My Folder" > c:\NewDir.txt
set equal=no
fc c:\OldDir.txt c:\NewDir.txt | find /i "no differences" > nul && set equal=yes
copy /y c:\Newdir.txt c:\OldDir.txt > nul
if %equal%==yes goto :eof
rem Your batch file lines go here

1 个答案:

答案 0 :(得分:4)

请....使它成为powershell?

http://archive.msdn.microsoft.com/PowerShellPack包含函数Start-FileSystemWatcher

请不要在任何系统上造成此批处理文件。


Powershell.com有样本

以下是来自this thread的示例:

$folder = '<full path to the folder to watch>'
$filter = '*.*'                             # <-- set this according to your requirements
$destination = '<full path to the destination folder>'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
 IncludeSubdirectories = $true              # <-- set this according to your requirements
 NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
 $path = $Event.SourceEventArgs.FullPath
 $name = $Event.SourceEventArgs.Name
 $changeType = $Event.SourceEventArgs.ChangeType
 $timeStamp = $Event.TimeGenerated
 Write-Host "The file '$name' was $changeType at $timeStamp"
 Move-Item $path -Destination $destination -Force -Verbose # Force will overwrite files with same name
}

最后,取消注册订阅:Unregister-Event -SourceIdentifier FileCreated