21

Under the folder images\, I have the following folders:

  • Usa\
  • Italy\
  • Japan\

etc..

I want a script that creates a .zip for each of these folders, so I have usa.zip , italy.zip, japan.zip.

How can I do that?

0

4 Answers 4

12

You can use Winrar for this.

Select all your folders, right click and select Add to archive. Now select the option Put each file to separate archive under the tab Files:

enter image description here

Click OK and you're done. If you want .zip files instead of .rar, just select ZIP in the General tab under Archive format.

12

I assume this is Windows, as you are using backslashes.

Get 7za.exe (the command-line version of 7-zip) and put it in your %PATH%.

Then run this within your Images\ directory:

for /f "tokens=* usebackq" %G in (`dir /b /a:d "%cd%"`) do 7za a -r -tzip "%~G.zip" "%~G"

Or in a batch script:

for /f "tokens=* usebackq" %%G in (`dir /b /a:d "%cd%"`) do 7za a -r -tzip "%%~G.zip" "%%~G"
4
  • This doesn't work if the folders have spaces in their name.
    – jiggunjer
    Sep 19, 2017 at 7:31
  • @jiggunjer Yes, you're right. I have fixed it to work with spaces in folder names.
    – paradroid
    Sep 19, 2017 at 13:19
  • 2
    This works even with normal 7zip executable. I updated 7za to "C:\Program Files\7-Zip\7z.exe" and it worked without issue. Jul 17, 2019 at 11:31
  • 1
    I can confirm that the above works with 7zip "out of the box" without any other download. I update my path to include 7z.exe and ran for /f "tokens=* usebackq" %G in (dir /b /a:d "%cd%") do 7z a -r -tzip "%~G.zip" "%~G" without issues. Exactly what I was looking for!
    – AJ Tatum
    Dec 1, 2020 at 18:45
9

Assuming you are on Linux you could use a Bash script like this:

  #!/bin/bash

  IFS=$'\n'

  for f in $(find "$1" -mindepth 1 -maxdepth 1 -type d ); do
    zip -r "$f" "$f"
  done

This script will take the first argument as folder, in this case images, and recurse exactly one level deep to get all your folders (Usa, Italy and Japan) and zip them in the same place.

It will not delete old zip files or the source folders. Zip is smart enough to detect existing zip files and update them when run again. However, if you delete a file in the source folders it will not be deleted in the zip files. You might circumvent this behaviour by deleting existing files beforehand.

0
2

This answer by MvG worked for me on Ubuntu:

You can use this loop in bash:

for i in */; do zip -r "${i%/}.zip" "$i"; done

i is the name of the loop variable. */ means every subdirectory of the current directory, and will include a trailing slash in those names. Make sure you cd to the right place before executing this. "$i" simply names that directory, including trailing slash. The quotation marks ensure that whitespace in the directory name won't cause trouble. ${i%/} is like $i but with the trailing slash removed, so you can use that to construct the name of the zip file.

If you want to see how this works, include an echo before the zip and you will see the commands printed instead of executed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .