You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by ck...@onebox.com on 2007/07/11 21:21:12 UTC

How can I capture a file's date to a property?

I'm writing a target that will update a file. I'd like to preserve the exisiting version of the file by renaming it and incorporating its date in the file name.

I've have found several tasks for copying and renaming files and directories, but no way to get the file's date-stamp. Am I simply not seeing it, or does ant really have no means of capturing a file's last modified date (oh, the platform is Windows)?

-- 
Charles Knell
cknell@onebox.com - email

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: How can I capture a file's date to a property?

Posted by Dale Anson <da...@grafidog.com>.
Hi Gilbert,

Your example looks just right.  That should work with no problems.

Dale


Rebhan, Gilbert wrote:
>  
> Hi,
>
> if you want/need to go a more ' Antlike ' way =
>
> i forgot there's another possibility with the <stringutils>
> task from Antelope
>
> "...
> lastmodified Get the "last modified" date/timestamp of a file.
> ..."
>
> so it gets something like (untested)
>
> <fileutil file="foobar.txt" property="moddate">
>   <lastmodified format="...">
> </fileutils>
>
> when ${moddate} contains your File.lastModified
>
> i never tried that task, but the Antelope task suite is great,
> recommended.
>
> get it here =
> http://antelope.tigris.org/files/documents/1409/11489/AntelopeTasks_3.4.
> 2.zip
>
> Regards, Gilbert
>
>
> -----Original Message-----
> From: Rebhan, Gilbert [mailto:Gilbert.Rebhan@huk-coburg.de] 
> Sent: Thursday, July 12, 2007 11:02 AM
> To: Ant Users List
> Subject: RE: How can I capture a file's date to a property?
>
>
> Hi,
>
> -----Original Message-----
> From: cknell@onebox.com [mailto:cknell@onebox.com] 
> Sent: Wednesday, July 11, 2007 9:21 PM
> To: user@ant.apache.org
> Subject: How can I capture a file's date to a property?
>
> /*
> I've have found several tasks for copying and renaming files and
> directories, but no way to get the file's date-stamp.
> */
>
> i would go via <script> i.e.
>
> with jruby =
>
> <project name="bla" default="main" basedir=".">
>   <target name="depends">
>   <scriptdef name="filemod" language="ruby">
>    <attribute name="fname"/>
>    <attribute name="prop"/>
>    <![CDATA[
>     attr = $bsf.lookupBean("attributes")
>     fname = attr.get("fname")
>     prop = attr.get("prop")
>     t=File.atime(fname)
>     $project.setNewProperty "filename", fname
>      $project.setNewProperty prop, t.strftime("%m.%d.%Y")
>    ]]>
>    </scriptdef>
>
>  <filemod fname="y:/test.txt"
>           prop="fmod"/>
>     
>   </target>
>
>   <target name="main" depends="depends">
>     <echo>
>       ${filename} last modified => ${fmod}
>     </echo>
>   </target>
> </project>
>
> see other time formatting possibilities =
>  http://www.ruby.ch/ProgrammingRuby/htmlC/ref_c_time.html#strftime
>
> or with javascript =
>
> <scriptdef name="filemod" language="javascript">
>  <attribute name="fname"/>
> <attribute name="prop"/>
> <![CDATA[
>   fname = attributes.get("fname");
>   prop = attributes.get("prop");
>   f = new java.io.File(fname);
>   date = new Date(f.lastModified());
>   project.setNewProperty("filename", f);
>   project.setNewProperty(prop, date);
> ]]>
> </scriptdef>
>
> have a look at java.util.Date apidocs if
> you need other time formatting
>
>
> Regards, Gilbert
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>   

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: How can I capture a file's date to a property?

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.
 
Hi,

if you want/need to go a more ' Antlike ' way =

i forgot there's another possibility with the <stringutils>
task from Antelope

"...
lastmodified Get the "last modified" date/timestamp of a file.
..."

so it gets something like (untested)

<fileutil file="foobar.txt" property="moddate">
  <lastmodified format="...">
</fileutils>

