首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell:如何仅将".exe“文件从ZIP文件夹复制到另一个文件夹

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

Stack Overflow用户
提问于 2018-05-02 15:44:53
回答 2查看 1.4K关注 0票数 1

我有一个"ZIP“文件,当我们提取这个,我们有一个"EXE”文件在4-5子文件夹深度水平。

我想抓取"EXE“文件并复制到另一个文件夹中。如何使用PowerShell实现它?

我试过了,但是它会复制所有的ZIP内容,

代码语言:javascript
复制
 $shell = New-Object -ComObject shell.application
 $zip = $shell.NameSpace("Source Path")
 foreach ($item in $zip.items()) {
   $shell.Namespace("Destination Path").CopyHere($item)
} 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-02 20:27:02

简单片段应该可以完成您的工作,

代码语言:javascript
复制
#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:\ .EXE *中的每个.EXE文件,并将它们复制到C:\Scripts

票数 1
EN

Stack Overflow用户

发布于 2022-02-23 16:06:32

就目前情况而言,克林特的回答对我没有用,但是基于从ZIP档案中提取特定文件的一些东西确实起作用,有一个变体来针对一个特定命名的文件。将需要进一步调整以处理共享相同名称的多个文件。

代码:

代码语言:javascript
复制
# 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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50138600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档