【问题标题】:Powershell copy some folders from a zip filePowershell 从 zip 文件中复制一些文件夹
【发布时间】:2025-12-26 05:35:12
【问题描述】:

我有一个 zip 文件夹,它包含 10-12 个不同名称的文件夹(在我有一些文件的文件夹中),我需要使用 Power Shell 获取文件夹并复制到名称包含“abc”或“xyz”的目的地脚本。

提前致谢, 中国V

【问题讨论】:

    标签: powershell


    【解决方案1】:

    试试这个,

    function Extract-Zip{
    param([string]$zipfilename, [string] $destination)
    
    if(test-path($zipfilename))
    {    
        $shellApplication = new-object -com shell.application
        $zipPackage = $shellApplication.NameSpace($zipfilename)
        $destinationFolder = $shellApplication.NameSpace($destination)
        $destinationFolder.CopyHere($zipPackage.Items())
    }
    

    }

    这应该将您的压缩文件解压缩到您选择的目的地。或者,您可以探索 PS 社区扩展 - http://pscx.codeplex.com

    【讨论】:

    • 我只需要复制名称包含“abc”或“xyz”的文件夹,而不是所有文件夹......这意味着我们需要编写 contains/like 语句来只获取那些文件夹..我试过但没有运气..你能帮我解决这个问题吗?在此先感谢,Chinna V
    • ######################################### ################# COMMENT : 搜索特定文件的 zip 文件并将它们提取到(临时)目录。 ################################################# ######### #ERROR REPORTING ALL Set-StrictMode -Version latest #------------------------------ ---- #STATIC VARIABLES #--------------------------------- $srcZipFilepath = “C:\Test \ZIPFILE.zip” $tempPath = “C:\Temp” $ABCDestination = “C:\Test123\ABC” $XYZDestination = “C:\Test123\XYZ” $PQRDestination = “C:\Test123\PQR”
    • #--------------------------------- #FUNCTION CopyFilesToFolders #--- ------------------------------- Function CopyFilesToFolders { Param([string]$tempPath) #Write-Host $tempPath + " 11" #循环遍历目标 $shell_app= New-Object -com shell.application $file = Get-ChildItem -Path $tempPath foreach($filist in $file) { #Write-Host $filist.Name + 中的每个文件夹副本21" if ($filist.Name -like "XYZ") {
    • if (!(Test-Path -path $XYZDestination )) { New-Item $XYZDestination -type directory } #Write-Host $filist.fullname + "- XYZ -" + $XYZDestination Move-项目 -Path $filist.fullname -Destination $XYZDestination } elseif($filist.Name -like "ABC") { if (!(Test-Path -path $ABCDestination)) { New-Item $ ABCDestination -type directory } #Write-Host $filist.fullname + "- Services -" + $ABCDestination Move-Item -Path $filist.fullname -Destination $ABCDestination }
    • else { if (!(Test-Path -path $PQRDestination)) { New-Item $PQRDestination -type directory } #Write-Host $filist.fullname + "- Application Location -" + $ PQRDestination Move-Item -Path $filist.fullname -Destination $PQRDestination } } #删除临时文件夹 ReMove-Item -Path $tempPath }