Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Visual Basic [VB] (http://www.go4expert.com/articles/visual-basic-tutorials/)
-   -   List All Files in Folder using VBScript (http://www.go4expert.com/articles/list-files-folder-using-vbscript-t927/)

eLn1r0 11Sep2009 15:30

Re: List All Files in Folder using VBScript
 
Hallo

This is what i was surching for a very long time. But i must filter the files i want listed in the .txt file by the extension of the files.. could someone help me?

pascalsk 12Sep2009 01:59

Re: List All Files in Folder using VBScript
 
Dis is great Keep it up. i hope to learn more from u.

chmod777 24Sep2009 19:22

Re: List All Files in Folder using VBScript
 
Quote:

Originally Posted by SaswatPadhi (Post 47411)
It's really simple ! You just need to check <folder>.folders instead of <folder>.files.
Code: vb
On Error Resume Next

Const WINDOW_HANDLE = 0
Const BIF_EDITBOX = &H10
Const BIF_NONEWFOLDER = &H0200
Const BIF_RETURNONLYFSDIRS = &H1

Set objShell = CreateObject("Shell.Application")
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'**Browse For Folder To Be Processed
strPrompt = "Please select the folder to process."
intOptions = BIF_RETURNONLYFSDIRS + BIF_NONEWFOLDER + BIF_EDITBOX
strTargetPath = wshShell.SpecialFolders("MyDocuments")
strFolderPath = Browse4Folder(strPrompt, intOptions, strTargetPath)

Set objNewFile = objFSO.CreateTextFile(strFolderPath & "\filelist.txt", True)
Set objFolder = objFSO.GetFolder(strFolderPath)
' CHANGE STARTS HERE :
Set objColFolders = objFolder.Folders

For Each tfolder In objColFolders
&nbsp;&nbsp;&nbsp;&nbsp;objNewFile.WriteLine(tfolder.Name)
Next
' CHANGE DONE.
objNewFile.Close

'**Browse4Folder Function
Function Browse4Folder(strPrompt, intOptions, strRoot)
&nbsp;&nbsp;&nbsp;&nbsp;Dim objFolder, objFolderItem

&nbsp;&nbsp;&nbsp;&nbsp;On Error Resume Next

&nbsp;&nbsp;&nbsp;&nbsp;Set objFolder = objShell.BrowseForFolder(0, strPrompt, intOptions, strRoot)
&nbsp;&nbsp;&nbsp;&nbsp;If (objFolder Is Nothing) Then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ws cript.Quit
&nbsp;&nbsp;&nbsp;&nbsp;End If
&nbsp;&nbsp;&nbsp;&nbsp;Set objFolderItem = objFolder.Self
&nbsp;&nbsp;&nbsp;&nbsp;Browse4Folder = objFolderItem.Path
&nbsp;&nbsp;&nbsp;&nbsp;Set objFolderItem = Nothing
&nbsp;&nbsp;&nbsp;&nbsp;Set objFolder = Nothing
End Function

I have tried this in both Windows Vista, and again on XP (thinking that maybe Vista had issues), and I cannot get this to read the folders. It just creates an empty 'filelist.txt' file. I have not modified this script in any way, and have tried diff. folders. Any suggestions from anyone?

SaswatPadhi 24Sep2009 19:37

Re: List All Files in Folder using VBScript
 
Quote:

Originally Posted by chmod777 (Post 57911)
I have tried this in both Windows Vista, and again on XP (thinking that maybe Vista had issues), and I cannot get this to read the folders. It just creates an empty 'filelist.txt' file. I have not modified this script in any way, and have tried diff. folders. Any suggestions from anyone?

Hi,

Thanx for reporting. :)

There was a typo at this line :Set objColFolders = objFolder.Folders
It should read :Set objColFolders = objFolder.SubFolders.

Correct code [ TESTED ] is :
Code: VB

On Error Resume Next

Const WINDOW_HANDLE = 0
Const BIF_EDITBOX = &H10
Const BIF_NONEWFOLDER = &H0200
Const BIF_RETURNONLYFSDIRS = &H1

Set objShell = CreateObject("Shell.Application")
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'**Browse For Folder To Be Processed
strPrompt = "Please select the folder to process."
intOptions = BIF_RETURNONLYFSDIRS + BIF_NONEWFOLDER + BIF_EDITBOX
strTargetPath = wshShell.SpecialFolders("MyComputer")
strFolderPath = Browse4Folder(strPrompt, intOptions, strTargetPath)

Set objNewFile = objFSO.CreateTextFile(strFolderPath & "\filelist.txt", True)
Set objFolder = objFSO.GetFolder(strFolderPath)
' CHANGE STARTS HERE :
Set objColFolders = objFolder.SubFolders

For Each tfolder In objColFolders
    objNewFile.WriteLine(tfolder.Name)
Next
' CHANGE DONE.
objNewFile.Close

'**Browse4Folder Function
Function Browse4Folder(strPrompt, intOptions, strRoot)
    Dim objFolder, objFolderItem

    On Error Resume Next

    Set objFolder = objShell.BrowseForFolder(0, strPrompt, intOptions, strRoot)
    If (objFolder Is Nothing) Then
      Wscript.Quit
    End If
    Set objFolderItem = objFolder.Self
    Browse4Folder = objFolderItem.Path
    Set objFolderItem = Nothing
    Set objFolder = Nothing
End Function


chmod777 24Sep2009 19:55

Re: List All Files in Folder using VBScript
 
Well that did the trick. Thanks.

SaswatPadhi 24Sep2009 20:03

Re: List All Files in Folder using VBScript
 
My pleasure :)

DivinationX 13Nov2009 12:36

Re: List All Files in Folder using VBScript
 
How do i code such that, the filename that is captures will be stored into an array of strings? Thanks!

chuck42 29Jan2010 16:06

Re: List All Files in Folder using VBScript
 
Is it possible to tiune this skript, so I get a file contianing fileextention, size and owner of the file?
(I found something similar in asp: classicasp.aspfaq.com/files/directories-fso/how-do-i-find-the-owner-author-and-other-properties-of-a-file.html but I dont know how to do so in vbs or similar

hanleyhansen 29Jan2010 16:58

Re: List All Files in Folder using VBScript
 
This is very good and fairly simple. It would be nice if you explained each line for those not familiar with VB

RomuloAraujo 25Feb2010 06:45

Re: List All Files in Folder using VBScript
 
Hi here,

Very good example guys. However, I would like to push a little bit ... Let's say I have a folder containing 140k .CSVs files (avg size 5k) and I want to list them all files not to a file but to memory into an array or something... so then after listing I would be about concatenate all those files into one single big.CSV file.

is that possible?

Rom


All times are GMT +5.5. The time now is 07:16.