Skip to main content
4everJang
Legend
April 21, 2019
Answered

Creating a ZIP file

  • April 21, 2019
  • 3 replies
  • 12015 views

Has anyone created a ZIP file via scripting - or even via a batch file on Windows 10 ?

This topic has been closed for replies.
Correct answer Ian Proudfoot

No, I did not get it working, even after trying all the variants I found on StackOverflow. PowerShell sounds good but is a horror when trying to call it from a batch file.


I have it working as follows:

Extend script must write two files the first is the bat file which contains the following:

PowerShell -ExecutionPolicy Bypass -File "C:\path_as_required\zipTest.ps1"

The second file is the PowerShell script which for example can be written to compress a folder or many individual files:

Compress-Archive -Path "D:\Support\CGM" -DestinationPath "D:\Support\CGMzip.zip"

This works without any annoying warnings because of the ExecutionPolicy Bypass.

I hope it works for you too!

Ian

3 replies

bretyboy
Inspiring
April 29, 2020

For anyone on Mac, here are the solutions that I've found:

var locationOfNewZip = "/Users/YourUserName/Desktop"; //path to the folder where you want your .zip file to appear
var nameOfFolderToBeZipped = "myFakeFolder";  // this is the name of the folder that get's zipped up
var nameOfNewZippedFolder = nameOfFolderToBeZipped + ".zip"; //Just the name of your new zip, not a path
var terminalCommand = 'cd "' + locationOfNewZip + '" && zip -r -X "' + nameOfNewZippedFolder + '" "' + nameOfFolderToBeZipped + '"';


//for After Effects. Returns terminal result
var test = app.system(terminalCommand)


//photoshop
//app.system(terminalCommand)

 

Another solution in the form of a function:

 

//This is a more complex version that takes in a path string and pushes out a zip for you:

var myNewZip = zipFolder("~/Desktop/MyFolderName");
//places zip file right near the folder being zipped. Just pass in path to folder to be zipped
function zipFolder(locationOfFolderToZip){
varnameOfFolderToBeZipped=Folder(locationOfFolderToZip).name
varcleanLocationOfFolderToZip=Folder(locationOfFolderToZip).fsName;//sometimes we get '~/Desktop/Folder/NextFolder'. The '~' path won't work for terminal. This changes it back to "Users/userName/Desktop" format
varlocationOfNewZip=returnPathWithoutLastItem(cleanLocationOfFolderToZip)//remove last item from path. So that the zip will be placed next to the folder we're zipping.
varnameOfNewZippedFolder=nameOfFolderToBeZipped+".zip"; //Just the name of your new zip, not a path
varterminalCommand='cd "'+locationOfNewZip+'" && zip -r -X "'+nameOfNewZippedFolder+'" "'+nameOfFolderToBeZipped+'"';
app.system(terminalCommand)
returncleanLocationOfFolderToZip+nameOfNewZippedFolder;
}//
//end function
//We use this in the zipping script to get a path to where the zip should go. (right near the folder it's zipping)
function returnPathWithoutLastItem(myPath){

myPath=decodeURI(myPath.toString())
if(myPath!==false&&myPath.toString().length>1&&myPath.toString().split("/").length>1){
varpathItems=myPath.split("/")
varlastPathItem=pathItems[pathItems.length-1];
varnewPath= [];
//loop through path items to add them to new string. This gets our array without the last item.
for(vari=1; i<pathItems.length-1; i++){//we use -1 so that this doesn't add the last item.
varcurrentItem=pathItems[i];
newPath.push(currentItem)
}//end for loop
varpathWithoutLastItem=newPath.join("/");
}//end if
returnpathWithoutLastItem
}//
//end function

 

Ian Proudfoot
Legend
May 13, 2019

How you tried a batch file that runs in Windows PowerShell? I seem to remember that PowerShell has a compress archive command.

Ian

4everJang
4everJangAuthor
Legend
May 13, 2019

How do I run a WIndows PowerShell ? The only option I have is use File.execute( ). I can create a batch file and execute it that way. I cannot hand any arguments to the execute( ) function - or I would not have needed a batch file at all.

Ian Proudfoot
Legend
May 13, 2019

Generate a text file. Add the commands as required then save it with a .ps1 extension. Then execute this new batch file.

I've not tried it yet, but it could work...

Klaus Göbel
Legend
April 21, 2019

Hi Jang,

I create zipfiles using a one-line batchfile with 7-Zip:

Zip.bat

Looks like:

"C:\Program Files\7-Zip\7z.exe" a "MyFiles.zip" @Content.txt

That calls 7zip.exe (you need whole path), creates the file MyFiles.zip and uses a text file with a list of files and folders.

Content.txt:

Folder1

Folder2

Folder3

Script1.jsxbin

Script2.jsxbin

Script3.jsxbin

HowToInstall.txt

4everJang
4everJangAuthor
Legend
April 22, 2019

OK, I found a method now that should work on any Windows machine, using a batch file that calls a vbs script, which in turn uses a call to a standard zipping library that only has the dot net interface.

4everJang
4everJangAuthor
Legend
May 13, 2019

I finally found time to start finishing this job. I got the batch file in place but there is a problem with access rights. When I run the batch from the command prompt or from Windows Explorer, it works fine. When I call it via the File.execute( ) function it throws an error.

The batch file uses a VBS to create the ZIP. I guess the VBS is so dangerous that you cannot run it from a batch file. So I will have to look at downloading an exe for zipping after all.