hiteksoftware.com Forum Index hiteksoftware.com
User discussion forum
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Script: Use file timestamp string in other tasks

 
Post new topic   Reply to topic    hiteksoftware.com Forum Index -> Task Sequences, Chains and Scripts
View previous topic :: View next topic  
Author Message
Robert
Site Admin


Joined: 25 May 2006
Posts: 1140

PostPosted: Mon Sep 25, 2006 11:24 am    Post subject: Script: Use file timestamp string in other tasks Reply with quote

User support request:

Once I have the files downloaded (they are always ZIP files) I need to archive the ZIP file away and append the create date/time of the ZIP file to the name. I then need to unzip the contents of each to a specific location and append to the files contained within the zip the create date/time of the zip itself. I can do this easily with the current system time, however, I can't see how I can reference the create time of the individual files.
Back to top
View user's profile Send private message
Robert
Site Admin


Joined: 25 May 2006
Posts: 1140

PostPosted: Mon Sep 25, 2006 11:33 am    Post subject: Reply with quote

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

// you should create an Unzip task with the task title = unzip
// the source folder for the unzip task whould be c:\test

//you should also create a copy task (title = copy) to append the timestamp to each unzippped file

//You should create a second copy task (titel = copy2) to append timestamp to zip archive

//get the list of zip files in the source folder
File dir = new File("c:\\test");
File[] list = dir.listFiles();

// now for each zip file in the list, loop through, get timestamp and run the tasks
for (int i=0; i<list.length ; i++)
{
//only process files - ignore folders
if(list[i].isFile()==false) continue;

//only process files ending in .zip
String name = list[i].getName();
if(name.endsWith(".zip")==false) continue;

//create a Date object and set it to the file timestamp using list[].lastModified()
Date zipTimestamp = new Date();
zipTimestamp.setTime(list[i].lastModified());

// now format the Date object into a usable string
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
String timestamp = df.format(zipTimestamp);
timestamp = timestamp.replace(':','_');
timestamp = timestamp.replace('/','_');

//look at the task properties file in the Automize7\data\tasks folder using wordpad
//the parameter for the unzip tasks - zip filename is Task.PARAMETERS2
//now set the zip filename field in the unzip task
Script.setPar("unzip", "Task.PARAMETERS2", name);

// run the unzip task
// exit code = 0 means successfull.
int exitCode = Script.run( "unzip" );
System.out.println("Unzip task Exit code = " + Integer.toString(exitCode));

// now set the append to filename parameter for the copy task
// the filename field should be blank to copy all files
// the parameter for the append to filename field is Task.PARAMETERS4
Script.setPar("copy", "Task.PARAMETERS4", timestamp);

//run the copy task to append timestamp to each unzipped file
int exitCode = Script.run( "copy" );
System.out.println("Copy task Exit code = " + Integer.toString(exitCode));

// now set the append to filename parameter for the copy2 task
// the parameter for the append to filename field is Task.PARAMETERS4
Script.setPar("copy2", "Task.PARAMETERS4", timestamp);

//set the source filename (Task.PARAMETERS3) to be the zip archive name
Script.setPar("copy2", "Task.PARAMETERS3", name);

//run the copy task to append timestamp to zip archive
int exitCode = Script.run( "copy2" );
System.out.println("Copy2 task Exit code = " + Integer.toString(exitCode));
}
Back to top
View user's profile Send private message
Robert
Site Admin


Joined: 25 May 2006
Posts: 1140

PostPosted: Wed Oct 04, 2006 12:51 pm    Post subject: Reply with quote

User Reply:

Many thanks for this.

I've almost managed to get this working. However, my current issue is that the appended data is after the file extension for both the copied zip and the extracted file.

I'm sure the following lines could be amended to insert the date element before the extension:

Script.setPar("copy", "Task.PARAMETERS4", timestamp);
Script.setPar("copy2", "Task.PARAMETERS4", timestamp);

I would also ideally like the date extensions to be in international format, e.g., 2nd July 2006 is "20060702" as this makes chronological sorting easier. I will continue to "play", however, if you could offer any assistance then great.


Last edited by Robert on Wed Oct 04, 2006 1:01 pm; edited 1 time in total
Back to top
View user's profile Send private message
Robert
Site Admin


Joined: 25 May 2006
Posts: 1140

PostPosted: Wed Oct 04, 2006 12:52 pm    Post subject: Reply with quote

To append the timestamp before the extension, use the P2:: syntax (see the help file on filename format or append to filename format)
i.e.
Script.setPar("copy", "Task.PARAMETERS4", "P2::" + timestamp);
Script.setPar("copy2", "Task.PARAMETERS4", "P2::" + timestamp);
Back to top
View user's profile Send private message
Robert
Site Admin


Joined: 25 May 2006
Posts: 1140

PostPosted: Wed Oct 04, 2006 12:57 pm    Post subject: Reply with quote

To change the format of the timestamp, you would need to use the java Calendar as follows:

1) At the top of the Script add this line to support the Calendar class
import java.util.*;

2) Use the following code snippet in the correct location in the script above:

// now format the Date object into a usable string
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
String timestamp = df.format(zipTimestamp);
timestamp = timestamp.replace(':','_');
timestamp = timestamp.replace('/','_');

System.out.println("default timestamp = " + timestamp);

Calendar timecal = Calendar.getInstance();
timecal.setTime(zipTimestamp);

String year = Integer.toString( timecal.get(Calendar.YEAR) );
String month = Integer.toString( timecal.get(Calendar.MONTH) );
if(month.length()==1) month = "0" + month; // use this line if you need 2 digit month
String date = Integer.toString( timecal.get(Calendar.DATE) );
if(date.length()==1) date = "0" + date;
String hour12 = Integer.toString( timecal.get(Calendar.HOUR) );
if(hour12.length()==1) hour12 = "0" + hour12;
String hour24 = Integer.toString( timecal.get(Calendar.HOUR_OF_DAY) );
if(hour24.length()==1) hour24 = "0" + hour24;
String minute = Integer.toString( timecal.get(Calendar.MINUTE) );
if(minute.length()==1) minute = "0" + minute;

String customTimestamp = year + month + date + "-" + hour24 + minute;
System.out.println("Custom timestamp = " + customTimestamp);


3) use the customTimestamp instead of timeStamp:

Script.setPar("copy", "Task.PARAMETERS4", "P2::" + customTimestamp);
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    hiteksoftware.com Forum Index -> Task Sequences, Chains and Scripts All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group