ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scot P. Floess" <sflo...@nc.rr.com>
Subject Re: Sorting file name
Date Wed, 24 Jun 2009 13:07:10 GMT


Here is a snippet beanshell that can do the work for you:

     <scriptdef
         name        = "sortFiles"
         language    = "beanshell" >

         <classpath>
             <!-- Set up here to include you libs, ie beanshell and friends 
-->
             <!--
                This is where I store mine :)

             <fileset  dir = "${keros.dependency.HOME}"  includes =
"*.jar"/>
             -->
         </classpath>

         <attribute  name = "dir"/>

         <![CDATA[
                 java.io.File dir = new java.io.File ( attributes.get ( 
"dir" ) );

                 String[] files =  dir.list ();

                 int maxLen = 0;

                 for ( String file : files )
                 {
                     if ( file.length () > maxLen )
                     {
                         maxLen = file.length ();
                     }
                 }

                 StringBuilder sb = new StringBuilder ();

                 for ( int len = 0; len < maxLen; len++ )
                 {
                     sb.append ( ' ' );
                 }

                 String padding = sb.toString ();

                 java.util.TreeSet treeSet = new java.util.TreeSet ();

                 for ( String file : files )
                 {
                     if ( file.length () < maxLen )
                     {
                         treeSet.add ( padding.substring ( 0, maxLen - 
file.length () ) + file );
                     }
                     else
                     {
                         treeSet.add ( file );
                     }
                 }

                 for ( String entry : treeSet )
                 {
                     System.out.println ( entry.trim () );
                 }
         ]]>
     </scriptdef>

I tested and it definitely works :)

To run, I did this:

<sortFiles dir = "/home/sfloess/testdir"/>

The directory /home/sfloess/testdir contains the files:

10.sql  11.sql  1.sql  2.sql

The results when run:

[sortFiles] 1.sql
[sortFiles] 2.sql
[sortFiles] 10.sql
[sortFiles] 11.sql


HTH,

Flossy




On Tue, 23 Jun 2009, David Weintraub wrote:

> Well, there is the <script> task which can help you make this happen. It's
> an optional Ant task, and you need to include in a few additional jarfiles
> when you run Ant. <
> http://ant.apache.org/manual/install.html#librarydependencies>. The Script
> macro can be found here: <
> http://ant.apache.org/manual/OptionalTasks/script.html>.
>
> I've never used the script macro, so I can't help you there, but you are
> taking on a rather complex task.
>
> You need to collect the file names, create a "sortkey" for each file name,
> sort the files on that key, and then recall these names one-by-one. There is
> no "numeric sort" command I know of in most languages, so you'll have to do
> that yourself. You'll need to do that in an "approved" language which may
> not be a language you are familiar with.
>
> If you are a decent Java developer, you could try writing a custom Ant task.
> Information about that can be found in the Ant Manual here: <
> http://ant.apache.org/manual/tutorial-writing-tasks.html>.
>
> Lots of luck with your endeavor.
>
> On Mon, Jun 22, 2009 at 10:55 PM, Henry Suhatman <henry@jawasoft.com> wrote:
>
>> Yes it way to solve, but i don't want fill zero in first filename, if i
>> write new task i don't know where i had started :(
>> Anyone can suggest? Please advice me.
>>
>> Thx.
>>
>>
>> David Weintraub wrote:
>>
>>> Well, it is sorting the files by name. Unfortunately, it is sorting them
>>> in
>>> ASCII dictionary order.
>>>
>>> The easiest way (and maybe the only way) is to zero fill in the names of
>>> these files when you create them, so that you have 001.sql and 010.sql
>>> instead of 1.sql and 10.sql.
>>>
>>> That way, they'll sort in the order you want:
>>>
>>> 001.sql
>>> 002.sql
>>> 010.sql
>>> 011.sql
>>>
>>> I looked at Resource Collections, and there is a <sort> resource
>>> collection,
>>> but nothing to customize your sort order the way you want.
>>>
>>> You could write your own task or script, but it's probably not worth it.
>>>
>>>
>>> On Fri, Jun 19, 2009 at 7:15 PM, Henry Suhatman <henry@jawasoft.com>
>>> wrote:
>>>
>>>
>>>
>>>> Hi all,
>>>>
>>>> I have use foreach task from ant-contrib, the output like this
>>>> 1.sql
>>>> 10.sql
>>>> 11.sql
>>>> 2.sql
>>>>
>>>> And, i want to sort file name like this:
>>>> 1.sql
>>>> 2.sql
>>>> 10.sql
>>>> 11.sql
>>>>
>>>>
>>>> ----------------------------------------------------------------------------
>>>> This is my code :
>>>>
>>>>
>>>> ----------------------------------------------------------------------------
>>>> <project name="check" default="testing">
>>>>  <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
>>>>  <target name="testing">
>>>>      <foreach param="file" target="loop">
>>>>          <path>
>>>>              <fileset dir="army">
>>>>                  <filename name="*.sql" />
>>>>              </fileset>
>>>>          </path>
>>>>      </foreach>
>>>>  </target>
>>>>  <target name="loop">             <echo>${file}</echo>
>>>>  </target>
>>>> </project>
>>>>
>>>> -------------------------------------------------------------------------------
>>>>
>>>> What should do to solve this problem?
>>>>
>>>> Thanks & Regards,
>>>>
>>>>
>>>>
>>>> Henry
>>>>
>>>> ---------------------------------------------------------------------
>>>> 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
>>
>>
>
>
> -- 
> David Weintraub
> qazwart@gmail.com
>

Scot P. Floess
27 Lake Royale
Louisburg, NC  27549

252-478-8087 (Home)
919-890-8117 (Work)

Chief Architect JPlate   http://sourceforge.net/projects/jplate
Chief Architect JavaPIM  http://sourceforge.net/projects/javapim

Architect Keros          http://sourceforge.net/projects/keros

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


Mime
View raw message