0

I'm using Dir function to get directory's content right now.

FileSpec = DirPath & "/GenerateID_*_*.zip"
FileName = Dir(FileSpec)

But Dir function is locking the folder and can't delete that folder until I close my VBA application. I have tried ChDir("C:\") to make this point to another directory after calling Dir function but it's not working.

Is it possible to filter using FileSystemObject like Dir function? Or get all files with FileSystemObject, loop each fileName and checking each one would be the only option?

Min Naing Oo
  • 1,085
  • 5
  • 24
  • 49

1 Answers1

1

You can certainly loop through the files with the FileSystemObject. From the MSDN documentation for the Files Property of the FileSystemObject:

Function ShowFileList(folderspec)
   Dim fso, f, f1, fc, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(folderspec)
   Set fc = f.Files
   For Each f1 in fc
      s = s & f1.name 
      s = s &   "<BR>"
   Next
   ShowFileList = s
End Function

This will return a string that is delimited by '<BR>'. You can replace this with something else, like vbCrLf or whatever you like then parse the string as required.

Steve S
  • 421
  • 3
  • 4