Jump to content

array sort not working correctly. Can't get JPG's in numerical order

Recommended Posts

Guys, I have a small problem and I can't figure it out.

I am renaming jpg files. I'm using win xp. I can go into the file and sort and rename the jpg's manually. I name them with a number like this "(1).jpg". In my script, I use the _FileListToArray function and when I display the array, they are not in numerical order. I try and use the _ArraySort function and it doesn't sort them in numerical order either. I took a look at the images in Windows live photo viewer and it appears they are being sorted by the Time stamp on the image (not the date stamp). How can I get these files in numerical order?? My script is set up rename the images based on the image position but I can't get the images in the correct positions.

#include <File.au3>
#include <Array.au3>
#include <String.au3>

$aFiles = _FileListToArray(@ScriptDir,"*",2)
;$aTime = FileSetTime(@ScriptDir & "\*.jpg","")
For $i = UBound($aFiles) -1 To 0 Step -1

    $aImg = _FileListToArray(@ScriptDir &"\" & $aFiles[$i],"*",1)
    For $iCt = UBound($aImg) -1 To 0 Step -1
        $aSearch = _ArraySearch($aImg,$iCt
        $rName = FileMove(@ScriptDir &"\" & $aFiles[$i] & "\" & $aImg[$iCt],@ScriptDir &"\pictest\" & $aFiles[$i] & "--" & $iCt & ".JPG",8)



My image file is too big to upload here on the forum so I uploaded a Zip on my server here http://beaman-auto.com/1HGCP2F72AA030249.zip

This is a sample file. It has abut 20 pics of one of our Used Cars.

I work for an automotive group and we are trying set up a process to upload our pics to the server in order. right now we have to go in and rearrange all the pics after they are uploaded. If I can get them numbered correctly before the upload, it would eliminate the 2nd step.

Edited by RickB75
Link to comment
Share on other sites

Your first step needs to be to find out how many images you have.

For this I use DirGetSize.

I've simplified the code down to just 7 lines.

#include <Array.au3>

Dim $Images[4000]

$FileCount = DirGetSize(@ScriptDir & "\Images", 1)

For $i = 1 To $FileCount[1]
    $Images[$i] = @ScriptDir & "\Images\" & $i & ".jpg"


The DirGetSize with a flag of 1 ends up giving extended information, with the array element [1] being how many files are located in the specified folder.

Alternatively, you can use this method that way you don't have to hard code the directory into it.

#include <Array.au3>

Dim $Images[4000]

$OpenFolder = FileSelectFolder("Select a folder", "")

$FileCount = DirGetSize($OpenFolder, 1)

For $i = 1 To $FileCount[1]
    $Images[$i] = @ScriptDir & "\Images\" & $i & ".jpg"


In the folder Images I have 4 image files, labeled 1, 2, 3, 4.jpg ect.

The DirGetSize with the flag of 1 gives the file count inside any given folder.

Next, all you need to do is run a For loop like you did and add in each picture per $i count (Going up each step)


Edited by Damein


Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Link to comment
Share on other sites

I'd sort by getting just the numbers from the file names, then formatting them to a uniform number of digits so that a simple _ArraySort() could do the trick for you.

Maybe there is an easier way, but I'd do something like this:

#include <File.au3>
#include <Array.au3>
#include <String.au3>

$aFiles = _FileListToArray(@ScriptDir,"*.jpg",1)
_ArrayDelete($aFiles, 0)

Func _Sort(ByRef $aSort)
    If Not IsArray($aSort) Then Return SetError(1)
    Local $aTemp[UBound($aSort)][2], $i, $aTemp2
    For $i = 0 To UBound($aSort) - 1
        $aTemp[$i][0] = $aSort[$i]
        $aTemp2 = StringRegExp($aSort[$i], "\d+", 3)
        If IsArray($aTemp2) Then $aTemp[$i][1] = StringFormat("%09i", $aTemp2[0])
    _ArraySort($aTemp, 0, 0, 0, 1)
    For $i = 0 To UBound($aSort) - 1
        $aSort[$i] = $aTemp[$i][0]
Link to comment
Share on other sites

Thank you guys for your replys and your help.

@ DW1   I think your solution is the one that is going to work for me. I'm still studying your example script but I just did a test run and it appears it is sorting the files in numerical order. That is what the main goal is.

@Damien  I apologize but I don't understand your solution. Please correct me if I'm wrong but, I thought the FileListToArray function was getting the data (file names and file extensions )  I needed. I need to put the files in numerical order first and then change the name of the file base on it's positon in the array.


 Here's an example of what it is currently returning before DW1's _sort function.


This is with DW1's function


DW1, I'm still trying to understand your function but, it's works and I'm very grateful.

Thank you guys once again for your help.

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

  • Recently Browsing   0 members

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