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

后端 未结 1 679
后悔当初
后悔当初 2020-12-21 06:20

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 subf

相关标签:
1条回答
  • 2020-12-21 06:47

    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 讨论(0)
提交回复
热议问题