繁体   English   中英

PowerShell:如何仅将“.exe”文件从 ZIP 文件夹复制到另一个文件夹

[英]PowerShell: How to copy only ".exe" file from ZIP folder to another folder

我有一个“ZIP”文件,当我们提取它时,我们在 4-5 个子文件夹深度级别内有 1 个“EXE”文件。

我想获取那个“EXE”文件并复制到另一个文件夹中。 如何使用 PowerShell 做到这一点?

我在下面试过,但它会复制所有 ZIP 内容,

 $shell = New-Object -ComObject shell.application
 $zip = $shell.NameSpace("Source Path")
 foreach ($item in $zip.items()) {
   $shell.Namespace("Destination Path").CopyHere($item)
} 

简单的代码段就可以完成您的工作

#Sets the variable to the Source folder, recurse drills down to folders within

$Source = get-childitem "C:\Users" -recurse   #"C:\Users" an example

#Filters by extension .exe
$List = $Source | where {$_.extension -eq ".exe"}

#Copies all the items to the specified destination
$List | Copy-Item -Destination "C:\Scripts"   #"C:\Scripts" an example

上面的模块扫描C:\\ Users *中的每个.EXE文件,并将它们复制到C:\\ Scripts

就目前而言,克林特的回答对我不起作用,但基于Extract Specific Files from ZIP Archive的答案对我有用,但有一个针对特定命名文件的变体。 将需要进一步调整以处理共享相同名称的多个文件。

代码:

# Set source zip path, target output directory and file name filter

$ZipPath = 'C:\temp\Test.zip'
$OutDir = 'C:\temp'
$Filter = 'MyExe.exe'

# Load compression methods
Add-Type -AssemblyName System.IO.Compression.FileSystem

# Open zip file for reading
$Zip = [System.IO.Compression.ZipFile]::OpenRead($Path)

# Copy selected items to the target directory
$Zip.Entries | 
    Where-Object { $_.FullName -eq $Filter } | 
    ForEach-Object { 
        # Extract the selected items from the zip archive
        # and copy them to the out folder
        $FileName = $_.Name
        [System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, "$OutDir\$FileName", $true)
    }

# Close zip file
$Zip.Dispose()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM