Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do we have a VBS version of listing files in a folder?

Status
Not open for further replies.

feipezi

IS-IT--Management
Aug 10, 2006
316
0
0
US
Hi,

There is a function 'filelist' on the web in Excel macro but I am looking for a similar func with VBS, which can be run in Window Explorer.

Thanks in advance.

Here is the Excel macro/function 'FILELIST'.

Function FileList(fldr As String, Optional fltr As String = "*.*") As Variant
Dim sTemp As String, sHldr As String
If Right$(fldr, 1) <> "\" Then fldr = fldr & "\"
sTemp = Dir(fldr & fltr)
If sTemp = "" Then
FileList = Split("No files found", "|")
Exit Function
End If
Do
sHldr = Dir
If sHldr = "" Then Exit Do
sTemp = sTemp & "|" & sHldr
Loop
FileList = Split(sTemp, "|")
End Function
 
I just happened to login and see this. I don't login like I used to.

Anyhow, here are a few ideas to chase after, if you're still curious:
* FileSystemObject - gives lots of things you can do. If nothing else, you can build a loop (with or without subfolder recursion) to loop through all files. I've done this many times to get whatever info I'm after. If it's super large, then be warned this method may take a while.

* SHELL() Command can run many command line commands from Windows. Basically you call SHELL() and you stick your command inside the parentheses, and in quotes inside the parentheses, b/c it expects a string. I've used this for a few things, but at the moment, the exacts are slipping my mind. That said, I would think something like SHELL("DIR >C:\MyTempFolder\MyFancyFileName.txt") would work, but you'd probably need to attach the "CD C:\MyBigFileFolder\" to the front of it, along with a carriage return. Haven't tried, but that's what I would expect. Or create a batch file, and use SHELL() to run the batch file.

Anyhow, those are a couple of ideas.

There is the [a href="[URL unfurl="true"]https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dir-function"[/URL]]Dir()[/href] VB/VBA command to test if a directory is real. So there may be something similar in VB/VBA that will list files in a directory to whatever. I imagine you'd want to send it to a spreadsheet, so depending upon how that worked, not sure if you can send it to clipboard and then paste in excel or else send to an array formula that you then loop back out to Excel.

You know, the more I think about this, the more I think, just go use FileSystemObject and create a loop to grab what you want for every file/sufolder. Loads of examples online.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Pretty much depends on what you actually want to do with the file list. Simply display it? Put it in a spreadsheet? Create a simple textfile? Do a mass rename? In other words, iis there more to your code than simply reading a filelist?


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top