VBScript – How to Sort Folders using VB’s FileSystemObject and an ADODB Recordset

This is an example of how to sort a list of folders using VBScript.

The FileSystemObject in Visual Basic does not have a built in sorting method. #FrownyFace

I wanted to have a function that could do this a painlessly as possible. I searched around and found a good example but it needed some tweaking as it was originally written for running in an Classic ASP page.

I started with sample code from the site link below. Posted by “Thing”

https://www.sitepoint.com/forums/showthread.php?330636-Sorting-Files-using-FileSystemObject

It uses the FileSystemObject but also uses an adhoc ADODB Recordset to store the folder names then we use the recordset to sort then output the list on the order we’ve chosen.

Simple and effective code.

I’ve converted it to run on any Standard Windows PC.

Enjoy!

VBScript Code Example

Set objRS = CreateObject("ADODB.Recordset")
objRS.CursorLocation = 3 ' adUseClient
objRS.Fields.Append "Name", 200, 50 ' adVarChar

objRS.Fields.Append "created", 7 ' adDate
objRS.Open

imagespath = "C:\scripts"
Dim FSObj, FOlderObj, FileCollObj

Set FSObj = CreateObject("Scripting.FileSystemObject")
Set FolderObj = FSObj.GetFolder(imagespath)
Set FolderCollObj = FolderObj.SubFolders

For Each item in FolderCollObj
    Set SubFolderObj = FSObj.GetFolder(FolderObj & "/" & item.name)
    objRS.AddNew
    objRS("Name") = item.name
    objRS("created") = SubFolderObj.DateCreated
Next

objRS.Sort = "created ASC"
objRS.MoveFirst

Do UNTIL objRS.EOF  
    msgbox "Folder Name=" & objRS(0)    
    objRS.movenext
Loop

For Loops for Beginners – ASP, C#, PHP, JavaScript & Python Examples

A “For Loop” executes a block of code a specific number of times or while a specified condition is true.

PHP For Loop

for (init; condition; increment)
  {
  code to be executed;
  }

php fOR lOOP PARAMETERS

  • init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop)
  • condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  • increment: Mostly used to increment a counter (but can be any code to be executed at the end of the iteration)

Note: The init and increment parameters above can be empty or have multiple expressions (separated by commas). Example The example below defines a loop that starts with i=1. The loop will continue to run as long as the variable i is less than, or equal to 5. The variable i will increase by 1 each time the loop runs:

PHP For Loop Example Code

<?php
for ($i=1; $i<=5; $i++)
  {
  echo("The number is " . $i . "<br>");
  }
?>

Classic ASP For Loop Example Code

<%
For i = 1 to 5
 Response.Write("The number is " & i & "<br>")
Next
%>

JavaScript For Loop Example

<%
For i = 1 to 5
 Response.Write("The number is " & i & "<br>")
Next
%>

C# For Loop Example

for (int i = 0; i < 5; i++) 
      {
        Console.WriteLine(i);
      }    

Python For Loop Example

states = ["Alaska", "Alabama", "Arkansas"]
for x in states:
  print(x) 
  if x == "Alabama":
    break