0

I am using the following vbs to read the JPG images in a directory:

Set objFolder = objFso.GetFolder(objShell.CurrentDirectory)
Set TifCollection = CreateObject("Scripting.Dictionary")

For Each objFile In objFolder.Files
  If objFso.GetExtensionName (objFile.Path) = "jpg" Then
    file = Split(objFile.Name, "_")(0)
    page = Split(objFile.Name, "_")(1)
    If Not TifCollection.Exists(file) Then
        TifCollection.Add file, CreateObject("Scripting.Dictionary")
    End If
    TifCollection.Item(file).Add page, ""
  End If
Next

For example directory contents:

image-00003844_1.jpg
image-00003844_2.jpg
image-00003844_3.jpg
...
image-00003844_10.jpg

The problem now is that the array TifCollection I create, is sorted as follows:

image-00003844_1.jpg
image-00003844_10.jpg
image-00003844_2.jpg
image-00003844_3.jpg
...
image-00003844_9.jpg

In other words, image 10 follows image 1 because, i assume, the array is sorted alphabetically (?)

When I later loop through the array though, I need the correct numerical order:

    image-00003844_1.jpg
    image-00003844_2.jpg
    ...
    image-00003844_10.jpg

How can i re-order the array?

---- EDIT - FULL CODE -----

Set objShell = CreateObject("WScript.Shell")
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(objShell.CurrentDirectory)
Set TifCollection = CreateObject("Scripting.Dictionary")
Set objLines = CreateObject("Scripting.Dictionary")

' Compile 2-dimensional array listing all documents with their pages
For Each objFile In objFolder.Files
  If objFso.GetExtensionName (objFile.Path) = "jpg" Then
    file = Split(objFile.Name, "_")(0)
    page = Split(objFile.Name, "_")(1)
    If Not TifCollection.Exists(file) Then
        TifCollection.Add file, CreateObject("Scripting.Dictionary")
    End If
    TifCollection.Item(file).Add page, ""
  End If
Next

funcSortKeysByLengthDesc(TifCollection)

' Loop through the array to compile the loadfile
For Each file In TifCollection.Keys
  For Each page In TifCollection(file)
    If (page = "1.jpg") Then 
      objLines.Add file & ",," & file & "_" & page & ",Y,,," & TifCollection(file).Count, ""
    Else 
      objLines.Add file & ",," & file & "_" & page & ",,,,", ""
    End If
  Next
Next

' Write to filesystem
strFileName = year(date) & month(date) & day(date) & hour(time) & minute(time) & second(time) & "-" & objLines.Count & "-IMAGES.opt"
Set objLoadfile = objFso.CreateTextFile(objFolder & "\" & strFileName,True)
objLoadfile.Write(Join(objLines.Keys, VBCRLF))



Function funcSortKeysByLengthDesc(dctList As Object) As Object
    'todo
End Function
Pr0no
  • 3,910
  • 21
  • 74
  • 121
  • 1
    Possible duplicate of [Sorting a dictionary by key in VBA](https://stackoverflow.com/q/14808104/11683) – GSerg Feb 22 '18 at 23:02
  • I tried running this, but I keep getting the error "missing ) [in the function name]". I have pasted my full code in the opening post. What am I doing wrong? – Pr0no Feb 22 '18 at 23:14
  • @GSerg VBA and VBScript are two separate languages, while they share similarities their syntax is not the same. – user692942 Feb 22 '18 at 23:15
  • 1
    Possible duplicate of [VBScript: Sorting Items from Scripting.Dictionary](https://stackoverflow.com/questions/11317541/vbscript-sorting-items-from-scripting-dictionary) – user692942 Feb 22 '18 at 23:16
  • 1
    @Lankymart The solution given is .NET; i am a beginner with VBscript. Why does everyone try to find a possible duplicate post? I did my searching beforehand, and wouldn't ask here if I was able to find a solution – Pr0no Feb 22 '18 at 23:31
  • @Pr0no actually, the solution is VBScript making use of .Net classes that are exposed to COM. To answer your other question, it's because you are not the first person to asked this. VBScript is over 20 years old and you say you are a beginner? [so] mission is to create a library of questions and answers for programmers, for the benefit of everyone not just the person asking the question. The last thing we need is yet more duplicates of existing questions, but you have enough rep to know this already. – user692942 Feb 22 '18 at 23:47
  • @Pr0no if you are still set on the classic sort approach, this might interest you - [Sorting a Dictionary Object](http://www.4guysfromrolla.com/webtech/062701-1.shtml) *(2 minute google search)* – user692942 Feb 22 '18 at 23:57
  • https://stackoverflow.com/questions/37946075/sorting-string-with-numbers-using-vb-script – ACatInLove Feb 23 '18 at 00:14
  • @Lankymart I know they are different languages. I also know exactly what the differences are, and looking through the shown VBA code I can see that in order to make it work you only need to [remove `As `](https://stackoverflow.com/q/13855633/11683) from the five variables in the beginning of the function, which is arguably much easier than switching to .NET classes or adapting [code that is originally in VBScript but sorts by property](https://stackoverflow.com/q/48114792/11683) as opposed to key. – GSerg Feb 23 '18 at 06:49
  • @GSerg in which case suggest it, but don't flag it as a duplicate as it isn't and just adds extra confusion for those who don't realise VBScript is typeless. – user692942 Feb 23 '18 at 08:08

0 Answers0