Skip to main content
Solved

sorting files in folder

  • February 19, 2025
  • 7 replies
  • 79 views

Forum|alt.badge.img+4

Hi,

How to sort files in folder in order of last 5 characters of filename? I'm looping through files in folder and getting last 5 characters of filename but I couldn't find a solution for sorting. Please suggest. 

TIA

Best answer by Marc Mueller

Hi ​@Chou6 ,

here is the final version for you… 😁

 

Step 1:

Use the Python code from ​@Aaron.Gleason. (I tested your code in AA and works)

 

Step 2:

Extract text between [ and ] from Python output (see L5 on screenshot)

 

Step 3:

Split string with delimiter “,” into list variable. (see L6 on screenshot)

 

Step 4:

Loop through your sorted list of filenames… 😎 (see L7 on screenshot)

 

What do you think ​@Chou6?

 

Cheers

Marc

 

View original
Did this topic help answer your question?

7 replies

Aaron.Gleason
Automation Anywhere Team
Forum|alt.badge.img+10
  • Automation Anywhere Team
  • 503 replies
  • February 19, 2025

I might use a Python script to do the sorting.

import os

def sortByLastFive(mypath):
    # get list of files as an array
    myfiles = os.listdir(mypath)
    # set indices for the myfiles array
    first = 0
    second = 0
    # loop through the array (kind of a bubble sort)
    for firstfile in myfiles:
        for secondfile in myfiles:
            # see if the files should be reversed
            if (firstfile[-5:] < secondfile[-5:]):
                # reverse the files in the array
                mytemp = myfiles[first]
                myfiles[first] = myfiles[second]
                myfiles[second] = mytemp
            second = second + 1
        second = 0
        first = first + 1
    # return the array
    return(myfiles)

Then, you can have the script pass back a list for you to loop through. I have tested the script but not the integration with Automation Anywhere.


Forum|alt.badge.img+4
  • Author
  • 9 replies
  • February 19, 2025

I will give it a try. Thank you Aaron.


Forum|alt.badge.img+4
  • Author
  • 9 replies
  • February 19, 2025

When we execute python function, looks like we can only assign the output to a string variable.


Marc Mueller
Pathfinder Advocate | Tier 6
Forum|alt.badge.img+13
  • Pathfinder Advocate | Tier 6
  • 133 replies
  • February 20, 2025

Hi ​@Chou6 ,

you can try this and loop through the list named $lFinalFilenames$ …

String Split and replace to get rid of [ and ] from Python output.

 

Output after the Python Code:

 

 

 

 

 

 


Marc Mueller
Pathfinder Advocate | Tier 6
Forum|alt.badge.img+13
  • Pathfinder Advocate | Tier 6
  • 133 replies
  • February 20, 2025

@Chou6 just get rid of [ and ] in the Python output before split into list... much easier 


Marc Mueller
Pathfinder Advocate | Tier 6
Forum|alt.badge.img+13
  • Pathfinder Advocate | Tier 6
  • 133 replies
  • Answer
  • February 21, 2025

Hi ​@Chou6 ,

here is the final version for you… 😁

 

Step 1:

Use the Python code from ​@Aaron.Gleason. (I tested your code in AA and works)

 

Step 2:

Extract text between [ and ] from Python output (see L5 on screenshot)

 

Step 3:

Split string with delimiter “,” into list variable. (see L6 on screenshot)

 

Step 4:

Loop through your sorted list of filenames… 😎 (see L7 on screenshot)

 

What do you think ​@Chou6?

 

Cheers

Marc

 


Forum|alt.badge.img+4
  • Author
  • 9 replies
  • February 21, 2025

Will give it a try Marc, thank you 🙂 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings