Adding today date/time value into excel file using ant - ant

I'm using ant to get CSV files from an other ERP and send it into my SFDC system.
The batch will be every hour, so I used a script to move files to other dir each time a batch is finished
The issue that I want to rename those files to be the same name given but with date/time of NOW.
I found a rename script but I didn't managed to find any date time script to get the string value of date time - now

You can use <tstamp> to get the current date and time. Example:
<tstamp>
<format property="TODAY" pattern="yyyy-MM-dd" />
</tstamp>
<echo message="${TODAY}" />

Related

Upload files using the FTP Ant Task with a timestamp

I have the following code working. I've done some searching and can't find anything on how to associate a time stamp for when each file is uploaded. This would be helpful since I can get an idea on when a file fails during the upload process.
<ftp password="${password}" server="${server}" userid="${userid}" passive="true" remotedir="${remoteDir}">
<fileset dir="${localDir}">
<include name="**/*"/>
</fileset>
Basically, instead of this being produced in the log:
[ftp] File /DirectorytoUploadto/File.xml copied to server
I want to have something along the lines of:
[ftp] File /DirectorytoUploadto/File.xml copied to server at 2013-10-01 12:19:09
Does anyone know how to go about doing this?
To get a timestamp for each logged message, use the Log4J Listener.

ant Task for or foreach loop in xml files

I need some help on looping through an xml file which I manged to get the nodes using xmlproperty but I am struggling on how to loop through them where there are more than one params.
So here is the format:
<Problems>
<Problem>
<Rule>1</Rule>
<ProblemDescription>1</ProblemDescription>
<SourceFile>1</SourceFile>
<Line>1</Line>
<Column>1</Column>
<Severity>Warning</Severity>
</Problem>
<Problem>
<Rule>2</Rule>
<ProblemDescription>2</ProblemDescription>
<SourceFile>2</SourceFile>
<Line>2</Line>
<Column>2</Column>
<Severity>Warning</Severity>
</Problem>
</problems>
I want to loop through this so I can get the following output:
1
1
1
1
1
1
2
2
2
2
2
Solution:
<target>
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
<xmltask source="problem.xml">
<call path="/Problems/Problem">
<param name="rule" path="Rule/text()" />
<param name="probdesc" path="ProblemDescription/text()" />
<actions>
<echo>Rule: #{rule}</echo>
<echo>Problem Description: #{probdesc}</echo>
</actions>
</call>
</target>
You can use an XPATH expression to return everything that matches a given pattern. In your case, the value of certain tags.
Here is an XPATH Ant task.
Haven't used ANT in years, not since I switched to maven.
When I was using ant I would create a custom ant task for this sort of functionality. Then you have the full power of java and far more readable code, at the cost of having to compile the task if you need to make changes.
It really depends on what you are going to do with the output. The other answer with xpath is more appropriate if your doing something really simple.
See:
http://ant.apache.org/manual/develop.html
http://docs.oracle.com/javase/tutorial/jaxp/sax/parsing.html

Can I conditionally stop an Ant script based on file last modified time?

I've been looking all over the Internet, but couldn't find an answer anywhere. I have an MQFTE job coded in ANT script and I'm having difficulty with a process that moves files if a file doesn't have today's date. What I would like to do is a conditional stop, something like a return true value in a middle of the execution so the job is not going to go through the further routine and just end if the file is identified as to be skipped.
Is that possible in ANT? Or does it have to go through every <target> in the script?
The Ant fail task may be used to conditionally stop an Ant script. The example below initializes the property TODAY to the current date and then uses a fileset with a nested <date> element to only select files modified prior to today's date. The pathconvert task then sets the property files-not-empty only if there is at least one file in the fileset modified prior to today's date. The fail task is then used to stop the Ant script if there are no files to copy.
<target name="copy-if-not-modified-today">
<property name="copy-from.dir" value="${basedir}" />
<property name="copy-to.dir" value="${basedir}/build/copied_files" />
<mkdir dir="${copy-to.dir}" />
<tstamp>
<format property="TODAY" pattern="MM/dd/yyyy" />
</tstamp>
<fileset id="files" dir="${copy-from.dir}" includes="*">
<date datetime="${TODAY} 12:00 AM" when="before"/>
</fileset>
<pathconvert property="files-not-empty" setonempty="false" refid="files" />
<!--
Stop the Ant script if there are no files to copy that were modified prior
to today's date.
-->
<fail unless="files-not-empty" />
<copy todir="${copy-to.dir}" preservelastmodified="true">
<fileset refid="files" />
</copy>
</target>
Maybe Ant-Contrib Tasks can help you. To perform tasks based on conditions, the "If-then-else" is a possible solution - "Ant if"

Find all directories in which a file exists, such that the file contains a search string

