List All Files in Folder using VBScript

pradeep's Avatar author of List All Files in Folder using VBScript
This is an article on List All Files in Folder using VBScript in Visual Basic [VB].
Rated 5.00 By 1 users
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
hanleyhansen, shabbir likes this
0
htl's Avatar, Join Date: Mar 2007
htl
Newbie Member
hi,
very nice
a wonderful example code

htl.
0
azrul's Avatar, Join Date: Jan 2009
Newbie Member
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
0
Cyprian Wyatt's Avatar, Join Date: Jan 2009
Newbie Member
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.

Last edited by shabbir; 30Jan2009 at 22:58.. Reason: Code block
0
stasis's Avatar, Join Date: Feb 2009
Newbie Member
So how could you modify this to list all sub directories inside the folder you choose?
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
Quote:
Originally Posted by stasis View Post
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
telkomek like this
0
seomaster's Avatar
Newbie Member
HI

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


Staffing service

Seo master
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
You are welcome !
0
ttran4's Avatar, Join Date: May 2009
Newbie Member
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
0
Yukta's Avatar, Join Date: Jun 2009
Newbie Member
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.