Hi Guys,

Im more familiar with coding in C# ... can someone please help me to convert the line highlighted below into VB.NET

Much appreciated

private int file_count = 0;
///
/// Get the file count of a given directory recursively.
///
public void GetDirectoryFileCount(string dir)
{
dir = dir + @”\”;
//get all the directories and files inside a directory
String[] all_files=Directory.GetFileSystemEntries(dir);
//loop through all items
[B]foreach(string file in all_files)[/B]
{
//check to see if the file is a directory if not increment the count
if(Directory.Exists(file))
{
//recursive call
GetDirectoryFileCount(file);
}
else
{
//increment file count
file_count++;
}
}
}

Dani AI

Generated

asked for the foreach-to-VB.NET translation; and gave the straightforward For Each conversion which works. A few practical refinements make the routine more reliable and easier to reuse:

  • Avoid a module-level file_count if possible. Returning an Integer from the routine keeps state local, makes recursion safe, and avoids needing to reset the counter before each run.
  • Prefer Path.Combine over manually appending a backslash to build paths; it handles root/UNC paths correctly.
  • For large trees use streaming enumeration (EnumerateFiles/EnumerateFileSystemEntries) instead of the array-returning GetFileSystemEntries to reduce memory spikes.
  • Wrap directory access in Try/Catch and explicitly handle/skip UnauthorizedAccessException and PathTooLongException so a single protected folder won’t abort the whole count.
  • For multi-threaded code avoid a shared counter; use local totals or Interlocked.Increment when a shared counter is required. For extremely deep trees, consider an iterative stack-based traversal to avoid stack-overflow from recursion.

A concise alternative (streaming + simple) that returns a count in one line:

Public Function CountFiles(root As String) As Integer
    Return Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories).Count()
End Function

Notes and cautions: the one-line approach will throw on inaccessible folders unless wrapped; for robustness use a recursive or iterative traversal that catches and skips access exceptions. Also enable Option Strict and declare types explicitly when compiling with strict settings. These changes keep the translation correct while improving resilience and maintainability compared with a single shared counter approach.

Recommended Answers

All 2 Replies

If you declare file in advance it's just

Dim file As string

followed with

For Each file in all_files

At least that's how I normally do it

"For Each liListItem In ddlAssigned.Items" is how I've done it

That's your codes or get from internet?

Private file_count As Integer = 0
Public Sub GetDirectoryFileCount(ByVal dir As String)
  dir = dir & ”\”
  Dim all_files() As String=Directory.GetFileSystemEntries(dir)
  For Each file As String In all_files
    If Directory.Exists(file) Then
      GetDirectoryFileCount(file)
    Else
      file_count += 1
    End If
  Next file
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.