Search code examples
vbscriptfilesystemobjectfso

Can I use FileSystemObject get a single file from a folder using its index?


If there is only one file in a folder, can I pick it up without knowing its name or iterating through the files in the folder?

(The code is VBS, but it could be anything, the FSO is the interesting part here.)

This didn't work for me:

dim fso
set fso = CreateObject("Scripting.FileSystemObject")
dim myFolder
Set myFolder = fso.getFolder("E:\test")
Dim myfiles
Set myfiles = myFolder.Files

WScript.Echo myfiles.Item(0).Path

Neither did WScript.Echo myfiles(0).Path work. (Index 0,1 tested, both fail.)

Using a for each to get just one file seems a bit of an overkill. Also, am I not supposed to be able to somehow iterate with a simple For loop, not a For Each? So there must be indexes... I just can't seem to find them.


Solution

  • No, you can't pick the file without knowing its name or iterating over the files in the folder, at least not with a FileSystemObject instance. As documented the Item property of the Files collection requires the name of an item, not its index:

    Item Property (Files)

    Gets the specified item for in a Files object

    Syntax

    object.Item(key)[ = newitem]
    

    Arguments

    object
    Required. The name of a File object.

    spec
    Required. The name of the item.

    Not every collection allows access by index.

    If you're looking for a magic trick you could do something like this:

    dir = "C:\your\folder"
    
    Set sh = CreateObject("WScript.Shell")
    Set ex = sh.Exec("cmd /c dir /b """ & dir & """")
    While ex.Status = 0 : WScript.Sleep 100 : Wend
    
    filename = Split(ex.StdOut.ReadAll, vbNewLine)(0)
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFile(fso.JoinPath(dir, filename))
    

    However, that approach is neither very elegant nor very robust, and I fail to see its advantage over something like

    dir = "C:\your\folder"
    Set fso = CreateObject("Scripting.FileSystemObject")
    For Each item In fso.GetFolder(dir).Files
      Set f = item
    Next