Batch move a list of zip files content up to root level
Andrew Mclaughlin
I have around 8,000 Zip files, each one has a folder inside of it with the same name. They look like this
Example #1.zip └─ Example #1 ├─ 1.png ├─ 2.png └─ 3.pngIs there a way to move the files inside the folder to the root of the Zip, and then delete the now empty folder? the result should look like this
Example #1.zip ├─ 1.png ├─ 2.png └─ 3.png 2 2 Answers
Assuming Example #1.zip is actually a folder (from unpacking), not a zip file:
cd "Example #1.zip"
for /r %f in (*.png) do move "%f" .(You can enter those lines as is into command prompt. If you run them from a .cmd file you'll need to use %%f instead of %f in both places.)
If the files are still zipped, you likely have to unzip them without pathnames and rezip. You can use the no compression/store option as .png doesn't compress significantly anyway.
1I wrote the following batch file and it works to solve the problem you were trying to fix. It will take zip files with directory structure matching the name of the zip file itself, and create zip files that don't. I only made 5 of them to test but it is indeed quite fast and leaves two tidy "Processed" and "Fixed" folders when complete.
- You will need the command line version of 7zip (
7z.exeor7za.exe) either in your path, or in the same directory with the zips and the batch file. If you have7za.exerename it to7z.exe. - You should copy off your zips to somewhere else before running it (just in case!)
- It takes zip files in the same directory as the batch file.
- It decompresses them one at a time.
- It compresses a fresh zip EXPECTING that there will be a directory there with the same name as the zipfile itself into a subfolder called "Fixed".
- It moves the old zip file into a folder called "Processed".
There are very few safeguards in here. ONLY have zip files that adhere to your scheme in the folder. Easily fixed, but I didn't bother safeguarding anything.
@echo off
Set WildCard=*.zip
Set ZipFileDir=%~DP0
Set FixedZipFilesDir=%ZipFileDir%Fixed
Set ProcessedZipFilesDir=%ZipFileDir%Processed
Set WorkingDir=%ZipFileDir%temp
md "%WorkingDir%"
md "%FixedZipFilesDir%"
md "%ProcessedZipFilesDir%"
REM Loop on zips in the directory.. call fn ProcessZip with each name
for %%f in (%WildCard%) do call :ProcessZip "%%f"
REM Clean up our temp working space and exit
rd /s /q "%WorkingDir%"
goto :EOF
:: --------------------------------------------------
REM Function to process each zip file found.
:: --------------------------------------------------
:ProcessZip
Set InputZipPath=%~DP1
Set InputZipName=%~N1
REM Extract the file to our temporary working directory
7z x "%InputZipPath%%InputZipName%.zip" -o%WorkingDir%
REM Create a new zip excluding the path
7z a "%FixedZipFilesDir%\%InputZipName%.zip" "%WorkingDir%\%InputZipName%\*"
REM Move the original zip to our processed folder.
move "%InputZipPath%%InputZipName%.zip" "%ProcessedZipFilesDir%"
goto :EOF