Question about walking the file system

  • Thread starter Thread starter Anthony P.
  • Start date Start date
A

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
 
Anthony said:
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.
 
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.
 
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?
 
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.


Spam Catcher said:
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?
 
Anthony P. said:
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
 
Back
Top