0

I have a folder consisting of files in the following format: File_1, File_2...File_n

When I loop through them, this is the output I receive:

File_1,File_10,File_11...

How do I order the Files using VBScript such that I receive the desired output(File_1,File_2...)?

Given below is the code I have developed so far:

Set objFSO = CreateObject("Scripting.FileSystemObject")
  objStartFolder = "FilePath"

  Set objFolder = objFSO.GetFolder(objStartFolder)
  Set colFiles = objFolder.Files
  For Each objFile in colFiles
  
    File = File & "," & objFile.Name
Next
 MsgBox File
user692942
  • 16,398
  • 7
  • 76
  • 175
Ashwin_23
  • 11
  • 3
  • 1
    Does this answer your question? [Order of Files collection in FileSystemObject](https://stackoverflow.com/questions/16895525/order-of-files-collection-in-filesystemobject) – user692942 Aug 11 '21 at 09:53

1 Answers1

0

Here is an subroutine that I wrote that will return a sorted array.

You'd use it like this:

oSortedFile = ArraySort(objFile, "desc")


Sub ArraySort(aArrayToSort, sOrder)
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com
  'This Sub will sort the array passed as aArrayToSort
  For i = UBound(aArrayToSort) - 1 To 0 Step -1
    For j = 0 To i
      If aArrayToSort(j) < aArrayToSort(j+1) And sOrder = "desc" Then
        sTempStr = aArrayToSort(j+1)
        aArrayToSort(j+1) = aArrayToSort(j)
        aArrayToSort(j) = sTempStr
      ElseIf aArrayToSort(j) > aArrayToSort(j+1) And sOrder = "asc" Then
        sTempStr = aArrayToSort(j+1)
        aArrayToSort(j+1) = aArrayToSort(j)
        aArrayToSort(j) = sTempStr
      End If
    Next
  Next
End Sub
Dharman
  • 30,962
  • 25
  • 85
  • 135
Nathan Rice
  • 3,091
  • 1
  • 20
  • 30