×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Directory.GetFiles() misbehaves

Directory.GetFiles() misbehaves

Directory.GetFiles() misbehaves

(OP)
Colleagues,
The subject line says it, and I'm at the end of the rope with frustration! This is what's happening:

I have a root dir, with subdirs, where I have files of type *.LOG, *.XML and *.PS;
These files can be located in the root dir or in a subdir, or both;
This Directory.GetFiles() method works when run as standalone, like this:



However, when I try it like this, in a Sub...

CODE

'Dim lsRootDir As String = "C:\Temp", laRes(0) As String, lsExt As String = "*.XML, *.LOG, *.PS"

GetAllFiles(lsRootDir, lsExt, laRes)

End Sub
'====================================================================================================================================

====================================================================================================================================
Private Sub GetAllFiles(ByVal tsRootDir As String, ByVal tsExt As String, ByRef taRet() As String)
'====================================================================================================================================
Dim laExt() As String, I As Integer, liCnt As Integer, lcExt As String

laExt = tsExt.Split(CType(",", Char()), StringSplitOptions.RemoveEmptyEntries)

liCnt = laExt.Length - 1

For I = 0 To liCnt
	lcExt = laExt(I)
	Dim laRes() As String = Directory.GetFiles(tsRootDir, lcExt, SearchOption.AllDirectories)

	If laRes.Length > 0 Then
		taRet = taRet.Concat(laRes).ToArray()
	End If

Next

End Sub
'==================================================================================================================================== 

... it gives me only the files in subdir, but not the files in the root dir:



I've been struggling with this problem for 3 workdays already - and can't figure out why it works as standalone, but doesn't in a Sub...

What am I doing wrong?

Regards,

Ilya

RE: Directory.GetFiles() misbehaves

remove or handle the leading spaces between the commas
i.e. instead of

CODE

lsExt As String = "*.XML, *.LOG, *.PS" 
use

CODE

lsExt As String = "*.XML,*.LOG,*.PS" 

RE: Directory.GetFiles() misbehaves

if you want to have the file-extensions-string with spaces, i.e.:

CODE

lsExt As String = "*.XML, *.LOG, *.PS" 
then you can remove the spaces in your for-loop

CODE

For I = 0 To liCnt
    lcExt = laExt(I).Trim
    ...
Next 

RE: Directory.GetFiles() misbehaves

As well as the spaces that mikrom has mentioned, if your problem is also the Nothing entry, then this is caused by

laRes(0) As String

and the subsequent way you then concatenate arrays. Someething like this may work better.

CODE -->

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim results() As String
        GetAllFiles("d:\downloads\deleteme\", "*.XML,*.LOG,*.PS", results)
        ' now do what youlike with results() ...
    End Sub

    '====================================================================================================================================
    Private Sub GetAllFiles(ByVal tsRootDir As String, ByVal tsExt As String, ByRef taRet() As String)
        '====================================================================================================================================
        Dim laExt() As String, I As Integer, liCnt As Integer, lcExt As String

        laExt = tsExt.Split(CType(",", Char()), StringSplitOptions.RemoveEmptyEntries)

        liCnt = laExt.Length - 1
        For I = 0 To liCnt
            lcExt = laExt(I)
            Dim laRes() As String = Directory.GetFiles(tsRootDir, lcExt, SearchOption.AllDirectories)
            If laRes.Length > 0 Then
                If taRet Is Nothing Then
                    taRet = laRes
                Else
                    taRet = taRet.Concat(laRes).ToArray()
                End If
            End If
        Next
    End Sub 

Or even

CODE -->

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Dim lsExt = {"*.XML", "*.LOG", "*.PS"}
       Dim results() As String
       GetAllFiles("d:\downloads\deleteme\", lsExt, results)
       ' now do what youlike with results() ...
   End Sub

   '====================================================================================================================================
   Private Sub GetAllFiles(ByVal tsRootDir As String, ByVal laExt() As String, ByRef taRet() As String)
       '====================================================================================================================================
       Dim I As Integer, liCnt As Integer, lcExt As String

       liCnt = laExt.Length - 1
       For I = 0 To liCnt
           lcExt = laExt(I)
           Dim laRes() As String = Directory.GetFiles(tsRootDir, lcExt, SearchOption.AllDirectories)
           If laRes.Length > 0 Then
               If taRet Is Nothing Then
                   taRet = laRes
               Else
                   taRet = taRet.Concat(laRes).ToArray()
               End If
           End If
       Next
   End Sub 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close