Here's the renaming problem I need to solve: There are a few hundred .zip archive files which contain at least few files in them. Files in archives are parts and are named like this:
Data_Customer1_20160101_part1.txt
Data_Customer1_20160101_part2.txt
Data_Customer1_20160101_partN.txt
I need to change Customer1 part to Customer2 using CMD, PowerShell or 7zip. Approach extract - rename - archive is not acceptable as there are few hundred GBs of data so it would be very slow.
Is there any possible solution to rename files in archives without extrating them?
This can be done using PowerShell
and the latest 7-Zip 15.14 exe. Download and install the 7-Zip, once installed 7z.exe should be in the following location C:\Program Files\7-Zip\7z.exe
.
$ArchivesFolder = "T:\Your Archives\"
$7zipExe = "C:\Program Files\7-Zip\7z.exe"
$search = "Customer1"
$replace = "Customer2"
gci $ArchivesFolder -Filter "*.zip" | % {
$archive = $_.FullName
Write-Host $archive -ForegroundColor DarkYellow
& $7zipExe l $archive `
| select -Skip 11 `
| %{IF($_.Length -gt 52 ){$_.substring(53)}} `
| ?{$_ -notmatch "\d* files, \d* folders|------------------------" -and $_ -match $search } `
| %{
write-host "Renaming:[$_] To:[$($_ -replace $search,$replace)]" -ForegroundColor cyan
& $7zipExe rn $archive "$_" "$($_ -replace $search,$replace)" | Out-Null # Comment out
}
}
The above PowerShell
will recurse through a give folder ($ArchivesFolder) of .zip files. It will look within each zip file and replace/rename any part of the files name that match the give search variable ($search
) with the replacement valuve given ($replace).
If you want to see what the script does before running it, comment out the line that has (# Comment out
) next to it with a #
. This will stop any file from being renamed.
The script is using the rn
parameter of 7z.exe The syntax to rename files inside archives is
7z rn <archive_name> <src_file_1> <dest_file_1> [ <src_file_2> <dest_file_2> ... ]
Note This will only work with 15.14 version of 7z.exe.