File.Exists and wildcards

J

John Dann

I need to check whether there are _any_ (ie one or more) files of a
specified file type in a folder. Will:

if file.exists(path & "*.typ") then ...

work reliably? If not then what's the best workaround please?

TIA
JGD
 
G

Guest

This should do the trick. Note that the GetFiles() method returns an array of
the matching files.

If (System.IO.Directory.GetFiles("c:\myfolder", "*.jpg").Length) > 1 Then
MsgBox("This folder contains some jpegs")
End If

- Rich
 
M

Mr. B

With Deft Fingers said:
I need to check whether there are _any_ (ie one or more) files of a
specified file type in a folder. Will:
if file.exists(path & "*.typ") then ...
work reliably? If not then what's the best workaround please?

Here's two chunks of code I have... the first looks for a specific file
Extension, CTB, and dumps the file names into a pulldown (combobox). Feel
free to modify it as you want if it is of any value...

This code ONLY looks in a Parent folder:

' Look for CTB's in Root CTB File Path
Dim CTBdirsRt() As String = Directory.GetFiles(CTBFilPth)
For Each CTBfilname In CTBdirsRt
CTBtestname = System.IO.Path.GetFileName(CTBfilname)
' Test for CTB files
Dim FilTest As String = CTBtestname.Remove(0, (Len(CTBtestname) - 3))
If UCase(FilTest) = "CTB" Then
cmbCTB.Items.Add(CTBtestname)
' Look for Default CTB file
If UCase(CTBtestname) = "OMICRONGROUP.CTB" Then
cmbCTB.Text = "OmicronGroup.ctb"
End If
End If
Next


This code looks for the same CTB files in any Child folder below the Parent:

' Look for CTB's in Sub-Folders of CTB File Path
' Root Folder name
Dim rootDi As New DirectoryInfo(CTBFilPth)
Dim di As DirectoryInfo
For Each di In rootDi.GetDirectories
Dim CTBdirs() As String = Directory.GetFiles(CTBFilPth & di.Name)
For Each CTBfilname In CTBdirs
CTBtestname = System.IO.Path.GetFileName(CTBfilname)
' Test for CTB files
Dim FilTest As String = CTBtestname.Remove(0, (Len(CTBtestname) - 3))
If UCase(FilTest) = "CTB" Then
cmbCTB.Items.Add(CTBtestname)
' Look for Default CTB file
If UCase(CTBtestname) = "OMICRONGROUP.CTB" Then
cmbCTB.Text = "OmicronGroup.ctb"
'Else : OmiCTBtest = "N" 'cmbCTB.Text = "OCG Not Found"
End If
End If
Next
Next


I couldn't figure out how to do both the Parent And Child folders with one
chunk of code.... but this works for me.

Regards,

Bruce
 
S

Shiva

File.Exists() checkes for the spcecified file.

This is one way to accomplish what you are looking for:

If (Directory.GetFiles(path, "*.typ").Length > 0) Then
'File(s) found
Else
'File(s) not found
End If

I need to check whether there are _any_ (ie one or more) files of a
specified file type in a folder. Will:

if file.exists(path & "*.typ") then ...

work reliably? If not then what's the best workaround please?

TIA
JGD
 
L

Larry Serflaten

John Dann said:
I need to check whether there are _any_ (ie one or more) files of a
specified file type in a folder. Will:

if file.exists(path & "*.typ") then ...

work reliably? If not then what's the best workaround please?


More like: (not tested)

Dim di as New DirectoryInfo("C:\Your\Path\Here")

If di.GetFiles("*.typ").Length > 0 Then
' Yep, they're here....
End if


HTH
LFS
 
J

John Dann

File.Exists() checkes for the spcecified file.

This is one way to accomplish what you are looking for:

If (Directory.GetFiles(path, "*.typ").Length > 0) Then

Many thanks, but one point of clarification: If the GetFiles method
returns an array of files, what does the .length method do in this
context. Is it essentially just returning the ubound of the array?
(Since the length of an array doesn't seem - to me at least - to have
any intrinsic meaning).

JGD
 
S

Shiva

Hi,
..Length just gives the numer of elements in the array. I used it just to
check the returned array is empty or not.

File.Exists() checkes for the spcecified file.

This is one way to accomplish what you are looking for:

If (Directory.GetFiles(path, "*.typ").Length > 0) Then

Many thanks, but one point of clarification: If the GetFiles method
returns an array of files, what does the .length method do in this
context. Is it essentially just returning the ubound of the array?
(Since the length of an array doesn't seem - to me at least - to have
any intrinsic meaning).

JGD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top