when ${moddate} contains your File.lastModified

i never tried that task, but the Antelope task suite is great,
recommended.

get it here =
http://antelope.tigris.org/files/documents/1409/11489/AntelopeTasks_3.4.
2.zip

Regards, Gilbert


-----Original Message-----
From: Rebhan, Gilbert [mailto:Gilbert.Rebhan@huk-coburg.de] 
Sent: Thursday, July 12, 2007 11:02 AM
To: Ant Users List
Subject: RE: How can I capture a file's date to a property?


Hi,

-----Original Message-----
From: cknell@onebox.com [mailto:cknell@onebox.com] 
Sent: Wednesday, July 11, 2007 9:21 PM
To: user@ant.apache.org
Subject: How can I capture a file's date to a property?

/*
I've have found several tasks for copying and renaming files and
directories, but no way to get the file's date-stamp.
*/

i would go via <script> i.e.

with jruby =

<project name="bla" default="main" basedir=".">
  <target name="depends">
  <scriptdef name="filemod" language="ruby">
   <attribute name="fname"/>
   <attribute name="prop"/>
   <![CDATA[
    attr = $bsf.lookupBean("attributes")
    fname = attr.get("fname")
    prop = attr.get("prop")
    t=File.atime(fname)
    $project.setNewProperty "filename", fname
     $project.setNewProperty prop, t.strftime("%m.%d.%Y")
   ]]>
   </scriptdef>

 <filemod fname="y:/test.txt"
          prop="fmod"/>
    
  </target>

  <target name="main" depends="depends">
    <echo>
      ${filename} last modified => ${fmod}
    </echo>
  </target>
</project>

see other time formatting possibilities =
 http://www.ruby.ch/ProgrammingRuby/htmlC/ref_c_time.html#strftime

or with javascript =

<scriptdef name="filemod" language="javascript">
 <attribute name="fname"/>
<attribute name="prop"/>
<![CDATA[
  fname = attributes.get("fname");
  prop = attributes.get("prop");
  f = new java.io.File(fname);
  date = new Date(f.lastModified());
  project.setNewProperty("filename", f);
  project.setNewProperty(prop, date);
]]>
</scriptdef>

have a look at java.util.Date apidocs if
you need other time formatting


Regards, Gilbert


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: How can I capture a file's date to a property?

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.
Hi,

-----Original Message-----
From: cknell@onebox.com [mailto:cknell@onebox.com] 
Sent: Wednesday, July 11, 2007 9:21 PM
To: user@ant.apache.org
Subject: How can I capture a file's date to a property?

/*
I've have found several tasks for copying and renaming files and
directories, but no way to get the file's date-stamp.
*/

i would go via <script> i.e.

with jruby =

<project name="bla" default="main" basedir=".">
  <target name="depends">
  <scriptdef name="filemod" language="ruby">
   <attribute name="fname"/>
   <attribute name="prop"/>
   <![CDATA[
    attr = $bsf.lookupBean("attributes")
    fname = attr.get("fname")
    prop = attr.get("prop")
    t=File.atime(fname)
    $project.setNewProperty "filename", fname
     $project.setNewProperty prop, t.strftime("%m.%d.%Y")
   ]]>
   </scriptdef>

 <filemod fname="y:/test.txt"
          prop="fmod"/>
    
  </target>

  <target name="main" depends="depends">
    <echo>
      ${filename} last modified => ${fmod}
    </echo>
  </target>
</project>

see other time formatting possibilities =
 http://www.ruby.ch/ProgrammingRuby/htmlC/ref_c_time.html#strftime

or with javascript =

<scriptdef name="filemod" language="javascript">
 <attribute name="fname"/>
<attribute name="prop"/>
<![CDATA[
  fname = attributes.get("fname");
  prop = attributes.get("prop");
  f = new java.io.File(fname);
  date = new Date(f.lastModified());
  project.setNewProperty("filename", f);
  project.setNewProperty(prop, date);
]]>
</scriptdef>

have a look at java.util.Date apidocs if
you need other time formatting


Regards, Gilbert


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org