Powershell 解压并重命名为 ZIP 文件名

问题描述 投票:0回答:3

我得到了一个应用程序,它可以生成一个 ZIP 文件,其中包含一个 HTML 文档。然而,提取的 HTML 文件的名称与 zip 文件的名称不同(由于一些我无法控制的奇怪原因)。

我需要提取 zip 文件并将提取的文件重命名为与 zip 文件名相同的名称,但是我不确定这会在我的脚本中插入什么位置,请提供一些帮助,脚本如下:

#Files Location
$ZipFilesPath = "C:\Test\"
#Unzip To Same Location
$UnzipPath = "C:\Test\"

$Shell = New-Object -com Shell.Application
$Location = $Shell.NameSpace($UnzipPath)
$ZipFiles = Get-Childitem $ZipFilesPath -Recurse -Include *.ZIP
$FileCounter = 1

#Clear Initilisation Vars from Console
clear

foreach ($ZipFile in $ZipFiles) {
    #Get The Base Filename without the Filepath
    $ZipFileActualName = [io.path]::GetFileNameWithoutExtension($ZipFile.FullName)

    write-host "File: "  $ZipFileActualName

    $ZipFolder = $Shell.NameSpace($ZipFile.fullname)
    $Location.Copyhere($ZipFolder.items(), 1040)

    #Move Along to Next File
    $FileCounter++
}

最终解决方案(下面添加了 Nick Cox 的部分)

#Location Of ZIP Files Defined Here
$ZipFilesPath = "C:\Test\";
#Unzip To Temp Folder Defined Here
$TempPath = "C:\Test\Temp"

#Create Our Temp File If It Doesnt Exist (Will Be Deleted Again)
If(!(test-path $TempPath))
{
      New-Item -ItemType Directory -Force -Path $TempPath
}

$Shell = New-Object -com Shell.Application
$Location = $Shell.NameSpace($TempPath)
$ZipFiles = Get-Childitem $ZipFilesPath -Recurse -Include *.ZIP
$FileCounter = 1

#Clear Initilisation Vars from Console
clear

foreach ($ZipFile in $ZipFiles) {
    #Get The Base Filename without the Filepath
    $ZipFileActualName = [io.path]::GetFileNameWithoutExtension($ZipFile.FullName)
    write-host "File: "  $ZipFileActualName
    $ZipFolder = $Shell.NameSpace($ZipFile.fullname)
    $Location.Copyhere($ZipFolder.items(), 1040)
    #Move Our Temp Files
    $HtmlFiles = Get-ChildItem $TempPath *.html
    $HtmlFiles |% {Move-Item  $_.Fullname "$UnzipPath/$ZipFileActualName.html"}
    #Move Along to Next File
    $FileCounter++
}

#Remove Our .ZIP Folders
Get-ChildItem -Path $ZipFilesPath -Include *.zip* -File -Recurse | foreach { $_.Delete()}

#Remove Our Temp Folder
Remove-Item –path $TempPath –recurse
powershell zip
3个回答
1
投票

为什么不提取到临时位置并从那里复制到目的地?

#Files Location
$ZipFilesPath = "C:\Test\"
#Unzip To Same Location
$UnzipPath = "C:\Test\"
$TempPath = "C:\Test\Temp"

$Shell = New-Object -com Shell.Application
$Location = $Shell.NameSpace($TempPath)
$ZipFiles = Get-Childitem $ZipFilesPath -Recurse -Include *.ZIP
$FileCounter = 1

#Clear Initialisation Vars from Console
clear

foreach ($ZipFile in $ZipFiles) {
    #Get The Base Filename without the Filepath
    $ZipFileActualName = [io.path]::GetFileNameWithoutExtension($ZipFile.FullName)

    write-host "File: "  $ZipFileActualName

    $ZipFolder = $Shell.NameSpace($ZipFile.fullname)
    $Location.Copyhere($ZipFolder.items(), 1040)

    $HtmlFiles = Get-ChildItem $TempPath *.html
    $HtmlFiles |% {Move-Item  $_.Fullname "$UnzipPath/$ZipFileActualName.html"}
    #Move Along to Next File
    $FileCounter++
}

