【问题标题】:Monitor a folder for a new file using powershell but ignore new folders使用 powershell 监视文件夹中的新文件,但忽略新文件夹
【发布时间】:2018-11-06 04:19:20
【问题描述】:

我制作了一个小脚本,用于监视我的音乐文件夹中的新文件并将它们移动到相应的艺术家文件夹中。如果该文件夹不存在,它会创建它,然后将 mp3/flac/m4a 文件移动到新创建的文件夹中。唯一的问题是,当它创建这个新文件夹时,它也会在脚本中触发一个 ObjectEvent,所以它开始表现得很愚蠢。是否可以更改脚本,使其仅在添加新文件时触发事件,而不是新文件夹?

###Load taglib
$TagLib = "F:\Music-10-12-2016\Scripts\taglib-sharp.dll"

#Load it into Powershell
[system.reflection.assembly]::loadfile($TagLib)

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "F:\Music-10-12-2016\Leak"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = { Write-Host "Recieved Event"
            $path = $Event.SourceEventArgs.FullPath
            $changeType = $Event.SourceEventArgs.ChangeType
            $logline = "$(Get-Date), $changeType, $path"
            $media = [TagLib.File]::Create(($path))
            $artists = [string]$media.Tag.Artists
            Write-Host $artists
            Write-Host $logline
            Write-Host $path
            Add-content "F:\Music-10-12-2016\Scripts\log.txt" -value $logline
            Add-content "F:\Music-10-12-2016\Scripts\log.txt" -value $artists
            if(!(Test-Path -Path "F:\Music-10-12-2016\Leak\$artists" )){
                New-Item -ItemType directory -Path "F:\Music-10-12-2016\Leak\$artists"
                Write-Host "New folder created"
                Start-Sleep -s 2
            }
            Move-Item -Path "$path" -Destination "F:\Music-10-12-2016\Leak\$artists"
            Add-content "F:\Music-10-12-2016\Scripts\log.txt" -value "moved"
          }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {
sleep 2 
Write-Host "Monitoring"
}

【问题讨论】:

  • 查看.NotifyFilter 属性。我认为这就是你需要的,但还没有测试过。从这里得到... FileSystemWatcher 类 (System.IO) | Microsoft Docs — docs.microsoft.com/en-us/dotnet/api/…
  • 我看到了 .Filter 属性,它似乎只适用于一种类型的文件。我似乎无法将其过滤到 .flac/.mp3/.m4a/.wav 等。NotifyFilter 属性看起来很有趣
  • 只需重写操作以检查文件夹...

标签: powershell


【解决方案1】:

您可以重写操作以检查文件或文件夹。 if((Get-ChildItem $Event.SourceEventArgs.FullPath -File)) 仅在项目为文件时获取项目。

###Load taglib
$TagLib = "F:\Music-10-12-2016\Scripts\taglib-sharp.dll"

#Load it into Powershell
[system.reflection.assembly]::loadfile($TagLib)

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = new-object System.IO.FileSystemWatcher
$watcher.Path = "F:\Music-10-12-2016\Leak"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = { Write-Host "Recieved Event"
            $path = $Event.SourceEventArgs.FullPath
            if((Get-ChildItem $Event.SourceEventArgs.FullPath -File)){
                write-host "FILE"
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                $media = [TagLib.File]::Create(($path))
                $artists = [string]$media.Tag.Artists
                Write-Host $artists
                Write-Host $logline
                Write-Host $path
                Add-content "F:\Music-10-12-2016\Scripts\log.txt" -value $logline
                Add-content "F:\Music-10-12-2016\Scripts\log.txt" -value $artists
                if(!(Test-Path -Path "F:\Music-10-12-2016\Leak\$artists" )){
                    New-Item -ItemType directory -Path "F:\Music-10-12-2016\Leak\$artists"
                    Write-Host "New folder created"
                    Start-Sleep -s 2
                }
                Move-Item -Path "$path" -Destination "F:\Music-10-12-2016\Leak\$artists"
                Add-content "F:\Music-10-12-2016\Scripts\log.txt" -value "moved"
            }
        }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {
    sleep 2 
    Write-Host "Monitoring"
}

【讨论】:

  • 试过了。似乎没有工作。将脚本编辑为 Write-Host $path ,您会看到,当它创建文件夹时,它收到了 Event 并且仍然认为它是一个文件 gyazo.com/93b1205126986486a15329cefa32aba4
  • @ark 您是否正在使用新的 powershell 实例,因为我没有看到您取消注册该事件。如果您使用相同的 powershell 实例,那么它只会注册越来越多的事件,直到您关闭 powershell。
【解决方案2】:

ArcSet 非常接近,他为我指出了正确的方向。这是我使用的:

###Load taglib
$TagLib = "F:\Music-10-12-2016\Scripts\taglib-sharp.dll"

#Load it into Powershell

[system.reflection.assembly]::loadfile($TagLib)

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "F:\Music-10-12-2016\Leak"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = { Write-Host "Received Event"
            $path = $Event.SourceEventArgs.FullPath
            Write-Host $path
            if((Test-Path -Path $path -PathType Leaf)){ 
                Write-Host "FILE"
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                $media = [TagLib.File]::Create(($path))
                $artists = [string]$media.Tag.Artists
                Write-Host $artists
                Write-Host $logline
                Write-Host $path
                Add-content "F:\Music-10-12-2016\Scripts\log.txt" -value $logline
                Add-content "F:\Music-10-12-2016\Scripts\log.txt" -value $artists
                if(!(Test-Path -Path "F:\Music-10-12-2016\Leak\$artists" )){
                    New-Item -ItemType directory -Path "F:\Music-10-12-2016\Leak\$artists"
                    Write-Host "New folder created"
                    Start-Sleep -s 2
                }
                Move-Item -Path "$path" -Destination "F:\Music-10-12-2016\Leak\$artists"
                Add-content "F:\Music-10-12-2016\Scripts\log.txt" -value "moved"
                }
          }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
Register-ObjectEvent $watcher "Created" -Action $action

#Register-ObjectEvent $watcher "Changed" -Action $action
#Register-ObjectEvent $watcher "Deleted" -Action $action
#Register-ObjectEvent $watcher "Renamed" -Action $action
while ($true) {
sleep 2 
Write-Host "Monitoring"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 2018-08-31
    • 2019-10-04
    • 2014-05-06
    • 2012-10-05
    相关资源
    最近更新 更多