我有一个应用程序,它生成一个带有HTML文档的ZIP文件。但是,提取的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
答案 0 :(得分: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进行本地解压缩。
答案 1 :(得分:-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++
}