如何将一个文件夹中的多个文件压缩到一个压缩文件夹中



我尝试通过Powershell脚本自动执行重复方案:

我有一个压缩 (.zip( 文件夹,其中包含多个文件 (.XML(。

我的方案是:

  • 解压缩此文件夹位于:C:Users*Downloads
  • 更改了每个文件中特定值
  • 将所有
  • 文件压缩到具有特定名称(格式:AppName.Version.zip(的文件夹中,该文件夹将位于同一路径中:C:Users*Downloads

问题:

    如何再次压缩文件夹,
  1. 以便通过单击压缩文件夹,我将立即看到文件(压缩文件夹>文件(,而不是文件夹,然后是文件(压缩文件夹>文件(

  2. 压缩和扩展命令对文件夹名称格式大喊大叫,因此这些命令无法完成其工作。我该如何克服它?

     $languageString = Read-Host ...
     $replaceString = '<value>' + $languageString
     $file = Get-Childitem "c:users*Downloadsapp.version.zip" -Recurse
     $originalFileName = $file.Name
     $absoluteFilePath = $file.FullName
     $rename-item -path $absoluteFilePath -newname translate.zip
     $file = Get-Childitem "c:users*Downloadstranslate.zip" -Recurse
     $absoluteFilePath = $file.FullName
     $unzipFilePath = $absoluteFilePath.Substring(0, $absoluteFilePath.LastIndexOf('.'))
     expand-archive -path $absoluteFilePath -destinationpath $unzipFilePath
     $files = @(Get-Childitem "c:users*Downloads*.xml)
     foreach ($file in $files)
     {
        (Get-Content -path $file.FullName -Raw) -replace '<value>' , $replaceString | Set-Content -Path $file.FullName
    #this line makes problems...
     Compress-Archive -path $unzipFilePath -Destinationpath $unzipFilePath -Force

我分三步看到它:1. 在临时干净的位置解压缩。2.在那里更改文件3.zip那个位置回来。

代码将具有以下功能:

function Unzip_archive ($ArchiveFullPath, $Temp_Work_Fld) {
    try {
        #unzip
        Add-Type -assembly 'system.io.compression.filesystem'
        [io.compression.zipfile]::ExtractToDirectory($ArchiveFullPath, $Temp_Work_Fld)
    }
    catch {
        throw "Failed to unzip the artifact"
    }
    Remove-Item $ArchiveFullPath -Force #only if you want to delete the archive
}
function CreateZipArchive($FolderToArchive, $ZipArchiveFullName){ 
    $source = Get-ChildItem * -Directory | Where-Object {$_.FullName -match "$FolderToArchive"}
    write-host "`nArchiving: $source ........."
    Add-Type -Assembly system.IO.compression.filesystem
    $compressionlevel = [System.IO.Compression.CompressionLevel]::Optimal
    [io.compression.zipfile]::CreateFromDirectory($source, $ZipArchiveFullName, $compressionlevel, $true)
    write-host "nDone !"
} 

然后只需在临时文件夹中进行更改之前和之后调用函数

最新更新