Zachary McClurg
Zachary McClurg

Reputation: 1

7zip Unzip Folder, rename contents, rezip folder

I have a list of thousands of folders. Inside these folders are zip files. I need to unzip the folders, rename the contents of the zipped folder to "zipped folder name" & "_" & "File Name" and then rezip the unzipped folder. I have tried this batch code but it does not work as all it does is create a folder with the zipped file name. Any help is appreciated! I have 7zip and powershell as the main options I work with but I will do whatever will work.

@echo off
setlocal enableextensions disabledelayedexpansion
for %%z in (*.zip) do (
    if not exist "%%~nz" md "%%~nz"
    7za e -o"%%~nz" "%%~fz"
    for %%f in ("%%~nz\*") do ren "%%~ff" "%%~nz - %%~nxf"
)

Upvotes: 0

Views: 951

Answers (1)

wolfrevokcats
wolfrevokcats

Reputation: 2100

Your code is missing an archiving step.
I would suggest putting modified archives into a separate directory, so in case something goes wrong your original files will stay intact.

This is the possible code:

@echo off
setlocal enableextensions disabledelayedexpansion
set OUTPUT_DIR=..\output
md "%OUTPUT_DIR%"
for %%z in (*.zip) do (
    if not exist "%%~nz" md "%%~nz"
    7za e -o"%%~nz" "%%~fz"
    for %%f in ("%%~nz\*") do ren "%%~ff" "%%~nz - %%~nxf"
    7za a -tzip "%OUTPUT_DIR%\%%~nz.zip" ".\%%~nz\*"
) 

And, since you are using -e instead of -x, I assume your zip archives are flat, that is they don't have repeating filenames in subdirectories.

Upvotes: 0

Related Questions