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