473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

File.Exists and wildcards

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
6 6655
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
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
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Geiregat Jonas | last post by:
is using if(open("file",O_EXCL) != -1){ printf("File does exists")}else{printf("file does not exists"); } a good way of checking if a file exists or not, if not how should I do it ?
18
by: Dan | last post by:
I have code like the following to test for existence of a file. I know the file is there, but File.Exists returns FALSE. The problem appears to be that the file is in a directory beneath "My...
2
by: Zeno Lee | last post by:
I'm using File.Exists to test a file on my C: drive. My program was strongly named and had caspol -af run on it to allow it to run from the network. There are 3 ways I am doing this: 1) Run...
2
by: Chris Fink | last post by:
I am using the System.IO.File class to determine if a file exists on a network share. The File.Exists method keeps returning false, even though the file does exist. The MSDN documentation...
4
by: DEWright_CA | last post by:
I am trying to see if a file exists in a virtual directory, and if so run a method. I try doing File.Exists and the method runs but the file isn't there. Is there a web version of File.Exists or...
3
by: Lou | last post by:
Question: I can't seem to get file.exists(filename) to return true when I search using wildcards, and I know there's a file in that dir with that extension. here's the path dim yesno as...
2
by: =?Utf-8?B?R29yZG9u?= | last post by:
Hi; What is the correct syntax for checking for a file's existence based on it's extension ? Example: Imports system.io Dim path as string = "C:\myDirectory\*.txt"
12
by: snow | last post by:
Hi All, I noticed if file path has a white space, for example "C:\my document \test.txt", the function File.Exists(filePath) always return false in release mode. How could I make this function...
7
by: sprash | last post by:
Newbie question: I'm trying to determine if a file physically exists regardless of the permissions on it Using File.Exists() returns false if it physically exists but the process does not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.