Ffffrank
Ffffrank

Reputation: 33

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

Upvotes: 3

Views: 3302

Answers (2)

Shubham Verma
Shubham Verma

Reputation: 9971

Here are the codes to get the list of files in the given directory:

Dim fso, folder, files, OutputFile
Dim strPath
 
' Create a FileSystemObject  
Set fso = CreateObject("Scripting.FileSystemObject")
 
' Define the folder we want to list files from
strPath = "C:\Users\shubhamVerma\OneDrive\Pictures\Camera Roll"
 
Set folder = fso.GetFolder(strPath)
Set files = folder.Files
 
' Create text file to output test data
Set OutputFile = fso.CreateTextFile("ScriptOutput.txt", True)
 
' Loop through each file  
For each item In files
    OutputFile.WriteLine(item.Name)

    ' Output file properties to a text file
    '   OutputFile.WriteLine(item.Name)
    '   OutputFile.WriteLine(item.Attributes)
    '   OutputFile.WriteLine(item.DateCreated)
    '   OutputFile.WriteLine(item.DateLastAccessed)
    '   OutputFile.WriteLine(item.DateLastModified)
    '   OutputFile.WriteLine(item.Drive)
    '   OutputFile.WriteLine(item.ParentFolder)
    '   OutputFile.WriteLine(item.Path)
    '   OutputFile.WriteLine(item.ShortName)
    '   OutputFile.WriteLine(item.ShortPath)
    '   OutputFile.WriteLine(item.Size)
    '   OutputFile.WriteLine(item.Type)   
    '   OutputFile.WriteLine("")
Next
 
' Close text file
OutputFile.Close

enter image description here

Upvotes: 0

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38775

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.

Upvotes: 4

Related Questions