Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Directory.GetFiles() misbehaves 1

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
566
0
16
US
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:

2024-06-11_DirectoryGetFilesStandaloneWorks_oehmvg.jpg


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:

2024-06-11_DirectoryGetFilesStandaloneWorksNot_qna3hq.jpg


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
 
remove or handle the leading spaces between the commas
i.e. instead of
Code:
lsExt As String = "*.XML,[highlight #FCE94F] [/highlight]*.LOG,[highlight #FCE94F] [/highlight]*.PS"
use
Code:
lsExt As String = "*.XML,*.LOG,*.PS"
 
if you want to have the file-extensions-string with spaces, i.e.:
Code:
lsExt As String = "*.XML,[highlight #FCE94F] [/highlight]*.LOG,[highlight #FCE94F] [/highlight]*.PS"
then you can remove the spaces in your for-loop
Code:
For I = 0 To liCnt
    lcExt = laExt(I)[highlight #FCE94F].Trim[/highlight]
    ...
Next
 
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:
[COLOR=blue]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)
        [COLOR=green]' now do what youlike with results() ...[/color]
    End Sub

[COLOR=green]    '====================================================================================================================================[/color]
    Private Sub GetAllFiles(ByVal tsRootDir As String, ByVal tsExt As String, ByRef taRet() As String)
[COLOR=green]        '====================================================================================================================================[/color]
        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[/color]

Or even

Code:
[COLOR=blue]   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)
       [COLOR=green]' now do what youlike with results() ...[/color]
   End Sub

  [COLOR=green] '====================================================================================================================================[/color]
   Private Sub GetAllFiles(ByVal tsRootDir As String, ByVal laExt() As String, ByRef taRet() As String)
[COLOR=green]       '====================================================================================================================================[/color]
       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[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top