1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

List All Files in Folder using VBScript

Discussion in 'Visual Basic [VB]' started by pradeep, Jun 24, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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:
    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
     
    shabbir likes this.
  2. htl

    htl New Member

    Joined:
    Mar 21, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi,
    very nice
    a wonderful example code

    htl.
     
  3. azrul

    azrul New Member

    Joined:
    Jan 20, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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?


     
  4. Cyprian Wyatt

    Cyprian Wyatt New Member

    Joined:
    Jan 30, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Jan 30, 2009
  5. stasis

    stasis New Member

    Joined:
    Feb 26, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    So how could you modify this to list all sub directories inside the folder you choose?
     
  6. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    It's really simple ! You just need to check <folder>.folders instead of <folder>.files.
    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)
    ' 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 likes this.
  7. seomaster

    seomaster New Member

    Joined:
    May 9, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    HI

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


    Staffing service

    Seo master
     
  8. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    You are welcome ! :smile:
     
  9. ttran4

    ttran4 New Member

    Joined:
    May 21, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  10. Yukta

    Yukta New Member

    Joined:
    Jun 17, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  11. eLn1r0

    eLn1r0 New Member

    Joined:
    Sep 11, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  12. pascalsk

    pascalsk New Member

    Joined:
    Sep 11, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Dis is great Keep it up. i hope to learn more from u.
     
  13. chmod777

    chmod777 New Member

    Joined:
    Sep 24, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  14. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    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:
    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
     
  15. chmod777

    chmod777 New Member

    Joined:
    Sep 24, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Well that did the trick. Thanks.
     
  16. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
  17. DivinationX

    DivinationX New Member

    Joined:
    Nov 12, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    How do i code such that, the filename that is captures will be stored into an array of strings? Thanks!
     
  18. chuck42

    chuck42 New Member

    Joined:
    Jan 29, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  19. hanleyhansen

    hanleyhansen New Member

    Joined:
    Jan 24, 2008
    Messages:
    336
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Drupal Developer/LAMP Developer
    Location:
    Clifton
    Home Page:
    This is very good and fairly simple. It would be nice if you explained each line for those not familiar with VB
     
  20. RomuloAraujo

    RomuloAraujo New Member

    Joined:
    Feb 25, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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
     

Share This Page