I have a directory tree that I need to process as follows:
I have a certain file that needs to be copied to a select few sub directories
A sub directory of interest is one that contains a file within which I can regex match a known search string
Ideally I would like to:
Perform a regex match across all files within a directory
If the regex matches, copy the file to that directory
The trouble is that I am quite new to ANT and I'm having difficulties finding my way around. I can't find any tasks in the docs about per directory operations based on regex search. The closest thing I've found is a regex replace task (<replaceregexp>) that can search and replace patterns across files.
Is this even possible? I'd really appreciate a sample to get started with. I apologize for requesting code - I simply don't know how to begin composing the tasks together to achieve this.
Alternatively I have the option of hardcoding all the copy operations per directory, but it would mean manually keeping everything in sync as my project grows. Ideally I'd like to automate it based on the regex search/copy approach I described.
Thanks!
Your requirement is a bit non-standard, so I've solved it using a custom Groovy task.
Here's a working example:
<project name="find-files" default="copy-files">
<!--
======================
Groovy task dependency
======================
-->
<path id="build.path">
<pathelement location="jars/groovy-all-1.8.6.jar"/>
</path>
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<!--
=========================
Search for matching files
=========================
-->
<target name="search-files">
<fileset id="filesContainingSearchString" dir="src">
<include name="**/*.txt"/>
<containsregexp expression="[4-6]\.[0-9]"/>
</fileset>
</target>
<!--
===================================
Copy file into each directory found
===================================
-->
<target name="copy-files" depends="search-files">
<groovy>
project.references.filesContainingSearchString.each { file ->
def dir = new File(file.toString()).parent
ant.copy(file:"fileToBeCopied.txt", toDir:dir)
}
</groovy>
</target>
</project>
Notes:
Groovy jar can be downloaded from Maven Central
Use the copy task with a fileset and regular expression selector :
<copy todir="your/target/dir">
<fileset dir="rootdir/of/your/directorytree" includes="**/*.txt">
<containsregexp expression="[4-6]\.[0-9]"/>
</fileset>
</copy>
This example is taken from the ant manual and slightly adapted.
Means select all files with .txt extension anywhere beyond rootdir/of/your/directorytree that match the regular expression (have a 4,5 or 6 followed by a period and a number from 0 to 9) and copy them to your/target/dir.
Just adapt it for your needs.

ANT: Load file names and extract data from the file names

Hello I wasn't quite sure how to title this question, but I'll explain why I'm trying to do.
First of all, I have a folder that contains SQL scripts in a specific format, which is updateXtoY.sql, where X and Y are integers. What I need is to know which Y is the highest number. (basically, to know which is the latest script)
So if I have in my folder "scripts/" 3 files:
update3to5.sql
update2to5.sql
update1to6.sql
the result I need is to have assign a property 'latest.version' the value of 6.
From that point I can easily run the script. So the problem I have is 3-fold:
1- How to load the file names into a data structure.
2- How to iterate over the data structure.
3- How to evaluate each file name so that I can extract the "Y" part of the file and get the highest value. (I'm reading on regex right now)
I'm new to ANT and I'm not sure if this is possible and/or feasible.
Thanks for any suggestions.
The first part of the task - getting the filenames into a 'structure' is best done using a FileSet - say for SQL scripts in a directory called scripts:
<fileset dir="scripts" includes="*.sql" id="versions" />
That creates an Ant resource collection that can be referred to using the id versions.
The collection knows about your SQL script files.
Using (as you suggest) a regexp mapper, we can convert the set of files into a collection of strings, holding just the version parts from the filenames:
<mappedresources id="versions">
<fileset dir="scripts" includes="*.sql" />
<regexpmapper from="update.*to(.*).sql" to="\1" />
</mappedresources>
In this example versions now holds a 'list', which would be "5,5,6" for your example files.
It gets trickier now, because you probably need to carry out a numeric sort on what is a list of strings - to avoid 10 sorting as 'less' than 9. Ant ships with an embbeded Javascript interpreter, so you could use that to find the maximum. Another option would be to make use of the numeric sorting capability that ant-contrib has to offer.
Here's a Javascript 'max finder':
<scriptdef name="numeric_max" language="javascript">
<attribute name="property" />
<attribute name="resources_id" />
<![CDATA[
var iter = project.getReference(
attributes.get( "resources_id" )
).iterator( );
var max_n = 0.0;
while ( iter.hasNext() )
{
var n = parseFloat( iter.next() );
if ( n > max_n ) max_n = n;
}
project.setProperty( attributes.get( "property" ), max_n );
]]>
</scriptdef>
That defines a new Ant XML entity - numeric_max - that looks like a task, and can be used to find the numeric maximum of a collection of strings.
It's not perfect - there's no validation of the strings, and I've used floats rather than ints.
Combining that with the mappedresources above:
<mappedresources id="versions">
<fileset dir="scripts" includes="*.sql" />
<regexpmapper from="update.*to(.*).sql" to="\1" />
</mappedresources>
<numeric_max property="latest.version" resources_id="versions" />
<echo message="Latest SQL script version: ${latest.version}." />
When I run that with your three files I get:
[echo] Latest SQL script version: 6.

Resources