Search code examples
vbscript

Getting list of files in current directory


I'm trying to get a script to read the contents of the directory where the script file is located, then identify a couple of specific files based on partial names and zip them. But I can't get the object.Files property to work. Can someone tell me what's wrong here?

Set FSO = CreateObject("Scripting.FileSystemObject")
objFolder = FSO.GetParentFolderName(WScript.ScriptFullName)
Set allFiles = objFolder.Files
For Each objFile in allFiles
    Wscript.Echo objFile.Name
Next

Solution

  • Your

    objFolder = FSO.GetParentFolderName(WScript.ScriptFullName)
    

    assigns a Path (String) to objFolder (type prefix fraud detected!). Use

    Set objFolder = FSO.GetFolder(FSO.GetParentFolderName(WScript.ScriptFullName))
    

    instead.