Community discussion forum

List Files in a Directory

This is a comment thread discussing List Files in a Directory
  • 9 years ago

    This thread is for discussions of List Files in a Directory.

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 4 years ago

    this is just what i was looking for. is it the same for getting directories? i tryed changing di.getfiles() to di.getdirectories() but i get an error

  • 3 years ago

    mmm That´s weird, check your read permissions !!!

  • 3 years ago

    Using (".extention") should be be preceeded with a * when you want all files.


    For instance, retrieving all usercontrols from a directoy:


    Code:
    Dim fso As IO.FileInfo() = myDir.GetFiles("*.ascx")


    As for the directory (check if it exists), use the following:


    Code:

           Dim myDirectory As New IO.DirectoryInfo("C:\temp")
           If myDirectory.Exists = True Then
              'directory exists
           Else
              'directory does NOT exisist
           End if



    Stephan

  • 3 years ago

    in order to get all the directories in the "main" directory use:


    Code:

          ' make a reference to a directory
           Dim di As New IO.DirectoryInfo(DriveLetter & ":\")
           Dim diar1 As IO.DirectoryInfo() = di.GetDirectories
           Dim dra As IO.DirectoryInfo


           'list the names of all directories in the base drive
           For Each dra In diar1
               MessageBox.Show(dra.ToString)
           Next
       End Sub



    of course you can change it so it uses a listbox instead of a messagebox or whatnot, but that will show all the base directories.  
    one change to make tho, is the Io.DirectoryInfo should be the drive letter... ie.


    instead of:

    Code:
    Dim di as new IO.DirectoryInfo(DriveLetter & ":\")

    you should write:
    Code:
    Dim di as new IO.DirectoryInfo("C:\")

    substituting C:\ for the drive of your choice.


    NOTE: This does not get the subdirectories.  I'm going to work on some code to do that.

  • 3 years ago

    Quote:
    [1]Posted by andy.davies on 16 Feb 2004 03:54 PM[/1]
    this is just what i was looking for. is it the same for getting directories? i tryed changing di.getfiles() to di.getdirectories() but i get an error


    in order to get all the directories in the "main" directory use:


    Code:

          ' make a reference to a directory
           Dim di As New IO.DirectoryInfo(DriveLetter & ":\")
           Dim diar1 As IO.DirectoryInfo() = di.GetDirectories
           Dim dra As IO.DirectoryInfo


           'list the names of all directories in the base drive
           For Each dra In diar1
               MessageBox.Show(dra.ToString)
           Next
       End Sub



    of course you can change it so it uses a listbox instead of a messagebox or whatnot, but that will show all the base directories.  
    one change to make tho, is the Io.DirectoryInfo should be the drive letter... ie.


    instead of:

    Code:
    Dim di as new IO.DirectoryInfo(DriveLetter & ":\")

    you should write:
    Code:
    Dim di as new IO.DirectoryInfo("C:\")

    substituting C:\ for the drive of your choice.


    NOTE: This does not get the subdirectories.  I'm going to work on some code to do that.

  • 3 years ago

    Get all the files in the directory, in each subdirectory, and their subdirectories etc...


    Code:

    ' This is a function to get all the files in a directory. This will get the files
       ' in all subdirectories of the parent folder
       Private Sub RecursiveSearch(ByRef strDirectory As String, ByRef array As ArrayList)
           Dim dirInfo As New IO.DirectoryInfo(strDirectory)
           ' Try to get the files for this directory
           Dim pFileInfo() As IO.FileInfo
           Try
               pFileInfo = dirInfo.GetFiles()
           Catch ex As UnauthorizedAccessException
               MessageBox.Show(ex.Message, "Exception!", MessageBoxButtons.OK, MessageBoxIcon.Error)
               Exit Sub
           End Try
           ' Add the file infos to the array
           array.AddRange(pFileInfo)
           ' Try to get the subdirectories of this one
           Dim pdirInfo() As IO.DirectoryInfo
           Try
               pdirInfo = dirInfo.GetDirectories()
           Catch ex As UnauthorizedAccessException
               MessageBox.Show(ex.Message, "Exception!", MessageBoxButtons.OK, MessageBoxIcon.Error)
               Exit Sub
           End Try
           ' Iterate through each directory and recurse!
           Dim dirIter As IO.DirectoryInfo
           For Each dirIter In pdirInfo
               RecursiveSearch(dirIter.FullName, array)
           Next dirIter
       End Sub

  • 3 years ago

    Hi.


    There is a problem, if the directory contains a lot of sub directories, the CPU cycles seem to max out, leaving you in a frozen state for quite a while, while the program waits to get all directory info.


    Any ideas how to minimise this?


    Thanks


    mark.

  • 3 years ago

    Very nicely done my friend. This is the cleanest way I have seen a VB.NET directory listing so far.

  • 3 years ago

    Quote:
    [1]Posted by markgrev on 15 Jun 2005 01:02 PM[/1]
    Hi.


    There is a problem, if the directory contains a lot of sub directories, the CPU cycles seem to max out, leaving you in a frozen state for quite a while, while the program waits to get all directory info.


    Any ideas how to minimise this?


    Thanks


    mark.




    i ran the whole thing in a new thread and that makes it "run in the background" so to speak, so the application doesn't hang up while it's processing.


    The only problem with using a thread is it leaves the application open to other processes at the same time.

  • 3 years ago

    This file search seems really nice.  Do you know what library to add in the project Reference in order to use File I/O?


    I'm getting errors here:


       Dim DI As New IO.DirectoryInfo
       Dim myFile As IO.FileInfo() = DI.GetFiles()
       Dim DFI As IO.FileInfo


    Errors say "User-defined type not defined"


    Thanks a bunch, I really appreciate it!  I've looked under the Reference list for the past 2 hours and still couldn't find File I/O system lib or some sort.  


  • 3 years ago

    What parameter do I supply for Array when calling the sub?

  • 3 years ago

    Quote:
    [1]Posted by hammett on 9 Aug 2005 01:38 AM[/1]
    What parameter do I supply for Array when calling the sub?


    define an arraylist and a string in your calling routine like this


    Code:

    Dim tarray As New ArrayList()
    Dim SearchPath As String


    SearchPath = "C:\"




    Then call the subroutine like this



    Code:

    RecursiveSearch(SearchPath, tarray)


    obviously you don't need to set tarray to anything on the first call.


  • 3 years ago

    Quote:
    [1]Posted by martini82 on 4 Aug 2005 12:52 AM[/1]
    This file search seems really nice.  Do you know what library to add in the project Reference in order to use File I/O?


    I'm getting errors here:


       Dim DI As New IO.DirectoryInfo
       Dim myFile As IO.FileInfo() = DI.GetFiles()
       Dim DFI As IO.FileInfo


    Errors say "User-defined type not defined"


    Thanks a bunch, I really appreciate it!  I've looked under the Reference list for the past 2 hours and still couldn't find File I/O system lib or some sort.  






    put this line in the top of your project


    Code:

    Imports System.IO

  • 3 years ago

    if you want to run the code in a thread so it doesn't tie up the GUI during long searches you can do this


    Code:

           Dim makeNewThread As New System.Threading.Thread(AddressOf SubNameHere)
           makeNewThread.Start()


    put the sub routine name in place of "SubNameHere" and this should contain the code to start off the recursive search function.


  • 3 years ago

    Code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


           ListBox1.Items.Clear()


           Dim basedir As New IO.DirectoryInfo(basepath)
           Dim dir1 As IO.DirectoryInfo() = base_dir.GetDirectories()
           Dim dir As IO.DirectoryInfo


           'list the names of all files in the specified directory
           For Each dir In dir1
               ListBox1.Items.Add(dir)
           Next
    End Sub



    base_path is the basic directory dimmed as String

Post a reply

Enter your message below

Sign in or Join us (it's free).