GetFiles Pattern  
Author Message
thomsson





PostPosted: Visual Basic Express Edition, GetFiles Pattern Top

Hi,

I use the following code to get all *.tlg files of an specified directory:

folderName = OpenFolderDialog.SelectedPath
Files = Directory.GetFiles(folderName, "*.tlg")

Unfortunatelly, I've no idea, how to select files with different extensions.
This one isn't working:

Files = Directory.GetFiles(folderName, "*.tlg;*.txt")

How do I have to specify the searchpattern for GetFiles




Visual Studio Express Editions14  
 
 
NateV





PostPosted: Visual Basic Express Edition, GetFiles Pattern Top

Hi there,

As far as I am aware, the ability to select files with different extensions with a single call to GetFiles() is not available. According to the MSDN documentation (System.IO.DirectoryInfo.GetFiles()) the only wildcards that are accepted are "*" (placeholder for 0 or more characters) and " " (placeholder for exactly one character).

You'd require something like a logical OR operator and I don't think this is supported in GetFiles()....I did some playing myself with possible logical operators without luck.

Unfortunately, it looks like you'll need to make multiple calls to GetFiles() to do what you want to do.

Another alternative to making multiple GetFiles() calls is to do something like:

Dim directory As System.IO.DirectoryInfo = New System.IO.DirectoryInfo("C:\HTML")

Dim fileNames As List(Of String) = New List(Of String)

Dim files() As String

For Each file As System.IO.FileInfo In directory.GetFiles()
    If file.Extension = ".tlg" Or file.Extension = ".txt" Then
        fileNames.Add(file.Name) 'This is not the full path, only the name of the file
    End If
Next

files = fileNames.ToArray() 'If you want it in an array of strings


Note that this actually requires you to know what file extensions you'll be searching on, which may or may not be viable in your situation.

Hope that helps a bit, but sorry if it doesn't


 
 
thomsson





PostPosted: Visual Basic Express Edition, GetFiles Pattern Top

Thanks for your reply. I just thought it would be possible to select all files with corresponding extensions by one command.

But your solution also works!

 
 
jwooley





PostPosted: Visual Basic Express Edition, GetFiles Pattern Top

If you are ultimately looking for a single file and want to use the OpenFileDialog, you can use the .Filter property to specify multiple extension types.

As an alternative to the iterative approach, you could use 2 Directory.GetFiles and merge the results. You may want to test the options to see which is better. Here is one possible implementation:

Dim f1 As New System.Collections.Specialized.StringCollection

f1.AddRange(System.IO.Directory.GetFiles("C:\windows", "*.exe"))

f1.AddRange(System.IO.Directory.GetFiles("C:\windows", "*.log"))

For Each f As String In f1

Console.WriteLine(f)

Next

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx



 
 
betorres





PostPosted: Visual Basic Express Edition, GetFiles Pattern Top

I used GetFiles Method passing an array with the file extensions, like showing below

Dim strTypeFiles() As String = New String() {"*.pdf", "*.zip", "*.doc", "*.ppt", "*.xls", "*.htm", "*.mht", "*.gif"}
Dim strFolder As String = "C:\"

........

For Each strFileName As String In My.Computer.FileSystem.GetFiles(strFolder, FileIO.SearchOption.SearchTopLevelOnly, strTypeFiles)

...................

Next

It works pretty good, with only one call you get all files with different extension

Hope helps!!

Roberto Torres



 
 
danhood





PostPosted: Visual Basic Express Edition, GetFiles Pattern Top

Hi Roberto,

did you try to use GetFiles Method with multiple attachme Have a look at below code, I just cant figure out what is wrong, Can you bail me out from this **** situation.:)

Public Sub Attach()
Dim i, iCnt As Integer
Dim email As Outlook.MailItem = TryCast(Me.CreateItem _
(Outlook.OlItemType.olMailItem), Outlook.MailItem)
For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\EDC", FileIO.SearchOption.SearchTopLevelOnly, "Aberdeen")
If foundFile.Trim() <> "" Then
iCnt = foundFile.Length - 1
For i = 0 To iCnt
email.Attachments.Add(foundFile)
Next
End If
Next
End Sub


 
 
Hurm





PostPosted: Visual Basic Express Edition, GetFiles Pattern Top

hi, a bit late july 2007
i wanted to select .html and .htm files from a directory
with getFiles("*.html") and getFiles("*.htm")

Dim inf As New DirectoryInfo(Server.MapPath("../") & "SavedItems")
Dim StackFiles As New Stack()
For Each elem As FileInfo In inf.GetFiles("*.htm")
StackFiles.Push(elem)
Next
For Each elem As FileInfo In inf.GetFiles("*.html")
StackFiles.Push(elem)
Next


but i got the .html files double
with only inf.GetFiles("*.html") I don't get the .htm files
I know you can choose only one filetype for each call
it is not allowed for e.g. GetFiles("*.htm "&& "*.html"") or GetFiles("*.htm |*.html"") or something alike
so; only one filetype
&& only for the first 3 letters after the .
I hope this information is usefull for others who googled || AltavistadBaby over here
e.g
inf.GetFiles("*.htm") also gets the file blahblahblah.htmOMYGODMETO selected
(it is i think an old MSDOS (thnkybg) 8.3 i am still here problem)
|| (some gray dude from MS implemented this method)
happy ptogramming::: the hurm.