I'm trying to print documents in a directory, ordered by file name ascending. I have the script below to print out the documents, which works, but it's in a random order. Is there any way to sort the "files" collection based on name?
'Set the TargetFolder
TargetFolder = "C:\Temp\Hewitt\TestPrintFolder"
Set shellApplication = CreateObject("Shell.Application")
Set folder = shellApplication.Namespace(TargetFolder)
Set files = folder.Items
For Each file In files
file.InvokeVerbEx ("Print")
Next
The are many ways to get an odered list of the file(name)s in a directory. One uses the .NET ArrayList - like this:
Option Explicit
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sDir : sDir = "... your folder ..."
Dim oFiles : Set oFiles = CreateObject("System.Collections.ArrayList")
Dim oFile
For Each oFile In oFS.GetFolder(sDir).Files
WScript.Echo oFile.Name
oFiles.Add oFile.Path
Next
WScript.Echo "----------"
oFiles.Sort
Dim sFile
For Each sFile In oFiles
WScript.Echo oFS.GetFile(sFile).Name
Next
If you can't harness .Net, you could
- store the names in a VBScript array and find/write a Sort Sub/Function
- use a disconnected ADODB recordset
- shell out to
dir /o:n