开发者

Execute batch file when a new file is added to a folder

开发者 https://www.devze.com 2023-03-03 11:32 出处:网络
I am using the batch file example below that I got from batch file to monitor additions to download folder. I would like to modify this so that it loops through all the subfolders.

I am using the batch file example below that I got from batch file to monitor additions to download folder. I would like to modify this so that it loops through all the subfolders.

Anyone have any ideas on how to do this?

@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
c开发者_如何学编程opy /y c:\Newdir.txt c:\OldDir.txt > nul
if %equal%==yes goto :eof
rem Your batch file lines go here


Please.... make it powershell?

http://archive.msdn.microsoft.com/PowerShellPack contains the function Start-FileSystemWatcher.

Please don't inflict this batch file on any system.


Powershell.com has samples

Here's a sample from 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
}

Eventually, unregister the subscription: Unregister-Event -SourceIdentifier FileCreated

0

精彩评论

暂无评论...
验证码 换一张
取 消