Jump to content

FileFindNextFile numerical sorting problem


Go to solution Solved by jdelaney,

Recommended Posts

Hi all i have files named like this
 
 
----------------
name1.mp3
name2.mp3
name3.mp3
....
name10.mp3
-----------------
 
i made  a script that get the name of all files in a folder and put all the file names it one txt file and separate the file names by "/" sign 
i used the "FileFindFirstFile" example and the "FileFindNextFile" function
 
the script is working properly but the problem is that it is sorting the files like this
---------------
name1.mp3
name10.mp3
name2.mp3
name3.mp3
--------------
 
 
here is my script 
 
$input1 = InputBox("","ÇÏÎá ÕíÛÉ ÇáãáÝÇÊ ÇáÊí ÊÑíÏ Çä ÇßÊÈåÇ Ýí ÇáãáÝ ÇáäÕí ÇÐÇ ßäÊ ÊÑíÏ ÌãíÚ ÇáÕíÛ ÇßÊÈ ÑãÒ ÇáäÌãÉ" & @CRLF & "ãËÇá: mp3 wav txt")
if @error then Exit
$input = InputBox("", "ÇÏÎá ÇáÇÓã ÇáßÇãá ááãÌÏ ÇáÐí íÍÊæí ÇáãáÝÇÊ ãËÇá: C:\Users\Salam\Desktop\aliklas")
if @error then Exit
FileChangeDir($input)
; Shows the filenames of all files in the current directory.
Local $search = FileFindFirstFile("*." & $input1)

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "áÇ íæÌÏ ãÌáÏÇÊ ÈåÐÇ ÇáÇÓã Çæ áÇ íæÌÏ ãáÝÇÊ Ýí ÇáãÌáÏ ÇáÐí ÇÏÎáÊ ãÓÇÑå Çæ áÇ íæÌÏ ãáÝÇÊ ÈÇáÕíÛå ÇáÊí ÇÏÎáÊåÇ")
    Exit
EndIf

While 1
    Local $file = FileFindNextFile($search)
    If @error Then ExitLoop
    FileWrite("names.txt", $file & "/")
WEnd

FileClose($search)
MsgBox(0,"","Êã ÓÍÈ ÇÓãÇÁ ÇáãáÝÇÊ ÈäÌÇÍ, ÓæÝ ÊÌÏ ãáÝ ÈÇáÇÓãÇÁ Ýí äÝÓ ãÌáÏ ÇáÇÕæÇÊ æÇÓã ÇáãáÝ  names.txt" & @CRLF & "ãáÇÍÙÉ ÓæÝ ÊÌÏ Ýí äåÇíÉ ãáÝ ÇáÇÓãÇÁ ÑãÒ / ÇÖÇÝí íÌÈ ÍÐÝå íÏæíÇ")
 
i want to to save them based on the number value (just like the first example)
 
i had done some researches i found that this a function in windows called  numerical sorting in Windows Explorer
anyway changing that dos not affects Autoit behavior
 
how can i fix that ?
 
thanks in advanced

Link to comment
Share on other sites

Something like this?

#include <array.au3>
#include <File.au3>
DirCreate(@DesktopDir & "\Testing")
For $i = 1 To 20
    _FileCreate(@DesktopDir & "\Testing\File" & $i & ".txt")
Next
$aFiles = _FileListToArray(@DesktopDir & "\Testing")

Local $aSorted[UBound($aFiles)-1][2]
For $i = 1 To UBound($aFiles)-1
    $aSorted[$i-1][0]=$aFiles[$i]
    $aSorted[$i-1][1]=Number(StringRegExpReplace($aFiles[$i],"[^\d]",""))
Next
_ArraySort($aSorted,0,0,0,1)
_ArrayDisplay($aSorted)

output:

[0]|File1.txt|1
[1]|File2.txt|2
[2]|File3.txt|3
[3]|File4.txt|4
[4]|File5.txt|5
[5]|File6.txt|6
[6]|File7.txt|7
[7]|File8.txt|8
[8]|File9.txt|9
[9]|File10.txt|10
[10]|File11.txt|11
[11]|File12.txt|12
[12]|File13.txt|13
[13]|File14.txt|14
[14]|File15.txt|15
[15]|File16.txt|16
[16]|File17.txt|17
[17]|File18.txt|18
[18]|File19.txt|19
[19]|File20.txt|20
 

Then you can loop through the array to do the writes.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Something like this?

#include <array.au3>
#include <File.au3>
DirCreate(@DesktopDir & "\Testing")
For $i = 1 To 20
    _FileCreate(@DesktopDir & "\Testing\File" & $i & ".txt")
Next
$aFiles = _FileListToArray(@DesktopDir & "\Testing")

Local $aSorted[UBound($aFiles)-1][2]
For $i = 1 To UBound($aFiles)-1
    $aSorted[$i-1][0]=$aFiles[$i]
    $aSorted[$i-1][1]=Number(StringRegExpReplace($aFiles[$i],"[^\d]",""))
