Zip group of folder (with space)

Hello
I have hundreds of folders to zip and I’m creating a script. Many folders have spaces and this creates problems.
I know that the command for zip a folder can be:
zip -r -X archive_name.zip folder_to_compress

But I need to convert this command to something that accept space in path and in name of zip
So, a folder Apex (OpenType) have to become Apex (OpenType).zip

Can someone help me to write a valid command that maintain space?
Thank you in advance

The thing that will help is to quote the variable used so that the shell doesn’t view those spaces (or other characters) as separators. But, don’t try to do it yourself by just adding quote characters. Instead use the AppleScript “quoted form of” command.
If you had the folder name/path in a variable someFolder, you’d do this:

set someZip to someFolder & ".zip"
do shell script "zip -r -X " & quoted form of someZip & " " &  quoted form of someFolder 

Note that, in my example, I’m assuming that someFolder doesn’t include the colon at the end (if it is the path of the folder). If your variable does, you’d obviously need to remove that before adding “.zip” at the end.

Thank you Krioni for answer
I have a script that try to get the element(s) by drag&drop with

on open these_items
	try
		repeat with this_item in these_items
			tell application "Finder" to set this_item_name to name of this_item
			
			listing(this_item, this_item_name)
		end repeat
		
		Ending()
		
	end try
end open

So si right to translate your script in these way?

set this_item to someFolder & ".zip"
do shell script "zip -r -X " & quoted form of this_item & " " &  quoted form of someFolder

By the way, what is a “colon”?

A couple of minor comments…

You can put the quoted form of in a variable, like so:

set srcFolder to POSIX path of (path to desktop as text) & "foo"
set qfSrcFolder to quoted form of srcFolder

This enables you to have a cleaner do shell script command line.

Also, you can combine the -r and the -X into a single option, -rX.

Additionally, the zip command will automatically append the zip extension so you don’t need to add that in separately.

Finally, I generally use the -x option to keep .DS_Store files from being added.

set srcFolder to POSIX path of (path to desktop as text) & "foo foo"
--> "/Users/username/Desktop/foo foo"

set qfSrcFolder to quoted form of srcFolder
--> "'/Users/username/Desktop/foo foo'"

do shell script "zip -rX " & qfSrcFolder & " " & qfSrcFolder & " -x \\*.DS_Store"
(*
"  adding: Users/username/Desktop/foo foo/ (stored 0%)
  adding: Users/username/Desktop/foo foo/font comparison times vs palatino.rtf (stored 0%)
  adding: Users/username/Desktop/foo foo/foo foo inner/ (stored 0%)
  adding: Users/username/Desktop/foo foo/foo foo inner/test pick by selecting markdown.scpt (stored 0%)"
    *)

Assuming a folder ‘foo foo’ on the desktop, the above will use that folder and create a zip file ‘foo foo.zip’, also on the desktop.

BTW, a colon : is used to separate elements in a HFS path, for example:

set srcFolder to (path to desktop as text) & "foo foo"
--> "MacHD:Users:username:Desktop:foo foo"

You can append a colon to explicitly define it as a folder.

When using the shell (or POSIX Path), substitute a forward slash /.

1 Like

Thank you, I will try it

That’s true but I guess I’m driven to putting the ‘as text’ there so I can alternatively make an HFS reference. Without the posix path of, it will generate a list.

set srcFolder to (path to desktop) & "foo"
1 Like