Home All Groups Group Topic Archive Search About

Question about walking the file system

Author
19 Oct 2006 1:31 AM
Anthony P.
Hello Everyone,

I am building an application and I need to walk the file system from
the root directory on down. I need to cover every single directory on
the system and all of their sub directories. Basically, I am trying to
programatically obtain a count of how many files the system has.

Any ideas on how to walk the filesystem in VB.NET or if there is a
better way to get a file count?

Thanks!
Anthony

Author
19 Oct 2006 2:19 AM
maligui
I would google for a recursive search code. Then have it count files inseat
of listing it. I have found the code on http://www.vb.net

Hope this helps.
Author
19 Oct 2006 2:22 AM
maligui
Author
19 Oct 2006 2:30 AM
Anthony P.
Thank you very much! Very helpful.

Anthony
Author
19 Oct 2006 3:27 AM
ShaneO
Anthony P. wrote:
> Hello Everyone,
>
> I am building an application and I need to walk the file system from
> the root directory on down. I need to cover every single directory on
> the system and all of their sub directories. Basically, I am trying to
> programatically obtain a count of how many files the system has.
>
> Any ideas on how to walk the filesystem in VB.NET or if there is a
> better way to get a file count?
>
> Thanks!
> Anthony
>
On the 14th of October, Stephany Young posted in a reply to "Find Files
and Folders", the following code which might be helpful -


File.WriteAllText([output filename], String.Join(Environment.Newline,
Directory.GetFiles([start point], "*.*", SearchOption.AllDirectories)))

See the OP for full details.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Author
19 Oct 2006 7:11 AM
Spam Catcher
ShaneO <spc***@optusnet.com.au> wrote in news:4536f02a$0$24796$afc38c87
@news.optusnet.com.au:

> File.WriteAllText([output filename], String.Join(Environment.Newline,
> Directory.GetFiles([start point], "*.*", SearchOption.AllDirectories)))
>
> See the OP for full details.

Doesn't this method take a while for the function to return results? So you
can't display progress?
Author
19 Oct 2006 8:51 AM
Stephany Young
Shane0 was a bit previous in posting that snippet in this context.

While it was perfectly valid for thr thread concerned it is not appropriate
for this case.

The OP is asking how to get a count of the files on a given disk drive.

Directory.GetFiles([start point], "*.*", SearchOption.AllDirectories).Length
will certainly give that count BUT it also returns a string array with an
element for each file which could be rather large and possibly blow out some
resource or other. It will also throw an exception if it encounters a folder
with restrictive permissions, so if you start from the root of a disk drive
e.g. C:\, it will blow up when it hits the System Volume Information folder.

I don't think there is 'quick' way to get this information and a recursive
procedure would be more appropriate.


Show quoteHide quote
"Spam Catcher" <spamhoneypot@rogers.com> wrote in message
news:Xns986120691EF2Dusenethoneypotrogers@127.0.0.1...
> ShaneO <spc***@optusnet.com.au> wrote in news:4536f02a$0$24796$afc38c87
> @news.optusnet.com.au:
>
>> File.WriteAllText([output filename], String.Join(Environment.Newline,
>> Directory.GetFiles([start point], "*.*", SearchOption.AllDirectories)))
>>
>> See the OP for full details.
>
> Doesn't this method take a while for the function to return results? So
> you
> can't display progress?
Author
19 Oct 2006 5:00 AM
Spam Catcher
"Anthony P." <papill***@gmail.com> wrote in news:1161221509.166876.283380
@h48g2000cwc.googlegroups.com:

> Any ideas on how to walk the filesystem in VB.NET or if there is a
> better way to get a file count?

You can use system.io but i found it to slow - rather ... the Windows API
seems to be much faster.
Author
21 Oct 2006 12:47 AM
Stuart Parker
Show quote Hide quote
"Anthony P." <papill***@gmail.com> wrote in message
news:1161221509.166876.283380@h48g2000cwc.googlegroups.com...
> Hello Everyone,
>
> I am building an application and I need to walk the file system from
> the root directory on down. I need to cover every single directory on
> the system and all of their sub directories. Basically, I am trying to
> programatically obtain a count of how many files the system has.
>
> Any ideas on how to walk the filesystem in VB.NET or if there is a
> better way to get a file count?
>
> Thanks!
> Anthony
>

I'm not saying its the most efficient code, but I leeced if from somewhere
and it works

Public FileArray As New ArrayList

Private Sub Recurse(ByVal DirPath As String, ByVal IncludeSubFolders As
Boolean)

    Dim objFileInfo As FileInfo

    Dim objDir As DirectoryInfo = New DirectoryInfo(DirPath)

    Dim objSubFolder As DirectoryInfo

    Try

        ' This bit does the getting of files in the current folder

        Console.WriteLine("Processing Folder: " & objDir.FullName)

        StsCurrentFile.Text = "Processing Folder: " & objDir.FullName

        For Each objFileInfo In objDir.GetFiles()

            Console.WriteLine("Processing File: " & objFileInfo.Name & vbTab
& objFileInfo.LastWriteTime)

            FileArray.Add(objFileInfo)

        Next

        ' This bit does the recursion for subdirs

        If IncludeSubFolders Then

            For Each objSubFolder In objDir.GetDirectories()

            Console.WriteLine("Processing Subfolder: " & objDir.FullName)

            'Console.WriteLine(Recurse(objSubFolder.FullName,
IncludeSubFolders))

            Recurse(objSubFolder.FullName, IncludeSubFolders)

            Next

        End If

    Catch Ex As Exception

        MsgBox("Err - " & Err.Number & vbNewLine & vbNewLine &
Err.Description & vbNewLine & vbNewLine & Err.GetException.StackTrace &
vbNewLine & vbNewLine & Err.Source & vbNewLine & vbNewLine &
Err.GetException.Source)

    End Try

End Sub