Next
_ArraySort($aSorted,0,0,0,1)
_ArrayDisplay($aSorted)

output:

[0]|File1.txt|1

[1]|File2.txt|2

[2]|File3.txt|3

[3]|File4.txt|4

[4]|File5.txt|5

[5]|File6.txt|6

[6]|File7.txt|7

[7]|File8.txt|8

[8]|File9.txt|9

[9]|File10.txt|10

[10]|File11.txt|11

[11]|File12.txt|12

[12]|File13.txt|13

[13]|File14.txt|14

[14]|File15.txt|15

[15]|File16.txt|16

[16]|File17.txt|17

[17]|File18.txt|18

[18]|File19.txt|19

[19]|File20.txt|20

 

Then you can loop through the array to do the writes.

 

thanks man for helping ...

but im not so familiar with arrays.

so what im asking for is not possible with "filefindnextfile"

if not possible may u please explain more on how i must convert your script to behave like i wanted in my script ?

sorry im a noob

thanks in advanced

Link to comment
Share on other sites

Loop example:

#include <array.au3>
#include <File.au3>
DirCreate(@DesktopDir & "\Testing")
For $i = 1 To 20
    _FileCreate(@DesktopDir & "\Testing\File" & $i & ".txt")
Next
$aFiles = _FileListToArray(@DesktopDir & "\Testing")

Local $aSorted[UBound($aFiles)-1][2]
For $i = 1 To UBound($aFiles)-1
    $aSorted[$i-1][0]=$aFiles[$i]
    $aSorted[$i-1][1]=Number(StringRegExpReplace($aFiles[$i],"[^\d]",""))
Next
;~ _ArrayDisplay($aSorted)
_ArraySort($aSorted,0,0,0,1)
;~ _ArrayDisplay($aSorted)

_FileCreate("names.txt")
For $i = 0 To UBound($aSorted)-1
    FileWrite("names.txt", $aSorted[$i][0] & "/")
Next

output would be:

File1.txt/File2.txt/File3.txt/File4.txt/File5.txt/File6.txt/File7.txt/File8.txt/File9.txt/File10.txt/File11.txt/File12.txt/File13.txt/File14.txt/File15.txt/File16.txt/File17.txt/File18.txt/File19.txt/File20.txt/

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

jdelaney

 

hey man thanks alot your script was helpfull

but i having an error

i faced files named

file1.mp3/file2.mp3/file3.mp3/file4.mp3/file5.mp3/file6.mp3/file8.mp3/file9.mp3/file10.mp3/file11.mp3/file12.mp3/file13.mp3/file14.mp3/file15.mp3/file16.mp3/file17.mp3/file18.mp3/file19.mp3/file7-1.mp3/file7-2.mp3/

 

as you see your script is working correctly except when there is number-subnumber

the "file7-1.mp3/file7-2.mp3/" must be after "/file6.mp3" not at the end

any ideas ?

Link to comment
Share on other sites

  • Solution

#include <array.au3>
#include <File.au3>
DirCreate(@DesktopDir & "\Testing")
For $i = 1 To 20
    _FileCreate(@DesktopDir & "\Testing\File" & $i & ".mp3")
Next
_FileCreate(@DesktopDir & "\Testing\File7-1.mp3")
_FileCreate(@DesktopDir & "\Testing\File7-2.mp3")


$aFiles = _FileListToArray(@DesktopDir & "\Testing")

Local $aSorted[UBound($aFiles)-1][2]
For $i = 1 To UBound($aFiles)-1
    $aSorted[$i-1][0]=$aFiles[$i]
    If StringRegExp($aFiles[$i],"[\d]+-[\d]+",0) Then
        $aSorted[$i-1][1] = Number(StringRegExpReplace($aFiles[$i],"([a-zA-Z]+)([\d]+)-([\d]+)\..*","$2.$3"))
    Else
        $aSorted[$i-1][1]=Number(StringRegExpReplace($aFiles[$i],"([a-zA-Z]+)(\d+)\..*","$2"))
    EndIf
Next
;~ _ArrayDisplay($aSorted)
_ArraySort($aSorted,0,0,0,1)
;~ _ArrayDisplay($aSorted)

_FileCreate("names.txt")
For $i = 0 To UBound($aSorted)-1
    FileWrite("names.txt", $aSorted[$i][0] & "/")
Next
ConsoleWrite(FileRead("names.txt") & @CRLF)

Output:

File1.mp3/File2.mp3/File3.mp3/File4.mp3/File5.mp3/File6.mp3/File7.mp3/File7-1.mp3/File7-2.mp3/File8.mp3/File9.mp3/File10.mp3/File11.mp3/File12.mp3/File13.mp3/File14.mp3/File15.mp3/File16.mp3/File17.mp3/File18.mp3/File19.mp3/File20.mp3/

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...