- Powerful, versatile, reliable and
affordable
-
Scripting features are available for
advanced users and programmers
- Run thousands of tasks a day
- Timely and Free technical support before and after you purchase
- Free 30-day trial and Free minor upgrades
- Runs on Windows , Mac OSX , Linux, and
other Unix platforms
------------------------------------------------------------------------------------------------------------
Scripting (Automize
Professional and
Automize Enterprise
Editions)
- Create complex automation scripts which build
upon existing Automize tasks
- Develop scripts that reduce repeated, laborious computer-related
activities.
- Add advanced functionality to your Automize tasks like email,
FTP,
monitors, file and directory lists.
- Use the Script Feature to get file lists or
filenames that you can use in copy , FTP , zip tasks etc..
- You can read and set the value of task parameters from the script. Then
run the task from the script.
This reduces the complexity of the script.
- Just 20-50 lines of code can result is extremely powerful customized
tasks and solutions.
- We provide free help for small 20-50 lines script
that can be usable to users.
- In-depth
tutorial and sample code to get you started.
- Our
online forum has many actual code examples based on real user
applications.
Example Script:
1) Script reads file list from file system
2) Script sets task parameters based on file name and folder
3) Script runs task. Task runs after reading task parameters set via
script.
4) Script reads task exit code and Task variables
5) Script can continue running task in same sequence 2-4 for all
filenames

