我制作了这个小脚本,用于监视音乐文件夹中的新文件,并将它们移到相应的Artist文件夹中。如果该文件夹不存在,它将创建它,然后将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"
}
答案 0 :(得分:3)
您可以重写操作以检查文件还是文件夹。 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"
}
答案 1 :(得分:0)
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"
}