ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan.Mate...@rzf.fin-nrw.de
Subject AW: Getting the size of a file
Date Mon, 26 May 2003 08:04:27 GMT
Back from holiday I am doing my emails ...
... and have a solution for you :-)


I used the <script> task for iterating over the selected files of a 
fileset, instatiate a java.io.File object and gets that filesize using
its length() method.
As a result I stored the value as ant property (with common prefix).

A quick shot but works for me. 
Here is the build file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="test" basedir="." default="main">
    <target name="main">
        <!-- for what files the size is required? -->
        <fileset id="fs" dir="test"/>

        <!-- use java.io.File in script task for getting that size -->
        <script language="javascript"> <![CDATA[

            // iterate over the files selected by the fileset defined above
            // code adapted from copy task implementation

            // Get the DirectoryScanner
            // project is an object defined by script task
            ds = fs.getDirectoryScanner(project);

            // Get the source files (array)
            srcFiles = ds.getIncludedFiles();

            // iterate over that array
            for (i=0; i<srcFiles.length; i++) {
                filename = srcFiles[i];
                file = new java.io.File(fs.getDir(project), filename);
                size = file.length();

                // store the size an a property
                // prefix.filename=size
                project.setProperty("filesize."+filename,size);
            }
        ]]> </script>

        <!-- prints the sizes -->
        <echoproperties prefix="filesize"/>

    </target>
</project>




Jan


> -----Urspr√ľngliche Nachricht-----
> Von: Morten Mortensen [mailto:Morten.Mortensen@TietoEnator.com]
> Gesendet am: Dienstag, 13. Mai 2003 11:59
> An: 'Ant Users List'
> Betreff: Getting the size of a file
> 
> 
> Hi Ant-fans,
> 
> Is it really true, that it is impossible to get the size of a 
> file - would
> like it as a property-value - ?
> 
> E.g. many MIDP-upload-programs *insist* on having the correct 
> size of the
> JAR-file in the JAD-file and I would like to build JAD-files 
> from templates
> by replacing values, but the missing "size of file" prohibits this... 
> 
> A generic "get size of file in bytes" could be quite useful.
> 
>      ?
> 
> Med venlig hilsen 
>   Morten Sabroe Mortensen 
>   mailto:Morten.Mortensen@tietoenator.com 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
> 

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message