Tech Off Thread

14 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Searching for files (kinda urgent!)

Back to Forum: Tech Off
  • User profile image
    W3bbo

    'lo

    How do you create an array or arraylist of all files, subdirectories, and subfiles (to an infinite depth) using System.IO?

    ...this is kinda urgent Smiley

  • User profile image
    AndyC

    Enumerate them, adding each one to the array in turn.

    Implementation is left as an exercise for the reader.

  • User profile image
    sbc

    Shameless plug:  Recursive File List Control v2 (ASP.NET)

    While not exactly what you want (you have to click each folder to list the files), it may be a good starting point. I have no idea what it is like in Visual Studio (which I don't have), as I just uses a plain text editor and let ASP.NET compile it automatically (using src="file.cs" instead of codebehind="file.cs").

  • User profile image
    Sven Groot

    Public Sub GetFiles(ByVal startDir As String, ByVal result As ArrayList)
    Dim dirs() As String = System.IO.Directory.GetDirectories(startDir)
    For Each dir As String In dirs
    result.Add(dir)
    GetFiles(dir, result)
    Next
    Dim files() As String = System.IO.Directory.GetFiles(startDir)
    For Each file As String In files
    result.Add(file)
    Next
    End Sub

    ...

    Dim list As New ArrayList
    GetFiles("C:\", list)

    Not tested, but it should work.

  • User profile image
    Red5

    Shouldn't you use BYREF for your ArrayList parameter?

    Public Sub GetFiles(ByVal startDir As String, ByRef result As ArrayList)


  • User profile image
    eddwo

    But why return the result as a parameter anyway?
    It returns a result, and only one result, so it should be a function!

    Public Function GetFiles(ByVal startDir As String) As ArrayList

    --

    Ok, I'm an idiot,
    It needs to be at least an input parameter otherwise it won't recurse.

    Sad

  • User profile image
    W3bbo

    I ran the code-block as-is, with the ByVal ArrayList param, but spookily enough, it runs all 7,000 files.

    But how?

    None of the params are ByRef or "Out", and it doesn't return anything either.

    Here's the context:

                Dim Filenames As New ArrayList
                GetFiles(Args(1), Filenames)
    
                Console.WriteLine(Filenames.Count & " files found")
    


  • User profile image
    eddwo

    Well you created the Arraylist outside the Sub, so you will still have a reference to that.  Since Arraylist is a reference type, using byval will not create a new arraylist internal to the sub, it will just pass a reference to it.

    Having byval will however, prevent the a new Arraylist that is created within the sub, being assigned to "Filenames" when the sub exits.

    So


    Public Sub GetFiles(ByVal startDir As String, ByVal result As ArrayList)
       Set result = new ArrayList;
       Dim dirs() As String = System.IO.Directory.GetDirectories(startDir)
       For Each dir As String In dirs
           result.Add(dir)
           GetFiles(dir, result)
       Next
       Dim files() As String = System.IO.Directory.GetFiles(startDir)
       For Each file As String In files
          result.Add(file)
       Next
    End Sub

    Dim FileList as ArrayList
    GetFiles("c:\",filelist)

    Will not work for byval but it would if it were byref

  • User profile image
    sbc

    wacko wrote:
    You know what I found to be interesting is that I could not pass in the root of any drive because it would complain about not having access to System Volume Information. Is there somethine that I did wrong ? if I pass this into a sub folder it works fine and sends back the correct information.



    It's a special folder used by Windows to store special data. Even administrators don't have access (unless you take ownership, or tweak the permissions). Just skip it when you come across it.

    What's the deal with the System Volume Information folder?

  • User profile image
    wacko

    You know what I found to be interesting is that I could not pass in the root of any drive because it would complain about not having access to System Volume Information. Is there somethine that I did wrong ? if I pass this into a sub folder it works fine and sends back the correct information.

    As far as the byref and out comments i get the same resaults

  • User profile image
    wacko

    So how would someone run an application that searched the drive ? Would you have to write something to look past it ?

  • User profile image
    Minh

    W3bbo wrote:
    I ran the code-block as-is, with the ByVal ArrayList param, but spookily enough, it runs all 7,000 files.

    But how?

    None of the params are ByRef or "Out", and it doesn't return anything either.

    Here's the context:



                Dim Filenames As New ArrayList
                GetFiles(Args(1), Filenames)

                Console.WriteLine(Filenames.Count & " files found")


    ByVal & ByRef works the way you think (allows changing or not) for value types. For reference types, it's a bit different.

    For ref types, ByVal effectively means don't allow reassignment. So you can't do this:

    Sub MySub(ByVal myObj As MyClass)

       myObj = somethingElse

    But you certainly be able to access any members

       myObj.ChangeMeAllYouWant(1000)


  • User profile image
    amotif

    wacko wrote:
    So how would someone run an application that searched the drive ? Would you have to write something to look past it ?


    Just catch the exception in your code and decide what you want to do with it.

    In my case I'm okay with not seeing parts of the file system that I don't have permission to access, so I just don't drill down into those directories.

  • User profile image
    amotif

    sbc wrote:


    omg, that post has an insane number of "hey, I have a virus in my SVI" comments in it. Almost like somebody was trying to DDoS Raymond. Wink I hope the AV vendors have it figured out by now.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.