Jump to content

Recommended Posts

Posted

Hi :)

Let's say I get a path called "E:\pics\". It has pictures inside which are numbered starting at 0. Then we get something like "E:\pics\0" or "E:\pics\1" or "E:\pics\2"... The amount of numbers, thus the path of the last picture, are known to me already. For example, there are 100 pictures, which means that the files start at 0 and end at 99

However, for some things that isn't enough, because they need the file extension. They can be pretty "random", though. Sometimes it's a JPG, sometimes it's a PNG, sometimes it's a GIF.

So, I know need some kind of function that gets me the file extension, or the complete path, from an incomplete path. The input would be "E:\pics\1" and I expect and output of "E:\pics\1.jpg" for example.

Also, there is just one file extension per number. For example, it there is "E:\pics\1.jpg", there won't be a "E:\pics\1.png" anymore.

And this will be looped as well. First I get number 0, then number 1, then number 2, and so on. So I need something that is able to be repeated in a for-loop without causing problems with the parameters.

Thanks a lot!

Posted

You can use _FileListToArray i believe. You have the folder/dir, grab the files and then loop them in the array and use the String functions to check them. For example you have 150 files in them folder (no folder just files for example). 0 to 99 is the ones you want and the other 50 its other files like "mydoc.doc", with the String (regex is a option here) functions you can check the name before the extension and put the valid ones in another array, them you will have the path (already known) and the full file name with the extension.

Posted

Great idea. Would also make things easier in the loop. Wouldn't that screw with my memory, though? Sometimes we're talking about several hundred strings with each up to 200 symbols. I don't have a clue, if it's that good to save like 100.000 symbols in one single variable.

Posted

Can I actually re-arrange that list?

Those numbers are there for a reason, so I don't really like the function screwing around with the order...

It should be 1, 2, 3,... not 1, 10, 100, 101, [...], 2, 20, 200,...

  • Moderators
Posted

Katie_Deely,

Do you have any control over the file naming? If so then padding the numbers with leading zeroes will get you the sort you want.

If not then you can do something like this:

#include <Array.au3>

; Simulated file list
Local $aArray[] = ["E:\Pics\1.jpg", "E:\Pics\10.jpg", "E:\Pics\3.jpg", "E:\Pics\100.jpg", "E:\Pics\2.jpg"]

; Add a column
_ArrayColInsert($aArray, 1)

; And you get this:
_ArrayDisplay($aArray, "New column", Default, 8)

; Now extract the number part, pad it and add it to the new column
For $i = 0 To UBound($aArray) - 1

    ; Extract the numeric part
    $sNumber = StringRegExpReplace($aArray[$i][0], "^(.*\\)(\d+)(\..*)$", "$2")
    ; Padd it - here we use a max of 4 characters
    $sPadded = StringFormat("%04s", $sNumber)
    ; And store it
    $aArray[$i][1] = $sPadded

Next

; And here is the result
_ArrayDisplay($aArray, "Added padded", Default, 8)

; Now sort the array on the padded column
_ArraySort($aArray, 0, 0, 0, 1)

; Which gives this
_ArrayDisplay($aArray, "Sorted padded", Default, 8)

; Now delete the added column (and revert to 1D array)
_ArrayColDelete($aArray, 1, True)

_ArrayDisplay($aArray, "Final", Default, 8)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted (edited)

Old, but seems to work perfectly. Won't affect the file names in Windows, it will only affect them "virtually" (i.e. inside your script)

 

Edited by Inpho
Posted

This function _ArraySortRE, is based on Melba23's post#6 example.  It sorts an array based on the results of a regular expression which is applied to each element in a specified column or the default column 0.

#include <Array.au3>

; ---- 1D array example ----
Local $aArray[] = ["E:\Pics\1.jpg", "E:\Pics\10.jpg", "E:\Pics\ab3cd.jpg", "E:\Pics\100.jpg", "E:\Pics\2.jpg"]
_ArrayDisplay($aArray, "Original Array", Default, 32)

_ArraySortRE($aArray, 0, 0, 0, 0, "(?i)(^.*\\\D*)|(\D*\..*)$")
_ArrayDisplay($aArray, "Col0 Sort Ascending", Default, 8)

; ---- 2D array examples ----
Local $aArr2D[][] = [["E:\Pics\1qw.gif", "m21a", "q32we45rt"],["E:\Pics\10.jpg", "d2t", "as3df14f"], _
        ["E:\Pics\rt3.png", 16, "zx15cv34"],["E:\Pics\100yy.jpg", "w23x", "mn9bvf4"],["E:\Pics\2.bmp", "cc19zz", 7]]
_ArrayDisplay($aArr2D, "Original Array", Default, 32)

_ArraySortRE($aArr2D, 0, 0, 0, 0, "(?i)(^.*\\\D*)|(\D*\..*)$")
_ArrayDisplay($aArr2D, "Col0 Sort Ascending", Default, 8)

_ArraySortRE($aArr2D, 0, 0, 0, 1, "\D*")
_ArrayDisplay($aArr2D, "Col1 Sort Ascending", Default, 8)

_ArraySortRE($aArr2D, 0, 0, 0, 2, "^\D*|(\D+\d*\D*)?$")
_ArrayDisplay($aArr2D, "Col2 1st number Sort Ascending", Default, 8)

_ArraySortRE($aArr2D, 0, 0, 0, 2, "(^\D+\d*\D*)?|\D*$")
_ArrayDisplay($aArr2D, "Col2 2nd number Sort Ascending", Default, 8)


;$aArray -  A 1D or 2D array to be sorted numerically on a Regular Expression derived number.
;$iDescending [optional] If set to 1, sort in descending order
;$iStart [optional] Index of array to start sorting (default 0 = first element or row)
;$iEnd [optional] Index of array to stop sorting (default 0 = last element or row)
;$iSubItem [optional] Sub-index to sort on in 2D arrays (default 0 = first column)
;RE pattern [optional] - To be applied to all elements in the $iSubItem column; and,
;                      - Is to delete all characters that are not the required sort value. This will leave only the number to be sorted; and,
;                      - When default (""), _ArraySortRE sorts the same as the _ArraySort function sorts.
;Requires - #include <Array.au3>
;
Func _ArraySortRE(ByRef $aArray, $iDescending = 0, $iStart = 0, $iEnd = 0, $iSubItem = 0, $REPattern = "")
    Local $MaxLen = 0
    _ArrayColInsert($aArray, 0); Add a new column

    ; Now extract the number part, pad it with leading zeros and add it to the new column (Col0)
    For $i = 0 To UBound($aArray) - 1
        If $REPattern = "" Then
            $aArray[$i][0] = $aArray[$i][$iSubItem + 1]
        Else
            $sNumber = StringRegExpReplace($aArray[$i][$iSubItem + 1], $REPattern, "") ; Extract the numeric part
            $aArray[$i][0] = StringRight("0000000000" & $sNumber, 10) ; Padd it with leading zeros - here we use a max of 10 characters
        EndIf
    Next
    ;_ArrayDisplay($aArr2D)
    _ArraySort($aArray, $iDescending, $iStart, $iEnd, 0); Now sort the array on the padded column (Col0)
    _ArrayColDelete($aArray, 0, True); Now delete the added column.
EndFunc   ;==>_ArraySortRE

 

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
×
×
  • Create New...