AnsweredAssumed Answered

Sort directory file list stored in a arry by name or date or ...

Question asked by strwo01 Employee on Aug 5, 2013
Latest reply on Aug 21, 2013 by gcubed
Hi everybody,

I need be sure to process files in the correct order by name.

I started with this script, which processed the files in the order of creation in the directory:
        <!-- MAIN LOOP : get all files and process -->
        <core:new className="java.io.File" var="dir">                                         <!-- Initialisierung der Klasse -->
            <core:arg type="java.lang.String" value="${rootdir}/input"/>                      <!-- Setzen des Pfades          -->
        </core:new>
        <core:set value="${dir.list().size()}" var="dircnt"/>                                 <!-- Anzahl Dateien zählen      -->
        <gel:log>Directory '${rootdir}/input' has ${dir.list().size()} files...</gel:log>
        <gel:log>List Directory entries</gel:log>
        <core:forEach items="${dir.list()}" var="fileRow">
            <core:set value="${fileRow}" var="v_file_name"/>
            <gel:log>Processing '${v_file_name}'</gel:log>
            <gel:log/>
        </core:forEach>
Then I try to use the method sort() but it doen't work. After using sort() the array do not show any value:
        <!-- MAIN LOOP : get all files and process -->
        <core:new className="java.io.File" var="dir">                                         <!-- Initialisierung der Klasse -->
            <core:arg type="java.lang.String" value="${rootdir}/input"/>                      <!-- Setzen des Pfades          -->
        </core:new>
        <core:set value="${dir.list().size()}" var="dircnt"/>                                 <!-- Anzahl Dateien zählen      -->
        <core:set value="${dir.list().sort()}" var="dirlist"/>                                 <!-- Anzahl Dateien zählen      -->
        <gel:log>Directory '${rootdir}/input' has ${dir.list().size()} files...</gel:log>
        <gel:log>List Directory entries sorted</gel:log>
        <core:forEach items="${dirlist}" var="fileRow">
            <core:set value="${fileRow}" var="v_file_name"/>
            <gel:log>Processing '${v_file_name}'</gel:log>
            <gel:log/>
        </core:forEach>
Instead of
        <core:forEach items="${dirlist}" var="fileRow">
I also try
        <core:forEach items="${dir.list().sort()}" var="fileRow">
Neither the first try nor the second on work.

Hope someone have a suggestion for me.

Many thanks in advance

Wolfgang

Outcomes