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