Creating a zip file and recursing through subfolders

@rjames86, many thanks again for your macro/script.

I've enhanced it a bit, to

  • add some documentation
  • Ask User to Confirm
  • Make it clear that the original source folder will be DELETED
  • Provide a nice report, copied to clipboard

Example Results


##Macro Library   @ZIP Folder (& all Sub-Folders) & DELETE


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/7/77efe4d6b43590ae28a8072e31feaa223d3ffabb.kmmacros">@ZIP Folder (& all Sub-Folders) & DELETE.kmmacros</a> (11 KB)

---

###ReleaseNotes

HOW TO USE:

1. Select one or more FOLDERs in the Finder
   • Each Folder wll be put in a separate Zip file
2. Trigger this Macro

Author:	 @rjames86 -- Original Macro/Script Posted below 
                @JMichaelTX -- this macro based on the one by @rjames86

WARNING!

The Folder selected in the Finder will be PERMINATELY DELETED.
(not just moved to Trash)

If the Zip File already exists, it will be OVERWRITTEN.


REFERENCE

Topic:		Creating a zip file and recursing through subfolders
Forum:		Keyboard Maestro Discourse, general

Post by:		rjames86
Post Date:	2016-12-22
Script Lang:	Bash
Post URL:	https://forum.keyboardmaestro.com/t/creating-a-zip-file-and-recursing-through-subfolders/5811/15?u=jmichaeltx

If you want to change the name of the zip file, 
  • just set a new variable in KM and 
  • then change the line to 
     `zip -rm "$KMVAR_<thevarname>" "$BASE" `

   where `<thevarname>` is the name of your variable

For a detailed, step-by-step description of the script, see:
[The above post by rjames86](https://forum.keyboardmaestro.com/t/creating-a-zip-file-and-recursing-through-subfolders/5811/17?u=jmichaeltx)

---

<img src="/uploads/default/original/2X/d/da09f3e2b3fe7f3b46c0a6260dc5304ee2e6d27d.png" width="595" height="895">

BTW, to address concerns about deleting the folder/files without first expanding the zip, I did expand the zip files a number of times in the process of testing the above macro, and it worked flawlessly.

Having said that, if the files were very important, or hard to replace, then I might just zip them normally (no deletion) and test the zip integrity before i deleted the original source files.

Again, this is an individual decision.

Great writeup! I personally don’t like the idea of deleting the original folder. Seems too dangerous.

I’d personally change the line zip -rm "$BASE_NO_EXT" "$BASE" to be:

zip -r "$BASE_NO_EXT" "$BASE"

It would be easy to add another confirmation if you wanted to delete or not. A way to do this could be to ask, and if they do, set a variable DELETE_CHECK to “true”. The code would then look like this:


if [[ $KMVAR_DELETE_CHECK == 'true' ]]; 
then 
    ZIP_ARGS="rm";
else
    ZIP_ARGS="m";
fi

zip -$ZIP_ARGS "$BASE_NO_EXT" "$BASE"

OK, I'm open to that.
How would you verify the integrity of the zip, before deleting the original folder? Or maybe just move the folder to Trash, rather than delete? Could this all be done in one macro?

oops. See above

OK, here's a version that moves the folder to trash, AFTER the zip has completed.

Still would like a way to verify the zip integrity. Any ideas?


Here are the main changes:


##Macro Library   @ZIP T -- Zip Folder (& all Sub-Folders) & Move to TRASH


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/4/467bd257d4dda460190f94134e6a4b8f745af4d4.kmmacros">@ZIP T -- Zip Folder (& all Sub-Folders) & Move to TRASH.kmmacros</a> (12 KB)

---

###ReleaseNotes

HOW TO USE:

1. Select one or more FOLDERs in the Finder
   • Each Folder wll be put in a separate Zip file
2. Trigger this Macro

Author:	 @rjames86 -- Original Macro/Script Posted below 
                @JMichaelTX -- this macro based on the one by @rjames86

WARNING!

The Folder selected in the Finder will be MOVED TO TRASH.

If the Zip File already exists, it will be OVERWRITTEN.


REFERENCE

Topic:		Creating a zip file and recursing through subfolders
Forum:		Keyboard Maestro Discourse, general

Post by:		rjames86
Post Date:	2016-12-22
Script Lang:	Bash
Post URL:	https://forum.keyboardmaestro.com/t/creating-a-zip-file-and-recursing-through-subfolders/5811/15?u=jmichaeltx

If you want to change the name of the zip file, 
  • just set a new variable in KM and 
  • then change the line to 
     zip -rm "$KMVAR_<thevarname>" "$BASE" 

     where <thevarname> is the name of your variable

For a detailed, step-by-step description of the script, see:
https://forum.keyboardmaestro.com/t/creating-a-zip-file-and-recursing-through-subfolders/5811/17?u=jmichaeltx

---

<img src="/uploads/default/original/2X/4/436a24f734c8243e2ad494ff783cd83898e0cebc.png" width="592" height="1590">

Add the T flag to zip.

zip -rT '$BASE_NO_EXT" "$BASE"

-T test zipfile integrity

@rjames86, et al:

How can we detect if either:

  • The zip fails
  • The zip lacks integrity

and thus NOT do the Trash?

You can add the following command after the zip, but before popd

if [[ `unzip -tq "${BASE_NO_EXT}.zip"` =~ "No errors detected" ]]; 
then
    osascript -e 'tell application "Keyboard Maestro Engine" to setvariable "DeleteAfterZip" to "true"';
else
    osascript -e 'tell application "Keyboard Maestro Engine" to setvariable "DeleteAfterZip" to "false"';
fi

you should then have a variable available to you in KM called DeleteAfterZip. If its set to the string “true”, you can delete it

1 Like

@rjames86, thanks, this looks perfect.

Just to make sure I understand correctly, this test will fail if the zip does not have integrity, correct? I doing both:

zip -rT "$BASE_NO_EXT" "$BASE"

if [[ `unzip -tq "${BASE_NO_EXT}.zip"` =~ "No errors detected" ]]; 
then
    osascript -e 'tell application "Keyboard Maestro Engine" to setvariable "ZIP__NoErrors" to "true"';
else
    osascript -e 'tell application "Keyboard Maestro Engine" to setvariable "ZIP__NoErrors" to "false"';
fi

Note I changed you KM Var name to "ZIP__NoErrors". :smile:

Thanks. I really appreciate your expertise and patience with me.

Correct. If it fails (i.e. doesn't contain the string "No errors detected") it'll set the NoErrors to false.

Absolutely! Happy to help out

Thanks to the original macro/script by @rjames86, and his help in improving my macro (based on his), I think we now have a decent macro good for general consumption. I also hope that @Kurt_Kessler finds it useful.

I have posted this maro here:

If you have any comments on this macro, please post them in the above thread.

Thanks again, @rjames86.

2 Likes

I think it’s perfect! Thanks to everyone!!!

1 Like