Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用powershell监视文件夹中的新文件,但忽略新文件夹_Powershell - Fatal编程技术网

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

使用powershell监视文件夹中的新文件,但忽略新文件夹,powershell,Powershell,我制作了一个小脚本,它监视我的音乐文件夹中的新文件,并将它们移动到相应的艺术家文件夹中。如果该文件夹不存在,它将创建该文件夹,然后将mp3/flac/m4a文件移动到新创建的文件夹中。唯一的问题是,当它创建这个新文件夹时,它也会在脚本中触发ObjectEvent,因此它开始表现出愚蠢的行为。是否可以更改脚本,使其仅在添加新文件而不是新文件夹时触发事件 ###Load taglib $TagLib = "F:\Music-10-12-2016\Scripts\taglib-sharp.dll"

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

您可以重写操作以检查文件或文件夹是否正确<代码>如果((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"
}

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"
}

查看
.NotifyFilter
属性。我认为这是你需要的,但还没有测试过。从这里得到的。。。FileSystemWatcher类(System.IO)| Microsoft文档-我看到了.Filter属性&它似乎只适用于一种类型的文件。似乎我无法将其过滤到.flac/.mp3/.m4a/.wav等。NotifyFilter属性看起来很有趣,但只需重写操作以检查文件夹…尝试了。似乎不起作用。编辑脚本以写入主机$path,您将看到,当它创建文件夹时,它收到了事件,并且仍然认为它是一个文件@ark。您正在使用新的powershell实例,因为我没有看到您注销事件。如果您使用同一个powershell实例,那么它只会注册越来越多的事件,直到您关闭powershell。