Jump to content
Sign in to follow this  
Burgs

Sorting?

Recommended Posts

Burgs

Greetings,

I have a folder containing several hundred small .txt files. Each file is named "MyFile" followed by a numerical value. The files are not in the sequencial order like I'd like them to be, for example:

"MyFile01"

"MyFile02"

"MyFile03"

"MyFile04"

"MyFile05"

"MyFile06"

"MyFile07"

"MyFile08"

"MyFile09"

"MyFile10"

"MyFile100"

"MyFile101"

...

"MyFile109"

"MyFile11"

...

As can be seen, file numbers "100 - 109" come before file number "11", and likewise with other files in the folder. I'd like to be able to write an AutoIT script to "sort" my folder so all the files are in the proper order, for example "MyFile 01 - MyFile500". Any advice? Thank you in advance.

Share this post


Link to post
Share on other sites
PsaltyDS

Greetings,

I have a folder containing several hundred small .txt files. Each file is named "MyFile" followed by a numerical value. The files are not in the sequencial order like I'd like them to be, for example:

"MyFile01"

"MyFile02"

"MyFile03"

"MyFile04"

"MyFile05"

"MyFile06"

"MyFile07"

"MyFile08"

"MyFile09"

"MyFile10"

"MyFile100"

"MyFile101"

...

"MyFile109"

"MyFile11"

...

As can be seen, file numbers "100 - 109" come before file number "11", and likewise with other files in the folder. I'd like to be able to write an AutoIT script to "sort" my folder so all the files are in the proper order, for example "MyFile 01 - MyFile500". Any advice? Thank you in advance.

One method:
#include <Array.au3>

; Simulated return from _FileListToArray()
Global $avFiles[12] = [11, "MyFile01.txt", "MyFile0.txt", _
        "MyFile.txt", "MyFile07.txt", "MyFile10.txt", _
        "MyFile100.txt", "MyFile101.txt", "MyFile109.txt", _
        "MyFile11.txt", "MyFile500.txt", "MyFile1001.txt"]

; Create 2D array for sorting
Global $avSort[UBound($avFiles)][2]

; Create reformatted file names in second column
For $n = 1 To UBound($avFiles) - 1
    $avSort[$n][0] = $avFiles[$n]
    $avRegEx = StringRegExp($avSort[$n][0], "(?i)(\D+)(\d+)(.*)", 3)
    If IsArray($avRegEx) And (UBound($avRegEx) >= 3) Then
        $avSort[$n][1] = $avRegEx[0] & StringFormat("%08u", $avRegEx[1]) & $avRegEx[2]
    Else
        $avSort[$n][1] = $avFiles[$n]
    EndIf
Next

; Sort
_ArrayDisplay($avSort, "Before Sort")
_ArraySort($avSort, 0, 1, 0, 1)
_ArrayDisplay($avSort, "After Sort")

:D


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
archgriffin

Maybe use _FileListToArray to put all the files in an array, then put each in to an array according to the file name length, use _ArraySort to sort them then walk through them either to a single array, or a text file or something.

Alternatively maybe have something find the longest file name, and then "pad" the rest to match it. Would be best with a regular expression to compare it to (finding the last numerical digit and adding 0s in front of it). But I'm rather horrible at regex.

I also bet there are better ideas then those, its just what I could think of from the top of my head.

edit: too slow...

Edited by archgriffin

"Human kind cannot gain anything without first giving something in return, to obtain; something of equal value must be lost."The Help File is truly your friend.

Share this post


Link to post
Share on other sites
GEOSoft

I'm thinking that the OP is not talking about getting the file names in a particular sort order. As I read it what (s)he is trying to do is open a folder and see the files in a particular sort order. What he showed is the standard Windows "Arrange Icons by >> name" order. The other options are size, type, modified. There is no way I'm aware of to sort by creation date so modified is as close as it gets and I'm not even sure how to change that sort order programmatically.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
Burgs

Thank you for all the replies. Yes I guess I'm interested in how Windows is displaying the file names, thank you for the link to the program that can rename to the proper amount of digits as well...

Share this post


Link to post
Share on other sites
PsaltyDS

Thank you for all the replies. Yes I guess I'm interested in how Windows is displaying the file names, thank you for the link to the program that can rename to the proper amount of digits as well...

If you achieve the sort as given in my earlier post, then all you need to add is:
; Move files to new names
$sDir = "C:\Temp\SubDir"; Directroy the list came from
For $n = 1 To UBound($avSort) - 1
    If $avFiles[$n][0] <> $avFiles[$n][1] Then 
        FileMove($sDir & "\" & $avSort[$n][0], $sDir & "\" & $avSort[$n][1])
    EndIf
Next

:D


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×