Script: Create zip file which has same name as zip folder

Discuss Task Sequences and Scripting related issues.

Script: Create zip file which has same name as zip folder

Postby melvin » Thu Aug 17, 2006 8:48 pm

This is a users email support request:

We have a folder called Zipped. Within that folder we have 100's of folders. Example we have folder 31415, 45136, 69343, 89452, etc... Which are dropped every day. Within this folder we have another folder.
Example under folder 31415, we have folder name test, call. Under this folder, we have multiple subfolders and files.

Here is what we want.

1. We want to auto zip folders which are 90 days and older. Example Folder 31415, 45136, and 69343 are 90 days older.
2. We want the folders to be zipped as the folder name. So it should zip as 31415.zip, 45136.zip, and 69343.zip. (remember the folder 31415 doesn't have any files within that.) It has another folder name under that folder and then it has files and subfolders...
melvin
Site Admin
 
Posts: 979
Joined: Thu May 25, 2006 10:20 am
Location: Santa Barbara, california

Postby melvin » Thu Aug 17, 2006 8:54 pm

You would have to use the Professional Edition (File menu / Set Edition ). It has a non graphical scripting module.

This can be accomplished in a 3 step Script:
1) Loop through the Zipped folder and get folder names.
2) Set the two varying parameters for a generic existing zip task. These 2 parameters would be zip filename and folder to zip
3) Then run the zip task for each folder name 31415 etc..

You would also have to create a Zip task called 'zipTest' with the following parameters:
1) "Directory to place zip archive" - should be entered
2) "Directory to Zip" - this will be filled in automatically by script
3) "Name for zip archive" - this will be filled in automatically by script

Script is attached in the next post. In the script the only thing you need to do is edit the line:
String unzippedDir = "c:\\test\\unzipped";

Enter your main unzipped folder. use \\ instead of \
melvin
Site Admin
 
Posts: 979
Joined: Thu May 25, 2006 10:20 am
Location: Santa Barbara, california

Postby melvin » Thu Aug 17, 2006 8:58 pm

import com.hitek.engine.mods.script.Script;
import java.text.DateFormat;

// set the directory where the source folders are located .. need to use \\ instead of \
String unzippedDir = "c:\\test\\unzipped";

// the newly created zip files are placed in a fixed folder, so this will not change and is set in the zipTask

// initialize some variables
String name = "";
Date modified;
DateFormat df = DateFormat.getDateTimeInstance();
String mod = "";

// create a new File object of the directory
File dir = new File(unzippedDir);

// list the file objects in the directory
File[] list = dir.listFiles();
for (int i=0; i < list.length ; i++)
{
// do not process any files in main unzipped folder. only process directories
if(list[i].isDirectory()==false)
{
continue;
}
name = list[i].getName();
modified = new Date(list[i].lastModified()); // create a date object of the directory timestamp
mod = df.format(modified); // format the timestamp for displaying in logs
System.out.println("Dir = " + name + " , timestamp = " + mod);
// set a calendar corresponding to the file timestamp date object
Calendar modifiedCal = Calendar.getInstance();
modifiedCal.setTime(modified);
// get a calendar corresponding to current time
Calendar now = Calendar.getInstance();

// add 90 days to the file timestamp calendar.
// if it is still newer than current time calendar, it indicates that folder is older than 90 days old
modifiedCal.add(Calendar.DATE,90);
if(modifiedCal.before(now))
{
System.out.println("Old Folder = " + name);
String zipFilename = name + ".zip";
String zipFolderpath = list[i].getPath();
// set the parameters
//Task.PARAMETERS1 = directory to zip
Script.setPar("zipTest", "Task.PARAMETERS1", zipFolderpath );
//Task.PARAMETERS3 = name for zip file
Script.setPar("zipTest", "Task.PARAMETERS3", zipFilename );
//run the zip task. exit code = 0 means zipped successfully.
int exitCode = Script.run( "zipTest" );
System.out.println("Exit code = " + Integer.toString(exitCode));
}
}
melvin
Site Admin
 
Posts: 979
Joined: Thu May 25, 2006 10:20 am
Location: Santa Barbara, california

Postby Tech Support » Wed Aug 23, 2006 9:13 am

user additionally wanted to know how to only zip folders older than 60 days but newer than 90 days..
Tech Support
Site Admin
 
Posts: 1276
Joined: Thu May 25, 2006 11:12 am

Postby Tech Support » Wed Aug 23, 2006 9:14 am

The code below should be used instead to get date range between 60 and 90 days
----------------------------------------------
...
...
System.out.println("Dir = " + name + " , timestamp = " + mod);
// get a calendar corresponding to current time
Calendar now = Calendar.getInstance();
// set a calendar corresponding to the file timestamp date object
Calendar startCal = Calendar.getInstance();
Calendar endCal = Calendar.getInstance();
startCal.setTime(modified);
endCal.setTime(modified);
startCal.add(Calendar.DATE,90);
endCal.add(Calendar.DATE,60);
if( startCal.after(now) && endCal.before(now) )
{
.....
....
}
Tech Support
Site Admin
 
Posts: 1276
Joined: Thu May 25, 2006 11:12 am

Postby Tech Support » Thu Oct 11, 2007 12:12 pm

user now wants to only zip folders that were created 4 months ago .. example if task is run on may 15th, it should only zip folders dated in Jan (1st-31st)..

following code should be used instead:
----------------------------------------------------
...
...
System.out.println("Dir = " + name + " , timestamp = " + mod);
// get a calendar corresponding to 4 months earlier
Calendar now_4 = Calendar.getInstance();
now_4.add(Calendar.MONTH,-4);
System.out.println(now_4);
// set a calendar corresponding to the folder timestamp date object
Calendar folderCal = Calendar.getInstance();
folderCal.setTime(modified);
int folderCalMonth = folderCal.get(Calendar.MONTH);
if( folderCalMonth == now_4.get(Calendar.MONTH) )
{
...
...
}
Tech Support
Site Admin
 
Posts: 1276
Joined: Thu May 25, 2006 11:12 am


Return to Task Sequences, Chains and Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

cron