« Return to Thread: Sorting file name

Re: Sorting file name

by Scot P. Floess-2 :: Rate this Message:

Reply to Author | View in Thread


If anyone is interested, I expanded upon my earlier example:

     <scriptdef
         name        = "sort-files"
         language    = "beanshell" >

         <classpath>
             <!-- put your classpath here for beanshell and friends -->
         </classpath>

         <attribute  name = "dir"/>
         <attribute  name = "property"/>
         <attribute  name = "padding"/>
         <attribute  name = "delimiter"/>
         <attribute  name = "description"/>

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

                 if ( dir.exists () && dir.isDirectory () )
                 {
                     String[] files =  dir.list ();

                     int maxLen = 0;

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

                     StringBuilder sb = new StringBuilder ();

                     String padding = attributes.get ( "padding" );
                     if ( null == padding  || "".equals ( padding ) )
                     {
                         padding = " ";
                     }

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

                     String padding = sb.toString ();

                     java.util.TreeMap treeMap = new java.util.TreeMap ();

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

                     int fileCount = 1;

                     String delimiter = attributes.get ( "delimeter" );
                     if ( null == delimiter  || "".equals ( delimiter ) )
                     {
                         delimiter = ",";
                     }

                     sb.setLength ( 0 );

                     for ( String entry : treeMap.keySet () )
                     {
                         sb.append ( treeMap.get ( entry ) );

                         if ( fileCount++ < files.length )
                         {
                             sb.append ( delimiter );
                         }
                     }

                     project.setProperty ( attributes.get ( "property" ),
sb.toString () );
                 }
                 else
                 {
                     project.setProperty ( attributes.get ( "property" ),
"" );
                 }
         ]]>
     </scriptdef>

The above will work in scenarios such as:

1.sql, 10.sql, 011.sql, 02.sql

Just use "0" for the pad attribute...



On Wed, 24 Jun 2009, Scot P. Floess wrote:

>
>
> 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@...>
>> 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@...>
>>>> 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@...
>>>>> For additional commands, e-mail: user-help@...
>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@...
>>> For additional commands, e-mail: user-help@...
>>>
>>>
>>
>>
>> --
>> David Weintraub
>> qazwart@...
>>
>
> 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@...
> For additional commands, e-mail: user-help@...
>
>

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@...
For additional commands, e-mail: user-help@...

 « Return to Thread: Sorting file name