0

How can I get a list of files that are present in a directory in visual basic script?

  1. I looked at rosetta code but it does not have such a task specified.
  2. The search results I found were poluted by vb.net scripts that I could not apply in my .vbs script.
Mary
  • 14,926
  • 3
  • 18
  • 27
a.t.
  • 2,002
  • 3
  • 26
  • 66
  • 1
    Possible duplicate of [Getting list of files in current directory](https://stackoverflow.com/questions/36091446/getting-list-of-files-in-current-directory) To search here for a specific language, use the tag at the beginning of the search, like `[vbscript] your search text`. – Ken White Sep 16 '19 at 17:24

1 Answers1

1

The code is a combination of:

  1. Getting current directory in VBScript
  2. https://www.go4expert.com/articles/list-files-folder-using-vbscript-t927/

The code below can be stored in a .vbs file. It first gets the current directory of the (executing) script, and then it gets the list of files that are inside that directory, returns it and finally it prints the first element.

' Get the path of the current (executing) script
scriptdir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
msgbox(scriptdir)

' get file list array
Set fileList = listFiles(scriptdir)

' display first element of file list array
msgbox(fileList(0))

On Error Resume Next
Function listFiles(sFolder)
  Set fileList = CreateObject("System.Collections.ArrayList")
  Dim fso, folder, files, NewsFile
  msgbox(sFolder)
  Set fso = CreateObject("Scripting.FileSystemObject")
  If sFolder = "" Then
      Wscript.Echo "No Folder parameter was passed"
      Wscript.Quit
  End If
  Set folder = fso.GetFolder(sFolder)
  Set files = folder.Files

  For each folderIdx In files
    fileList.add(folderIdx.Name)
  Next
  Set listFiles = fileList
End Function
a.t.
  • 2,002
  • 3
  • 26
  • 66