【问题标题】:Using Compress-Archive to compress multiple folders multithreadded with Powershell使用 Compress-Archive 压缩多个使用 Powershell 进行多线程处理的文件夹
【发布时间】:2021-12-07 19:14:55
【问题描述】:

每天我都需要创建以下档案

C:.
└───1.Jan
    ├───20000
    │       img1.bmp
    │       img2.bmp
    │       img3.bmp
    │
    ├───20001
    │       img1.bmp
    │       img2.bmp
    │       img3.bmp
    │
    ├───20002
    │       img1.bmp
    │       img2.bmp
    │       img3.bmp
    │
    ├───20003
    │       img1.bmp
    │       img2.bmp
    │       img3.bmp
    │
    ├───20004
    │       img1.bmp
    │       img2.bmp
    │       img3.bmp
    │
    ├───20005
    │        img1.bmp
    │        img2.bmp
    │        img3.bmp
    │
    └───Entered

我目前有一个脚本可以一次创建一个 zip 文件,但是有时我可以有 200 多个要压缩的文件夹,而且它们的大小不同,所以我想让这个多线程工作。

function Zip-Folders([String] $FolderPath) {
if ($FolderPath -eq '') {
    return
} 
else {
    $FolderNames = Get-ChildItem -Path $FolderPath -Name -Directory -Exclude Enter*
        foreach ($i in $FolderNames) {
            $TempPath = "$FolderPath\$i"
            $TempFileName = "$i Photos"
            if (-Not(Get-ChildItem -Path $TempPath | Where-Object {$_.Name -like '*.zip'})) {
                Write-Host "[$TempPath] has been compressed to [$TempFileName]."
                Compress-Archive -Path $tempPath\* -DestinationPath $tempPath\$TempFileName
            }
            Else {
                Write-Host "[$i] has already been compressed."
            }
        }
}
}

代码通过文件夹浏览器对话框询问文件夹。

如果有人可以帮助我编写代码或指出我可以找到相关信息的方向,我是 PowerShell 的初学者,但已经完成了一些编程。

如果需要任何其他信息,请告诉我。

【问题讨论】:

  • 如果你有能力安装模块,比如ThreadJob,这很容易做到。另一种选择是使用 Runspace,但这需要更多的编码。

标签: multithreading powershell jobs compress-archive


【解决方案1】:

这是您可以使用Runspace 执行此操作的方法,使用-Threshold 参数取决于您要同时压缩多少个文件夹。注意您的主机资源并小心使用:)

Runspace 代码上的所有积分都转到this answer

function Zip-Folders {
param(
    [ValidateScript({Test-Path $_ -PathType Container})]
    [String]$FolderPath,
    [int]$Threshold = 10
)
    begin
    {
        $RunspacePool = [runspacefactory]::CreateRunspacePool(1, $Threshold)
        $RunspacePool.Open()
        $subFolders = Get-ChildItem -Path $FolderPath -Directory -Exclude Enter*
    }
    
    process
    {
        $runspaces = foreach ($folder in $subFolders)
        {
            $PSInstance = [powershell]::Create().AddScript({
                param($thisFolder)

                $fileName = "{0} Photos.zip" -f $thisFolder.Name
                $absolutePath = $thisFolder.FullName
                $zipPath = Join-Path $absolutePath -ChildPath $fileName

                if(-not(Get-ChildItem -Path $absolutePath -Filter *.zip))
                {
                    Compress-Archive -Path $absolutePath\* -DestinationPath $zipPath
                    "[$absolutePath] has been compressed to [$zipPath]."

                    continue
                }

                "[$absolutePath] has already been compressed."

            }).AddParameter('thisFolder',$folder)

            $PSInstance.RunspacePool = $RunspacePool

            [pscustomobject]@{
                Instance = $PSInstance
                IAResult = $PSInstance.BeginInvoke()
            }
        }

        while($runspaces | Where-Object {-not $_.IAResult.IsCompleted})
        {
            Start-Sleep -Milliseconds 50
        }
            
        $Runspaces | ForEach-Object {
            $_.Instance.EndInvoke($_.IAResult)            
        }
    }

    end
    {
        $RunspacePool.Dispose()
    }
}

【讨论】:

    猜你喜欢
    • 2021-10-23
    • 2021-07-11
    • 2019-05-02
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 2018-12-11
    相关资源
    最近更新 更多