Reputation: 59
I try to automate a repetitive scenario by Powershell script:
I have a compressed (.zip) folder with multiple files (.XML) in it.
My scenario is to:
C:\Users\*\Downloads
AppName.Version.zip
) that will be located in the same path: C:\Users\*\Downloads
.Questions:
How do I compress the folder again so that by clicking on the compress folder I'll see immediately the files (zipped folder>files) and not a folder then the files (zipped folder>files)
Compress and Expand commands are yelling about the folder name format of and therefore those commands are failed to their job. How do I get over it?
$languageString = Read-Host ...
$replaceString = '<value>' + $languageString
$file = Get-Childitem "c:\users\*\Downloads\app.version.zip" -Recurse
$originalFileName = $file.Name
$absoluteFilePath = $file.FullName
$rename-item -path $absoluteFilePath -newname translate.zip
$file = Get-Childitem "c:\users\*\Downloads\translate.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
Upvotes: 0
Views: 693
Reputation: 1140
I see it in three steps: 1. unzip in a temporary clean location. 2. change your files there 3. zip that location back.
the code would have the functions:
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 !"
}
then just call the functions before and after doing your changes in the temporary folder
Upvotes: 0