问题
Iv got an application that produces a ZIP file with a HTML document inside of it. However the extracted HTML file is not named the same as the zip file (for some strange reason that I have no control over).
I need to extract the zip file and rename the extracted file the same as the zip file name however I am unsure where this would slot in on my script some help would be appreciated please, script below:
#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++
}
Final Solution (With Added Piece From Nick Cox below)
#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
回答1:
Why not extract to a temp location and copy from there to the destination?
#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++
}
By the way, if you're using PowerShell >= 5.0 you can unzip natively with Expand-Archive.
回答2:
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++
}
来源:https://stackoverflow.com/questions/50268035/powershell-extract-and-rename-as-zip-filename