顺便说一下,如果您使用的是 PowerShell >= 5.0,您可以使用 Expand-Archive 进行本地解压。


0
投票

首先,感谢大家为解决这个问题所做的努力。我对上面的代码进行了一些修改,使其更加用户友好和可重复。

<#
.Synopsis
Extract files from ZIP and rename files of the specified file extension to match the base name of the source ZIP file

.PARAMETER ZipFilesPath
Location of the ZIP files to extract

.PARAMETER FileExtension
File Extension of the extracted files to rename

.PARAMETER CleanUp
Remove the source ZIP files on completion, use with caution

.PARAMETER UnzipPath
Location to save the extracted and renamed files

.Notes

Name:       extract-zip.ps1
Author:     Steven French       
Version:    1.1
Changlog:

v0.1    Original base code from Soprono (https://stackoverflow.com/users/2657329/soprono) and Nick Cox (https://stackoverflow.com/users/56151/nick-cox): https://stackoverflow.com/questions/50268035/powershell-extract-and-rename-as-zip-filename

v1.0    Initial Public Release

v1.1    Converted variables to parameters
        
.EXAMPLE
.\extract-zip.ps1 -ZipFilesPath "C:\Users\johnsmith\Videos" -FileExtension mp4 -CleanUp -UnzipPath c:\unzip

.EXAMPLE
.\extract-zip.ps1 -ZipFilesPath "C:\downloads\LinuxISOs" -FileExtension ISO 

#>

Param(  
    [Parameter(Mandatory = $true)][string]$ZipFilesPath,
    [Parameter(Mandatory = $true)][string]$FileExtension,
    [Parameter(Mandatory = $false)][switch]$CleanUp,
    [Parameter(Mandatory = $false)][string]$UnzipPath = "$ZipFilesPath\Extracted"
)

#Unzip To Temp Folder Defined Here
$TempPath = "$ZipFilesPath\temp"

#Create Our Temp File If It Doesnt Exist (Will Be Deleted Again)
If (!(test-path $TempPath)) {
    New-Item -ItemType Directory -Force -Path $TempPath
}

#Create Our Unzip Path If It Doesnt Exist
If (!(test-path $UnzipPath)) {
    New-Item -ItemType Directory -Force -Path $UnzipPath
}

$Shell = New-Object -com Shell.Application
$Location = $Shell.NameSpace($TempPath)
$ZipFiles = Get-Childitem $ZipFilesPath -Recurse -Include *.ZIP
$FileCounter = 1

#Clear Initilisation Vars from Console
Clear-Host

foreach ($ZipFile in $ZipFiles) {
    #Get The Base Filename without the Filepath
    $ZipFileActualName = [io.path]::GetFileNameWithoutExtension($ZipFile.FullName)
    write-host "File: "  $ZipFileActualName
    $ZipFolder = $Shell.NameSpace($ZipFile.fullname)
    $Location.Copyhere($ZipFolder.items(), 1040)
    #Move Our Temp Files
    $ISOFiles = Get-ChildItem $TempPath *.$FileExtension
    $ISOFiles | ForEach-Object { Move-Item  $_.Fullname "$UnzipPath/$ZipFileActualName.$FileExtension" }
    #Move Along to Next File
    $FileCounter++
}

if ($CleanUp) {
    #Remove Our .ZIP Folders
    Get-ChildItem -Path $ZipFilesPath -Include *.zip* -File -Recurse | ForEach-Object { $_.Delete() }
}

#Remove Our Temp Folder
Remove-Item -path $TempPath -recurse

-1
投票
    foreach ($ZipFile in $ZipFiles) {
    #Get The Base Filename without the Filepath
    $ZipFileActualName = [io.path]::GetFileNameWithoutExtension($ZipFile.FullName)

    write-host "File: "  $ZipFileActualName

    $ZipFolder = $Shell.NameSpace($ZipFile.fullname)
    $Location.Copyhere($ZipFolder.items(), 1041)

    $HtmlFiles = Get-ChildItem $TempPath *.html
    $HtmlFiles |% {Move-Item  $_.Fullname "$UnzipPath/$ZipFileActualName.html"}
    #Move Along to Next File
    $FileCounter++
}
© www.soinside.com 2019 - 2024. All rights reserved.