我有一个"ZIP“文件,当我们提取这个,我们有一个"EXE”文件在4-5子文件夹深度水平。
我想抓取"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)
} 发布于 2018-05-02 20:27:02
简单片段应该可以完成您的工作,
#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
发布于 2022-02-23 16:06:32
就目前情况而言,克林特的回答对我没有用,但是基于从ZIP档案中提取特定文件的一些东西确实起作用,有一个变体来针对一个特定命名的文件。将需要进一步调整以处理共享相同名称的多个文件。
代码:
# 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()https://stackoverflow.com/questions/50138600
复制相似问题