By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,010 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,010 IT Pros & Developers. It's quick & easy.

File.Exists and wildcards

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
With Deft Fingers, John Dann <ne**@prodata.co.uk> wrote:
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
Nov 21 '05 #2

P: n/a
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

"John Dann" wrote:
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

Nov 21 '05 #3

P: n/a
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

"John Dann" <ne**@prodata.co.uk> wrote in message
news:57********************************@4ax.com...
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
Nov 21 '05 #4

P: n/a

"John Dann" <ne**@prodata.co.uk> wrote
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
Nov 21 '05 #5

P: n/a
On Sat, 2 Oct 2004 21:35:28 +0530, "Shiva"
<sh******@online.excite.com> wrote:
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
Nov 21 '05 #6

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

"John Dann" <ne**@prodata.co.uk> wrote in message
news:tj********************************@4ax.com...
On Sat, 2 Oct 2004 21:35:28 +0530, "Shiva"
<sh******@online.excite.com> wrote:
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
Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.