powershell - 使用 powershell 监视文件夹中的新文件,但忽略新文件夹

标签 powershell

我制作了这个小脚本,用于监视我的音乐文件夹中的新文件并将它们移动到相应的艺术家文件夹中。如果该文件夹不存在,则会创建该文件夹,然后将 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"
}

最佳答案

您可以重写该操作来检查文件或文件夹。 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"
}

关于powershell - 使用 powershell 监视文件夹中的新文件,但忽略新文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53141260/

相关文章:

PowerShell,按需从互联网自动加载功能

powershell - 使用 PowerShell start-process 和 invoke-command 远程压缩文件

powershell - 获取进程与总内存使用情况

sql-server - 使用 PowerShell 和 SMO 恢复数据库时显示进度

windows - Powershell 设置项属性 : How does one pass a variable to the -value parameter?

powershell - 签署 powershell 脚本?

java - 将 Powershell 触发的 java 程序的输出写入标准输出

powershell - 从 PowerShell 调用 CreateProcess

mysql - 导入文件时出现错误 "1366 Incorrect integer value: ' 1'"

windows - 如何将具有特定扩展名集的目录和所有子目录中的所有文件复制到另一个目录中?