Learn how to Make Money Online | Free Tech Magazines
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Programming > Visual Basic [VB]

Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 
 
Bookmarks Article Tools Search this Article Display Modes

List All Files in Folder using VBScript

By pradeep pradeep is offline

On 24th June, 2006
Wink List All Files in Folder using VBScript

ADVERTISEMENT
Show Printable Version Email this Page Subscription Add to Favorites Copy List All Files in Folder using VBScript link

Author

pradeep ( Team Leader )

Yet to provide details about himself


All articles By pradeep

Recent Articles

Similar Articles

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"
Old 03-21-2007, 06:34 PM   #2
htl
Newbie Member
 
Join Date: Mar 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
htl is on a distinguished road

Re: List All Files in Folder using VBScript


hi,
very nice
a wonderful example code

htl.
htl is offline   Reply With Quote
Old 01-20-2009, 06:51 AM   #3
azrul
Newbie Member
 
Join Date: Jan 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
azrul is on a distinguished road

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
azrul is offline   Reply With Quote
Old 01-30-2009, 09:00 PM   #4
Cyprian Wyatt
Newbie Member
 
Join Date: Jan 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
Cyprian Wyatt is on a distinguished road

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.

Last edited by shabbir; 01-30-2009 at 10:58 PM. Reason: Code block
Cyprian Wyatt is offline   Reply With Quote
Old 02-26-2009, 05:50 AM   #5
stasis
Newbie Member
 
Join Date: Feb 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
stasis is on a distinguished road

Re: List All Files in Folder using VBScript


So how could you modify this to list all sub directories inside the folder you choose?
stasis is offline   Reply With Quote
Old 05-08-2009, 06:52 PM   #6
SaswatPadhi
~ Б0ЯИ Τ0 С0δЭ ~
 
SaswatPadhi's Avatar
 
Join Date: May 2009
Location: Orissa, INDIA
Posts: 1,302
Thanks: 42
Thanked 71 Times in 55 Posts
Rep Power: 4
SaswatPadhi is a jewel in the roughSaswatPadhi is a jewel in the roughSaswatPadhi is a jewel in the rough
Send a message via ICQ to SaswatPadhi Send a message via Yahoo to SaswatPadhi

Re: List All Files in Folder using VBScript


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
__________________
My articles --=|=-- Download G4EF Toolbar for Firefox --=|=-- Report G4EF Toolbar Bugs
There are times when sorrow seems the only truth.
SaswatPadhi is offline   Reply With Quote
Old 05-09-2009, 05:19 PM   #7
seomaster
Newbie Member
 
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
seomaster is on a distinguished road

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
seomaster is offline   Reply With Quote
Old 05-09-2009, 05:27 PM   #8
SaswatPadhi
~ Б0ЯИ Τ0 С0δЭ ~
 
SaswatPadhi's Avatar
 
Join Date: May 2009
Location: Orissa, INDIA
Posts: 1,302
Thanks: 42
Thanked 71 Times in 55 Posts
Rep Power: 4
SaswatPadhi is a jewel in the roughSaswatPadhi is a jewel in the roughSaswatPadhi is a jewel in the rough
Send a message via ICQ to SaswatPadhi Send a message via Yahoo to SaswatPadhi

Re: List All Files in Folder using VBScript


You are welcome !
__________________
My articles --=|=-- Download G4EF Toolbar for Firefox --=|=-- Report G4EF Toolbar Bugs
There are times when sorrow seems the only truth.
SaswatPadhi is offline   Reply With Quote
Old 05-21-2009, 11:42 AM   #9
ttran4
Newbie Member
 
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
ttran4 is on a distinguished road

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
ttran4 is offline   Reply With Quote
Old 06-18-2009, 03:11 AM   #10
Yukta
Newbie Member
 
Join Date: Jun 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
Yukta is on a distinguished road

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.
Yukta is offline   Reply With Quote
Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 


Currently Active Users Reading This Article: 2 (0 members and 2 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
System Folders, Normal Folders, and Directories pradeep Operating System 1 03-02-2009 02:31 PM
A couple of questions bout VB, VBScript VB.NET, etc rai_gandalf Visual Basic ( VB ) 6 12-28-2008 01:23 PM
Sorting Algorithms saomcol Programming 5 09-30-2007 01:50 PM
sorting linked list musicmancanora4 C-C++ 1 05-20-2006 11:41 PM
sort list musicmancanora4 C-C++ 3 05-20-2006 05:29 PM

 

All times are GMT +5.5. The time now is 04:52 AM.