2
$\begingroup$

I know that one can archive data with Mathematica using: CreateArchive[source,path]

I have a lot of folders (folder1, folder2,folder2 etc.) that I would like to compress as a batch. The result should be: folder1.zip, folder2.zip, folder3.zip.

Anyone knows how to do that with mMathematica ?

could be a starting point: https://stackoverflow.com/questions/44644428/batch-file-to-zip-folders-with-original-names

$\endgroup$
6
  • 2
    $\begingroup$ I feel this is one of those times where you should be using a dedicated program, instead of trying to do it in Mathematica. $\endgroup$ Oct 20, 2018 at 14:39
  • $\begingroup$ @J.M.iscomputer-less I know, but it would be so convenient. I don't care if it takes a bit longer. $\endgroup$
    – james
    Oct 20, 2018 at 14:41
  • $\begingroup$ I think if you wish to add to an already compress zip folder it would require unzipping it, and re-zipping it with the new contents. $\endgroup$
    – ktm
    Oct 20, 2018 at 14:43
  • $\begingroup$ @user6014 Or simply putting everything into a folder and zipping it, and then deleting the original files $\endgroup$
    – james
    Oct 20, 2018 at 15:13
  • $\begingroup$ @james Can you describe in more detail exactly what you are after? I think one of the main reasons you haven't gotten a ton of replies here is that it's entirely unclear what you want. $\endgroup$
    – ktm
    Oct 22, 2018 at 20:08

1 Answer 1

10
+100
$\begingroup$

This can be achieved with Map (/@).

1) Identify the folders you want to compress:

In[30]:= FileNames["folder*"]

Out[30]= {"folder1", "folder2", "folder3"}

2) Map CreateArchive across them:

In[31]:= CreateArchive /@ FileNames["folder*"]

Out[31]= {"/Users/user/Desktop/folder1.zip", \
"/Users/user/Desktop/folder2.zip", \
"/Users/user/Desktop/folder3.zip"}

Edit:

To delete the uncompressed folders after, you can run

DeleteDirectory[#, DeleteContents -> True]& /@ {"folder1", "folder2", "folder3"}

(Using FileNames["folder*"] here will possibly delete the .zip folders a well, so I'd refernce the original files directly)

$\endgroup$
4
  • $\begingroup$ Thanks a lot ! This is exactly what I was looking for ! Is it also possible to delete the uncompressed folders afterwards ? $\endgroup$
    – james
    Oct 23, 2018 at 12:24
  • $\begingroup$ @james added that $\endgroup$
    – ktm
    Oct 23, 2018 at 13:26
  • $\begingroup$ I did that a lot recently, but every now and then, I had some temporary files left over. Mathematica creates a temporary folder and sometimes just leaves it there. Has someone else seen this? Adding a Pause[0.25] helped a bit, but not always. $\endgroup$
    – AxelF
    Oct 23, 2018 at 15:19
  • 1
    $\begingroup$ To avoid the temporary folders, try the option CreateIntermediateDirectories -> False with the CreateArchive command. $\endgroup$
    – LouisB
    Oct 23, 2018 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.