简体   繁体   English

powershell,如何管理目录并将文件移动到另一个文件夹

[英]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.

相关问题 如何将文件移动到文件夹中,然后在Powershell中将文件夹压缩 - How to move files into a folder and then zip the folder in powershell Powershell 将子文件夹中的所有文件和子文件夹移动到当前目录 - Powershell move all files and subfolders in a child folder to CURRENT directory Powershell 移动到同一目录下的文件夹 - Powershell move to folder in the same directory Powershell将文本文件从一个文件夹移动到另一个文件夹 - Powershell to move text files from one folder to another 在Powershell中将X文件从一个文件夹移动到另一个文件夹 - Move X files from one folder to another in Powershell Powershell 无法仅将文件从一个目录移动到另一个目录? - Powershell Unable to move only files from a one dire to another directory? 如何使用PowerShell将具有多个扩展名的文件集移动到另一个目录? - How to move set of files having multiple extension to another directory using PowerShell? 如何使用 PowerShell 搜索特定文件名然后将该文件之后的所有文件移动到另一个文件夹 - How to search for a specific file name then move all files after that file to another folder using PowerShell 如果目录中存在文件,请将其移动powershell - If files exist in directory,move it powershell Powershell-将文件深移1个文件夹 - Powershell - Move files 1 folder deeper
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM