Print out files in a directory sorted by FileName

后端 未结 1 1072
遇见更好的自我
遇见更好的自我 2021-01-28 07:12

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

相关标签:
1条回答
  • 2021-01-28 08:14

    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

    1. store the names in a VBScript array and find/write a Sort Sub/Function
    2. use a disconnected ADODB recordset
    3. shell out to dir /o:n
    0 讨论(0)
提交回复
热议问题