[英]powershell, how to mointor a directory and move files over to another folder
I am using powershell 3. need to monitor a folder, if there are any image files, move them over to antoher folder. 我正在使用Powershell3。需要监视一个文件夹,如果有任何图像文件,请将它们移到另一个文件夹中。
here's my code, i test it, its not working, couldn't figure out what need to be fixed. 这是我的代码,我对其进行了测试,它无法正常工作,无法找出需要解决的问题。
#<BEGIN_SCRIPT>#
#<Set Path to be monitored>#
$searchPath = "F:\download\temp"
$torrentFolderPath = "Z:\"
#<Set Watch routine>#
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $searchPath
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
$created = Register-ObjectEvent $watcher "Created" -Action {
Copy-Item -Path $searchPath -Filter *.jpg -Destination $torrentFolderPath –Recurse
}
#<END_SCRIPT>#
UPDATE: 更新:
I got it partially working. 我得到它的部分工作。 still have one issue left.
还有一个问题。 lets start with an empty folder.
让我们从一个空文件夹开始。 I download an image (1.jpg) to the folder, nothing moved to Z: drive.
我将图像(1.jpg)下载到该文件夹,没有任何内容移动到Z:驱动器。 then I download another image (2.jpg) to the folder.
然后将另一张图片(2.jpg)下载到该文件夹中。 1.jpg will be moved to Z: drive.
1.jpg将被移动到Z:驱动器。 seems like the newly created one never get moved over.
似乎新创建的一个永远都不会移动。
$folder = "F:\\download\\temp"
$dest = "Z:\\"
$filter = "*.jpg"
$fsw = new-object System.IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubDirectories=$false
NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
Move-Item -Path F:\download\temp\*.jpg Z:\
}
You have not registered the NotifyFilter. 您尚未注册NotifyFilter。 That is why your code is not working.
这就是为什么您的代码无法正常工作的原因。
here is a sample which registers the NotifyFilter and prints the file details which was created 这是一个注册NotifyFilter并打印创建的文件详细信息的示例
$folder = "c:\\temp"
$filter = "*.txt"
$fsw = new-object System.IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubDirectories=$false
NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceVentArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host $path
Write-Host $name
Write-Host $changeType
Write-Host $timeStamp
}
Event action scripts run in a separate scope that only has access to global variables, so depending on your implementation you have have issues trying to use those variables in the action script. 事件操作脚本在只能访问全局变量的单独范围内运行,因此,根据您的实现,在尝试在操作脚本中使用这些变量时会遇到问题。 One way around without resorting to declaring global variables (bad mojo!) is to use an expandable string to create a script block with the variables expanded before you register the event:
一种无需诉诸声明全局变量的方法(坏的mojo!)是使用可扩展字符串创建一个脚本块,并在注册事件之前将变量扩展:
$ActionScript =
[Scriptblock]::Create("Copy-Item -Path $searchPath -Filter *.jpg -Destination $torrentFolderPath –Recurse")
$created = Register-ObjectEvent $watcher "Created" -Action $ActionScript
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.