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/)

pradeep 24Jun2006 14:10

List All Files in Folder using VBScript
 
Sometimes you might require to get a list of all the files in a folder, I was thinking how it can be implemented, the solution is to used the WSH interface and write a script in VBScript.
Below is a sample script which will get the list of files on a directory, say c:\windows and save it to the file c:\windows\FileList.txt.

Code: VB

On Error Resume Next
  Dim fso, folder, files, NewsFile,sFolder
 
  Set fso = CreateObject("Scripting.FileSystemObject")
  sFolder = Wscript.Arguments.Item(0)
  If sFolder = "" Then
      Wscript.Echo "No Folder parameter was passed"
      Wscript.Quit
  End If
  Set NewFile = fso.CreateTextFile(sFolder&"\FileList.txt", True)
  Set folder = fso.GetFolder(sFolder)
  Set files = folder.Files
 
  For each folderIdx In files
    NewFile.WriteLine(folderIdx.Name)
  Next
  NewFile.Close


Example usage:
Code:

lister.vbs "c:\documents and settings"

More VB Articles



VB.Net Array Basics
Variables in Visual Basic
Strings in VB - Part I
Binary Conversion In VB
VB6 Small Backup Program

htl 21Mar2007 17:34

Re: List All Files in Folder using VBScript
 
hi,
very nice
a wonderful example code

htl.

azrul 20Jan2009 06:51

Re: List All Files in Folder using VBScript
 
Hey, i would like to enchance it by providing a dialog box. However, i failed to make it work. Care to point me on the right direction?


Quote:


On Error Resume Next
Dim fso, folder, files, NewFile, sfolder, path

path = InputBox ("Please write your path")
sFolder = "& path &"

Set fso = CreateObject("Scripting.FileSystemObject")
If sFolder = null Then
Wscript.Echo "No Folder parameter was passed"
Wscript.Quit
End If

Set NewFile = fso.CreateTextFile(sFolder&"\Filelist.txt", True)
Set folder = fso.GetFolder(sFolder)
Set files = folder.Files
For each folderIdx In files
NewFile.WriteLine(folderIdx.Name)
Next
NewFile.close


Cyprian Wyatt 30Jan2009 21:00

Re: List All Files in Folder using VBScript
 
Code:

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)
Set objColFiles = objFolder.Files

For Each file In objColFiles
        objNewFile.WriteLine(file.Name)
Next
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

Here ya go. I would change the createtext file to another location, such as to 'c:\'. You can also change the strTargetPath to any valid path.

stasis 26Feb2009 05:50

Re: List All Files in Folder using VBScript
 
So how could you modify this to list all sub directories inside the folder you choose?

SaswatPadhi 8May2009 17:52

Re: List All Files in Folder using VBScript
 
Quote:

Originally Posted by stasis (Post 43509)
So how could you modify this to list all sub directories inside the folder you choose?

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
    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


seomaster 9May2009 16:19

Re: List All Files in Folder using VBScript
 
HI

hi,
very nice
a wonderful example code and i thank u really great.........


Staffing service

Seo master

SaswatPadhi 9May2009 16:27

Re: List All Files in Folder using VBScript
 
You are welcome ! :smile:

ttran4 21May2009 10:42

Re: List All Files in Folder using VBScript
 
This is definitley a great example.

How can it be modified to list files in the main directory and any subdirectories? I'd like to write something that will give me the name, create date, modify date for all files in the main directory and for any files in the sub-directories.

I wrote the following, but it can not handle sub-directories. Any help is appreciated. Thanks.



On Error Resume Next
Dim fso, folder, files, NewsFile,sFolder
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\Documents and Settings\Phat Buddha\desktop\")
Set outfile = fso.CreateTextFile("c:\temp\testout.txt")
Set files = folder.Files


For each folderIdx In files
outfile.WriteLine(folderIdx.Name & ";" & folderIdx.DateCreated & ";" & folderIdx.DateLastModified)
Next
outfile.Close

Yukta 18Jun2009 02:11

Re: List All Files in Folder using VBScript
 
Hi:
I am a novice to VBS. I was wodering if it is possible to read thru the files in a folder and capture the last line in every file(csv file) and write to a logfile that has the name(with date and time)
Also, is it possible that when capturing the last line , I have to extract the last field value compare it with a base value and if it is greater than the base value then write it the log file. Since it loops thru each file in the folder, base values for each file keep changing. I am not quite sure how to do it. Any help would be great.
Thanks,
Yukta.


All times are GMT +5.5. The time now is 03:12.