-----------------------------------------------------------------------------------------------------------------------------------------------
Automize Java Scripting Tutorial
Automize uses BeanShell (beanshell.org)
for its scripting. BeanShell supports 100% java syntax, and Automize
itself is written in java. This is a great way to learn java syntax and
get started on the easiest and most powerful modern programming language.
To follow through in this tutorial, copy and paste these examples into the
Automize Script Window. Then run these examples:
Section 1 - Basics
Section 2 - Control Statements
Section 3 - Strings, Numbers
and Booleans
Section 4 - Arrays and Dates
Section 5 - File and Directory
Section 6 - Methods
Section 1 - Basics
Example: Introductory Script
// Lines starting with // are comment lines
// Most programming languages (c, java, c++, etc.. require a main() method)
// However, you do not need to define a main() method while scripting
// define a String
String test = "Welcome to java scripting!!";
// write to system stdout
System.out.println(test);
Example: Using the Script class
// The Script class provides a connection between your scripts and Hitek Software inbuilt functions
// import statements allow you to use existing Hitek Software or java functions
// In this example, we import the Hitek Software Script class
import com.hitek.engine.mods.script.Script;
// define a String
String test = "Welcome to Java Scripting!!";
// Call the Script.out method to write output to the output log file
Script.out(test);
// after you run/test this script, check the output log file (logs menu in Automize user interface)
Example: Integers and Strings
import com.hitek.engine.mods.script.Script;
//integers - you will frequently use int or long in your scripts
int largestInteger = Integer.MAX_VALUE;
long largestLong = Long.MAX_VALUE;
//boolean - you will also need to use boolean
boolean javaRocks = true;
//String - you will be using Strings a lot
String testString = "java strings are very usefull";
Script.out( "Maximum integer value = " + largestInteger);
Script.out( "Maximum long value = " + largestLong);
Script.out( "Value of javaRocks = " + javaRocks);
Script.out(testString);
//check the output log after you run this script.
Example: This example shows expressions which work with integers.
Check the output log after you run this script.
import com.hitek.engine.mods.script.Script;
int m = 10;
int k = 5;
Script.out( "Variable values...");
Script.out(" m = " + m);
Script.out(" k = " + k);
//using the increment operator ++
m++;
Script.out(" incremented using m++,new value of m = " + m);
//using > (greater than) < (less than)
if(m > k )
{
Script.out("Yes , " +m + " is bigger than " + k);
}
//using >= (greater than or equal) and <= (less than or equal)
if(m >= k +5)
{
Script.out("Yes , m is greater than or equal to k + 5 ");
}
//using the equality operator ==
if(k==5)
{
Script.out("Yes , k is equal to 5");
}
//using the Not Equals operator !=
if(k != 5)
{
Script.out("Yes , k is not equal to 5");
}
//using the && (logical AND)
if( (k == 5) && (m==10) )
{
Script.out("Yes , k is equal to 5, and m is equal to 10");
}
//using the || (logical OR)
if( (k == 5) || (m==234545) )
{
Script.out("Yes , either k is equal to 5, or m is equal to 10");
}
//check the output log after you run this script.
Example: This example shows basic integer manipulating operators
import com.hitek.engine.mods.script.Script;
//this example shows basic integer manipulating operators
//check the output log after you run this script.
int i = 10;
int k = 5;
Script.out( "Variable values...");
Script.out(" i = " + i);
Script.out(" k = " + k);
//adding numbers
Script.out("Adding...");
Script.out(" i + k = " + (i + k));
//subtracting numbers
Script.out("Subtracting...");
Script.out(" i - k = " + (i - k));
//multiplying numbers
Script.out("Multiplying...");
Script.out(" i * k = " + (i * k));
//dividing numbers
Script.out("Dividing...");
Script.out(" i / k = " + (i / k));
//check the output log after you run this script.
Section 2 - Control Statements
Back To Top
Example: if statements
import com.hitek.engine.mods.script.Script;
int temp = 100;
String weather = "cold";
// Simplest form of the if statement:
if ( temp > 80)
{
weather = "hot";
}
Script.out("The weather today is " + weather);
// (if + else) statments
temp = 60;
if( temp > 80)
{
weather = "hot";
}
else
{
weather = "nice";
}
Script.out("The weather today is " + weather);
// (If + ( else if ) + else) statements
temp = 75;
if( temp < 70)
{
weather = "cool";
}
else if( temp >= 70 && temp <= 80)
{
weather = "perfect";
}
else
{
weather = "hot";
}
Script.out("The weather today is " + weather);
//check the output log after you run this script.
Example: Using 'for' loops
import com.hitek.engine.mods.script.Script;
// define an integer and empty String
int sum = 0;
String result = "";
// use a 'for' loop between 1 and 10, increment sum by 1.
// ++ incrementing operator
// <= is less than or equal to
for (int current = 1; current <= 10; current++)
{
sum = sum + current;
result = "Current loop number = " + current + " , Sum = " + sum;
Script.out( result );
}
// after you run/test this script, check the output log.
Example: while loops
// while statement continually executes a block of statements while a condition is satisfied
// while loop evaluates the expression at the top of the loop
//BE VERY CAREFULL WITH WHILE LOOPS.
//Make sure that you increment a value to exit the loop at some point.
import com.hitek.engine.mods.script.Script;
int loop = 1;
int max = 7;
while (loop < max )
{
//WARNING - if you get stuck in an infinite loop you have to RESTART the engine / user interface
Script.out("This is a while loop. loop = " + loop);
//increment the value of loop otherwise this while loop will never terminate !!
loop++;
}
// do-while loop evaluates the expression at the bottom
//reset loop value to 1
loop = 1;
do
{
//WARNING - if you get stuck in an infinite loop you have to RESTART the engine / user interface
Script.out("This is a do-while loop. loop = " + loop);
//increment the value of loop otherwise this do-while loop will never terminate !!
loop++;
}
while (loop < max);
Example: The switch statement
//the switch statement conditionally performs statements based on an integer expression
import com.hitek.engine.mods.script.Script;
int dayNumber = 3;
String day = " ";
switch (dayNumber)
{
case 1: day="sunday";
break;
case 2: day="monday";
break;
case 3: day="tuesday";
break;
case 4: day="wednesday";
break;
case 5: day="thursday";
break;
case 6: day="friday";
break;
case 7: day="saturday";
break;
}
Script.out("Today is " + day);
Example: The continue statement
//the continue statement allows you to skip the current iteration of the for or while loop
import com.hitek.engine.mods.script.Script;
int max = 7;
//break out of a for loop
for (int i=1; i < 10 ; i++)
{
if ( i > 3 && i < 7)
{
continue;
}
Script.out( "This is a for loop with continue. iteration = " + i );
}
//continue and break out of an infinite while loop
int loop = 1;
while (true)
{
//WARNING - if you get stuck in an infinite loop you have to RESTART the engine / user interface
//increment the value of loop otherwise this while loop will never break out !!
//the increment should be done before the continue statement
loop++;
//break out else this while loop will never terminate!!
if(loop > max)
{
break;
}
//continue on to next iteration for iterations 1 and 2
if(loop < 4)
{
continue;
}
Script.out("This is a while loop with contine for iterations < 3. iteration = " + loop);
//You cannot have the loop increment after the continue.
// !! loop ++; !! loop ++ should not be here. It should be located before the continue.
}
Script.out( "Break out of while loop after loops > " + max );
Example: The break statement
//the break statement allows you to break out of loops.
import com.hitek.engine.mods.script.Script;
int max = 7;
//break out of a for loop
for (int i=1; i < 10 ; i++)
{
//WARNING - if you get stuck in an infinite loop you have to RESTART the engine / user interface
Script.out( "This is a for loop. loop = " + i );
if ( i >= max )
{
break;
}
}
Script.out( "Break out of for loop after loops > " + max );
//break out of an infinite while loop
int loop = 1;
while (true)
{
//WARNING - if you get stuck in an infinite loop you have to RESTART the engine / user interface
Script.out("This is a while loop. loop = " + loop);
//increment the value of loop otherwise this while loop will never terminate !!
loop++;
//break out else this while loop will never terminate!!
if(loop > max)
{
break;
}
}
Script.out( "Break out of while loop after loops > " + max );
Section 3 - Strings, Numbers
and Booleans
Back To Top
Example: Java String class functions
// The java String class allows you to get information, search, manipulate, convert, and output Strings
import com.hitek.engine.mods.script.Script;
// define a String
String str = "My first name is John. My last name is Doe.";
// print them out
Script.out( "The test String is: " + str );
// get String length
Script.out( "The length of the String str is : " + str.length() );
// get character at index 8
Script.out( "The character at index 9 is : " + str.charAt(9) );
// find the first occurance of a sequence in the String and print the index
Script.out( "The sequence 'name' was first found at index :" + str.indexOf("name") );
// if the sequence is not found, the return index is -1
// java String searches and other operations are case sensitive by default
Script.out( "The sequence 'NAME' was found at index :" + str.indexOf("NAME") );
// find the first occurance of a sequence in the String, beginning the search at index 15
Script.out( "The sequence 'name' was also found at index :" + str.indexOf("name", 15) );
// find the last occurance of a sequence in the String and print the index
Script.out( "The sequence 'name' was last found at index :" + str.lastIndexOf("name") );
// check if the String starts with a certain sequence.
// startsWith method returns a boolean true if the String starts with sequence entered.
if ( str.startsWith( "My" ) == true )
{
Script.out( "Yes.. the test String starts with 'My' " );
}
// check if the String ends with a certain sequence.
// startsWith method returns a boolean true if the String ends with sequence entered.
if ( str.endsWith( "Doe." ) == true )
{
Script.out( "Yes.. the test String ends with 'Doe.' " );
}
// check if the String equals another string
String anotherString = "My first name is John. My last name is Doe.";
Script.out( "anotherString = " + anotherString );
if( str.equals( anotherString ) )
{
Script.out( "Yes.. the two string are equal " );
}
// check if the String equals another string ignoring case
String lowerCaseString = "my first name is john. my last name is doe.";
Script.out( "lowerCaseString = " + lowerCaseString );
if( str.equalsIgnoreCase( lowerCaseString ) )
{
Script.out( "Yes.. the two string are equal ignoring case" );
}
Example: String manipulation
// String manipulation examples
import com.hitek.engine.mods.script.Script;
// define some Strings
String str = "My first name is John. My last name is Doe.";
String str2 = " I like cake.";
// print it out
Script.out( "The test String is: " + str );
// concatenate (append) another string to this string
String str3 = str.concat(str2);
Script.out( "Concatenating str2 to str1. Result : " + str3 );
// replace the first occurance of a String (or Regular expression pattern) with another String
str3 = str.replaceFirst( "is" , "was" );
Script.out( "Replacing first occurance of 'is' with 'was'. Result : " + str3 );
// replace all occurances of a String (or regular expression pattern) with another String
str3 = str.replaceAll( "My" , "His" );
Script.out( "Replacing all occurances of 'My' with 'His'. Result : " + str3 );
// get a substring beginning at specified index
str3 = str.substring(8);
Script.out( "Substring begining at index 8 = " + str3 );
// get a substring contained within specified indices
str3 = str.substring(8,30);
Script.out( "Substring beggining at index 8 and ending at index 30 = " + str3 );
// convert String to lower case
str3 = str.toLowerCase();
Script.out( "Converting String to lower case = " + str3 );
// convert String to upper case
str3 = str.toUpperCase();
Script.out( "Converting String to upper case = " + str3 );
Example: java numbers
// The java number primitive types include int , long, float, double, short, byte
// You will mostly require int and long. Sometimes you may need to use float.
// Each of these primitive types have Java Classes that wrap around them.
// The class Integer wraps around int . The class Long wraps around long.
import com.hitek.engine.mods.script.Script;
// define some numbers
int i = 3;
long l = 123456787;
float f = 25.45;
Integer I = new Integer(i);
Float F = new Float(f);
Long L = new Long(l);
// print out the int and Integer
Script.out( " The int i is : " + i );
Script.out( " The wrapper class Integer is : " + I.toString() );
// print out the long and Long
Script.out( " The long l is : " + l );
Script.out( " The wrapper class Long is : " + L.toString() );
// print out the float and Float
Script.out( " The float f is : " + f );
Script.out( " The wrapper class Float is : " + F.toString() );
Example: convert string values to numbers
// You will frequently need to convert string values to numbers
import com.hitek.engine.mods.script.Script;
// define some Strings.
String intString = "25";
String longString = "123456789";
String floatString = "25.567";
int i = Integer.valueOf(intString);
// print out the int
Script.out( " The int i is : " + i );
long l = Long.valueOf(longString);
// print out the long
Script.out( " The long l is : " + l );
float f = Float.valueOf(floatString);
// print out the float
Script.out( " The float f is : " + f );
Example: java Math class
// The java Math class provides more math functions, other than +, - , / or *
import com.hitek.engine.mods.script.Script;
// define some numbers
int i2 = 3;
int i3 = -7;
long l = 123456787;
float f = 25.45;
double d2 = 25.34;
double d3 = 2.4;
// print out the maximum of two numbers
Script.out( " The maximum between i2 and i3 is : " + Math.max(i2,i3 ) );
// print out the minimum of two numbers
Script.out( " The minimum between i2 and i3 is : " + Math.min(i2,i3 ) );
// round off the float
Script.out( " 25.45 is rounded off to : " + Math.round( f ) );
// get exp value
Script.out( " exp(3) = " + Math.exp( i2 ) );
// get log value
Script.out( " log(3) = " + Math.log( i2 ) );
// get square root value
Script.out( " square root of 3 = " + Math.sqrt( i2 ) );
Example: The Boolean class
// The Boolean class wraps around the java primitive type boolean
// The primitive boolean can be either 'true' or 'false' (case sensitive)
import com.hitek.engine.mods.script.Script;
// define some booleans
boolean trueBoolean = true;
boolean falseBoolean = false;
// convert the boolean to string and print out the boolean
Script.out( "trueBoolean is : " + trueBoolean.toString() );
Script.out( "falseBoolean is : " + falseBoolean.toString() );
// use the Boolean class to convert a String to boolean
String trueString = "true";
Boolean test = Boolean.valueOf(trueString);
boolean convertedBoolean = test.booleanValue();
Script.out( "Converting String 'true' to boolean type " );
Script.out( "Converted boolean value = " + convertedBoolean.toString() );
Section 4 - Arrays and Dates
Back To Top
Example: Arrays
// Arrays will be required frequently to process directory listing and other uses.
import com.hitek.engine.mods.script.Script;
// declare an intetger array
int[] arr;
// create the integer array
arr = new int[10];
// assign a value to each array element and print it out
// the field 'length' returns the size of the array
//
for (int i = 0; i < arr.length; i++)
{
arr[i] = i*2;
Script.out( " the array element at index : " + i + " is : " + arr[i] );
}
// example of using String array
String[] str = { "one is 1", "two is 2", "three is 3" };
for (int i = 0; i < str.length; i++)
{
Script.out( " the array element at index : " + i + " is : " + str[i] );
}
// 2-D arrays
String[][] names =
{
{ "bill", "bob", "betty"},
{ "mark", "matt",},
{ "karen", "kitty", "kevin", "kong", },
};
for (int i = 0; i < names.length; i++)
{
Script.out("Start of 1-D subarray of names. Row = " + i);
for (int j = 0; j < names[i].length; j++)
{
Script.out(names[i][j] + " ");
}
Script.out("End of 1-D subarray of names. Row = " + i);
}
Example: vectors
// A vector is an array that can grow or shrink dynamically
// A vector can contain any type of Object or primitive java type
import com.hitek.engine.mods.script.Script;
// create a blank arraylist
Vector v = new Vector();
// add some elements to it
v.add(0);
v.add("first string");
v.add("second string");
v.add(3);
v.add(new Date());
// print out the vector elements
for (int i = 0; i < v.size() ; i++)
{
Script.out( " the array element at index : " + i + " is : " + v.get(i).toString() );
}
// print out the size of vector
Script.out( " The vector size is : " + v.size() );
// remove an element at index 3
v.remove(3);
// print out the size of vector again
Script.out( " Removed element at index 3. The new vector size is : " + v.size() );
//insert element into index 3 again
v.add(3, "Mr. Insert");
//print out element 3
Script.out( " Inserted element at index 3 is : " + v.get(3).toString() );
// search the vector for objects
if(v.contains("Mr. Insert"))
{
Script.out( " Yes, this vector contains the element 'Mr. Insert' " );
}
// print out the index of 'Mr. Insert'
Script.out( " The index of Mr. Insert is : " + v.indexOf("Mr. Insert") );
// clear out the array
v.clear();
// print out the size of vector
Script.out( " Vector cleared. The vector size is : " + v.size() );
Example: Date and DateFormat classes
// Date and DateFormat classes are useful in formatting, parsing and modifying file timestamps
// Calendar classes help perform Date manipulations and comparisons.
import com.hitek.engine.mods.script.Script;
import java.text.DateFormat;
// get the current Date
Date now = new Date();
// print out the date using a default formatter
Script.out("Current default format Date / Time is : " + now.toString() );
// create a custom Date formatter showing short Date and short Time formats
// you have to import the java.text.DateFormat class at the top of the script
// java.text.DateFormat is not automatically imported by the script.
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
// print out the Date using this custom short formatter
Script.out("Current SHORT format Date / Time is : " + df.format(now) );
// switch the format to long date and time
df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
Script.out("Current LONG Date / Time is : " + df.format(now) );
// lets calculate elapsed time in Milliseconds
// the getTime() method returns the total milliseconds elapsed since the start of computer time (~1970)
long startMillis = now.getTime();
Script.out("start time in millis : " + startMillis );
// lets recalculate the Date
now = new Date();
long endMillis = now.getTime();
Script.out("end time in millis : " + endMillis );
//calculate the elapsed time in millis
long diff = endMillis - startMillis;
Script.out("elaspsed time in millis : " + diff );
// we can also convert a time in milliseconds to a Date
Date endDate = new Date(endMillis);
Script.out("end time in Long format : " + df.format( endDate ) );
Example: Parsing Date
// Parsing Date from Strings
import com.hitek.engine.mods.script.Script;
import java.text.DateFormat;
// Define a Date String
String shortDateString = "4/11/06 6:53 PM";
String longDateString = "April 11, 2006 6:53:27 PM PDT";
// define short date format
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
// parse the short date in
Date shortDate = df.parse(shortDateString);
// print out the short date using a default formatter
Script.out(" shortDate was parsed correctly. short date : " + shortDate.toString() );
// switch the formatter to long date format
df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
// parse in the long date
Date longDate = df.parse(longDateString);
// print out the long date using a default formatter
Script.out(" longDate was parsed correctly. long date : " + longDate.toString() );
Example: Calendar class
// Calendar class allows us to manipulate a date or compare dates
import com.hitek.engine.mods.script.Script;
//import Date and Calendar classes which are located in the java.util package
import java.util.*; //
// get the current Calendar
Calendar now = Calendar.getInstance();
// print out the calendar information.
Script.out("Current calendar is : " + now.toString() );
// convert the Calendar to a Date object using the Calender getTime() function
Date dateNow = now.getTime();
// print out the date information
Script.out("Date obtained from Calendar is : " + dateNow.toString() );
// convert a date to a calendar using the Calendar setTime(Date) function
now.setTime(dateNow);
//print out the calendar information again
Script.out("Calendar obtained from date is : " + now.toString() );
// get the individual fields of the Calendar
Script.out("The year is: " + now.get(Calendar.YEAR) );
Script.out("The month is: " + now.get(Calendar.MONTH) );
Script.out("The date is: " + now.get(Calendar.DATE) );
Script.out("The hour (0-12) is: " + now.get(Calendar.HOUR) );
Script.out("The hour of the day (0-23) is: " + now.get(Calendar.HOUR_OF_DAY) );
Script.out("The minute is: " + now.get(Calendar.MINUTE) );
Script.out("The second is: " + now.get(Calendar.SECOND) );
Script.out("The millisecond is: " + now.get(Calendar.MILLISECOND) );
Script.out("The AM / PM value is: " + now.get(Calendar.AM_PM) );
Script.out("The day of week is: " + now.get(Calendar.DAY_OF_WEEK) );
Script.out("The week of month is: " + now.get(Calendar.WEEK_OF_MONTH) );
Script.out("The day of year is: " + now.get(Calendar.DAY_OF_YEAR) );
Script.out("The day of week in month is: " + now.get(Calendar.DAY_OF_WEEK_IN_MONTH) );
Script.out("The day of month is: " + now.get(Calendar.DAY_OF_MONTH) );
Script.out("The week of year is: " + now.get(Calendar.DAY_OF_YEAR) );
// add some fields to the now calendar. Adding automatically rolls over the bigger fields for you
Calendar yesterday = Calendar.getInstance();
yesterday.add(Calendar.DATE, -1);
Calendar tomorrow = Calendar.getInstance();
tomorrow.add(Calendar.DATE, 1);
//print out the dates corresponding to these calendars. First convert to date objects before printing
Script.out(" Yesterday was : " + yesterday.getTime().toString() );
Script.out(" Tomorrow is : " + tomorrow.getTime().toString() );
// compare yesterday and tommorrow using the Calendar after and before methods
if(yesterday.before(tomorrow))
{
Script.out("Yes, yesterday is before tomorrow");
}
if(tomorrow.after(yesterday))
{
Script.out("Yes, tomorrow is after yesterday");
}
Section 5 - File and Directory
Back To Top
Example: directory and file information
// In java , directory and file information is obtained from the java File class
import com.hitek.engine.mods.script.Script;
import java.text.DateFormat;
// get the default java temporary directory
String tmpdir = System.getProperty("java.io.tmpdir");
// print out the directory name
Script.out( " The default java temp folder is : " + tmpdir );
// create a new File object of the directory
File dir = new File(tmpdir);
// create a new file object
File newFile = new File(dir,"newFile.txt");
// create the new file
boolean success = newFile.createNewFile();
//print out file information
if(success == true)
{
Script.out( " Created new file " );
}
else
{
Script.out( " Failed to create file or file already exists " );
}
// verify that file path exists and is a file
if ( newFile.exists() && newFile.isFile() )
{
Script.out( " file exists and is a file" );
}
else
{
Script.out( " file does not exists, or is not a file " );
return; // no point continuing if there is no file
}
// print out the files information
Script.out( " File name = " + newFile.getName() );
Script.out( " File Size = " + newFile.length() );
Script.out( " File path = " + newFile.getPath() );
Script.out( " File absolute path = " + newFile.getAbsolutePath() );
Script.out( " File canonical path = " + newFile.getCanonicalPath() );
Script.out( " Files directory = " + newFile.getParent() );
Script.out( " File is readable = " + newFile.canRead() );
Script.out( " File is writeable = " + newFile.canWrite() );
Script.out( " File is hidden = " + newFile.isHidden() );
DateFormat df = DateFormat.getDateTimeInstance();
long modtime = newFile.lastModified();
Date mod = new Date(modtime);
Script.out( " File date = " + df.format(mod) );
Example: file methods
import com.hitek.engine.mods.script.Script;
import java.text.DateFormat;
// get the default java temporary directory
String tmpdir = System.getProperty("java.io.tmpdir");
// print out the directory name
Script.out( " The default java temp folder is : " + tmpdir );
// create a new File object of the directory
File dir = new File(tmpdir);
// create a new file object
File file1 = new File(dir,"file1.txt");
File file2 = new File(dir,"file2.txt");
// create the new file 1
file1.createNewFile();
// verify that newfile1 exists and is a file
if ( file1.exists() && file1.isFile() )
{
Script.out( " file1 exists" );
}
else
{
Script.out( " file1 does not exists, or is not a file " );
return; // no point continuing if there is no file
}
//rename the file
boolean success = file1.renameTo(file2);
if(success == true)
{
Script.out( " rename from file1 to file2 was successfull " );
}
//confirm that newFile1 does not exist
if ( !file1.exists() )
{
Script.out( " file1 does not exists, because it was renamed " );
}
// delete newFile2
success = file2.delete();
if(success == true)
{
Script.out( " file2 delete was successfull " );
}
Example: file read / write
// file read / write
import com.hitek.engine.mods.script.Script;
// get the default java temporary directory
String tmpdir = System.getProperty("java.io.tmpdir");
// print out the directory name
Script.out( " The default java temp folder is : " + tmpdir );
// create a new File object of the directory
File dir = new File(tmpdir);
// create a new file object
File file1 = new File(dir,"file1.txt");
//create FileWriter to write to file1 , the false is to overwrite the file, true is to append to end of existing file
FileWriter fw = new FileWriter(file1,false);
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < 10; i++)
{
bw.write("This is line " + i );
// write a new line character
bw.newLine();
}
bw.close();
// create a reader to read in the whole file and print it out to output log
FileReader fr = new FileReader(file1);
BufferedReader br = new BufferedReader(fr);
String line = "";
while ( ( line = br.readLine() ) != null)
{
Script.out( line );
}
br.close();
Example: directory methods
// In java , directory and file information is obtained from the java File class
import com.hitek.engine.mods.script.Script;
// get the default java temporary directory
String tmpdir = System.getProperty("java.io.tmpdir");
// print out the directory name
Script.out( " The default java temp folder is : " + tmpdir );
// create a new File object of the directory
File dir = new File(tmpdir);
// confirm that this path exists on your system
if (dir.exists())
{
Script.out( "Yes, this path exists" );
}
// confirm that this is a valid directory on your system
if (dir.isDirectory())
{
Script.out( "Yes, this is a valid directory" );
}
// get the path
Script.out( "The path is : " + dir.getPath());
// get the absolute path
Script.out( "The absolute path is : " + dir.getAbsolutePath());
// get the parent directory
File parent = dir.getParentFile();
Script.out( "The parent path is : " + parent.getAbsolutePath());
Example: directory information
// In java , directory and file information is obtained from the java File
class
import com.hitek.engine.mods.script.Script;
import java.text.DateFormat;
// get the default java temporary directory
String tmpdir = System.getProperty("java.io.tmpdir");
// print out the directory name
Script.out( " The default java temp folder is : " + tmpdir );
// create a new File object of the directory
File dir = new File(tmpdir);
// get the list of file names in the dir
String[] names = dir.list();
// print out the first 100 names of all the files in your temp dir
int max = names.length;
if(max > 100)
{
max = 100;
}
for (int i=0; i < max ; i++)
{
Script.out( "File " + i + " = " + names[i] );
}
// get an array of file objects
File[] files = dir.listFiles();
// print out the information for the first 100 files.
String name = "";
long size = 0;
Date modified;
// get default date time formatter
DateFormat df = DateFormat.getDateTimeInstance();
String mod = "";
for (int i=0; i < max ; i++)
{
String name = files[i].getName();
size = files[i].length();
modified = new Date(files[i].lastModified());
mod = df.format(modified);
Script.out( "File " + i + " = " + name + " , size = " + size + " , timestamp = " + mod );
}
Example: make folders
// example to make folders
import com.hitek.engine.mods.script.Script;
// get the default java temporary directory
String tmpdir = System.getProperty("java.io.tmpdir");
// print out the directory name
Script.out( " The default java temp folder is : " + tmpdir );
// create a new File object of the directory
File dir = new File(tmpdir);
// create a file object representing a new subfolder within tmpdir
File newdir = new File(dir,"newDirectory2");
// check if folder exists
boolean exists = newdir.exists();
if (exists == true)
{
Script.out( " folder already exists : " + newdir.getAbsolutePath() );
return; // no need to create subfolder
}
// make a new subdirectory. method returns true if it suceeds
// returns false if folder already exists, or could not create folder
boolean success = newdir.mkdir();
if (success == true)
{
Script.out( " created new folder : " + newdir.getAbsolutePath() );
}
Section 6 - Methods
Back To Top
Example: using methods
// you can define methods to reuse code in your scripts
// all methods MUST be defined before the main script is executed
import com.hitek.engine.mods.script.Script;
// this method simply outputs the String message to the output log
void out(String message)
{
Script.out(message);
}
// this method gets the square of a number
int square(int input)
{
return input*input;
}
// main method starts now
// test the above methods
out("This is a test of using methods in scripts");
int fiveSquare = square(5);
out("five square is " + fiveSquare);
Example: hitek methods
// we provide some methods which allow you to run tasks, log data, get and set variables and task parameters.
import com.hitek.engine.mods.script.Script;
// write a string to the output log
Script.out("Writing to the output log");
// write a string to the activity log
Script.act("Writing to the activity log");
// write an error stack trace to the debug log
try
{
Integer.parseInt("bad integer");
}
catch(Exception e)
{
Script.err(e);
}
// run a task and get the exit code
// make sure you enter a valid task title here
String taskTitle = "Enter_valid_task_title_here";
taskTitle = "Copy";
int exitCode = Script.run( taskTitle );
Script.out("Exit Code of task = " + exitCode );
// get a task paramter. You need to enter the correct taskTitle and parameter key
// look at the property files of the tasks in the data\tasks folder.
// these files show the parameter keys from Task.PARAMETERS0 to Task.PARAMETERS50
// read Task.PARAMETERS1
String par = Script.getPar( taskTitle, "Task.PARAMETERS1");
Script.out("Task.PARAMETERS1 = " + par );
// set a task parameter dynamically
Script.setPar( taskTitle, "Task.PARAMETERS1", "new test value");
// again read it out to confirm that changes were made
par = Script.getPar( taskTitle, "Task.PARAMETERS1");
Script.out("Task.PARAMETERS1 = " + par );
// you can get all task, user, java, and system variable values
// you can set user variable values. We do not allow task, java or system variable values to be overwritten.
String var = taskTitle + "::ExitCode";
String value = Script.getVar(var);
Script.out("Variable value = " + value );
// set a user variable value
Script.setVar("newVariable", "variable testing" );
value = Script.getVar("newVariable");
Script.out("New Variable value = " + value );
Back
To Top