Locked

Creating a ZIP file

Advocate ,
Apr 21, 2019 Apr 21, 2019

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

5.3K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , May 14, 2019 May 14, 2019

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!

...

Likes

Translate

Translate
Enthusiast ,
May 14, 2019 May 14, 2019

Copy link to clipboard

Copied

Yes Markus, this is the same as I explained above. However yours accepts parameters to the PowerShell script while I avoid that and just re-write both scripts.

In any case it's good to know that the ability to zip files and folders works without any third-party extras to install.

When you think about it, this method opens up a lot of the capabilities of PowerShell and has got me thinking about other possibilities...

Ian

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
May 15, 2019 May 15, 2019

Copy link to clipboard

Copied

Hello Ian and Markus,

Thanks much for demystifying the PowerShell approach. Now that I create the batch files from ExtendScript there seem to be no more security issues.

I guess there is no way to make the command prompt invisible ?

4everJang

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 15, 2019 May 15, 2019

Copy link to clipboard

Copied

Hi Jang,

PowerShell.exe -windowstyle hidden

should do the job.

Markus

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
May 15, 2019 May 15, 2019

Copy link to clipboard

Copied

and what about the command prompt where the first batch file runs that calls the powershell script ?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 15, 2019 May 15, 2019

Copy link to clipboard

Copied

Hi Jang,

You Need to write a thrid file in VB Script, which calls the Batch file, which calls the PowerShell Script.

windows - Run a batch file in a completely hidden way - Super User

But I think it should also be possible, to call PowerShell from VBS directly.

If Adobe would provied "File.execute([list of parameters]" it would be possible to call PowerShell directly, but unfortunately this isn't support.

Markus

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
May 15, 2019 May 15, 2019

Copy link to clipboard

Copied

I think I will stick with the ugly solution of having a large black command prompt window pop up for the time being. If the customer does not like it, they will have to raise the budget so that I can get the fancy stuff done as well...

I did find some vbs scripts that run the powershell, but I am deferring that to the final stage of the current project.

Thanks

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 15, 2019 May 15, 2019

Copy link to clipboard

Copied

Hi Jang,

There's no way to hide the initial command prompt console. However if you add a Start command before PowerShell, the console window will close immediately rather than waiting for the PowerShell script to finish.

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

Markus, I think that the PowerShell window will also flash on screen even when the WindowStyle parameter is added.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 15, 2019 May 15, 2019

Copy link to clipboard

Copied

Jang and Ian,

I think it wouldn't be possible to avoid the window. Any solution, even VBS, requires a call by Parameter, which is not supported bei ExtendScript.

You have to live with that or you have to write a Plugin or your own (DotNet) executable, which takes a parameter file and runs all stuff in hidden mode.

Markus

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 15, 2019 May 15, 2019

Copy link to clipboard

Copied

Ian,

yes I saw your post after I posted mine.

PowerShell is very powerful. You can refer any DotNet-Library and integrate these in your script. Simply write your own DotNet.dll, if functionality is missing, refer it, and do what ever you want.

Looking Forward to the time these ugly, not type-safe, DOS Batch Scripts are not used and supported anymore.

Have a great day

Markus

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 29, 2020 Apr 29, 2020

Copy link to clipboard

Copied

LATEST

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

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines