473,385 Members | 1,372 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,385 software developers and data experts.

counting files that contain a certain string in the filename?

I need to count files in a certain directory that has the string -Contract
Template.xml at the end of it. How would I do this?

Jun 30 '08 #1
15 1390
Try something like:

Dim _files = Directory.GetFiles(_path)

Dim _count = 0

For Each _file In _files
If _file.EndsWith("-Contract Template.xml") then _count += 1
Next
"Andy B" <a_*****@sbcglobal.netwrote in message
news:e1**************@TK2MSFTNGP04.phx.gbl...
>I need to count files in a certain directory that has the string -Contract
Template.xml at the end of it. How would I do this?
Jun 30 '08 #2
On Jun 29, 9:28*pm, "Stephany Young" <noone@localhostwrote:
Try something like:

* Dim _files = Directory.GetFiles(_path)

* Dim _count = 0

* For Each _file In _files
* * If _file.EndsWith("-Contract Template.xml") then _count += 1
* Next

"Andy B" <a_bo...@sbcglobal.netwrote in message

news:e1**************@TK2MSFTNGP04.phx.gbl...
I need to count files in a certain directory that has the string -Contract
Template.xml at the end of it. How would I do this?
Or you could do it without the loop:

/////////////////////////
Dim fileCount As Integer = Directory.GetFiles(path, "*-Contract
Template.xml").Length
/////////////////////////

You also have the option to search sub directories if need be:

/////////////////////////
Dim totalFileCount As Integer = Directory.GetFiles(path, "*-Contract
Template.xml", SearchOption.AllDirectories).Length
/////////////////////////

Thanks,

Seth Rowe [MVP]
Jun 30 '08 #3
Yes, correct Seth, but you could get bitten by the undesirable 3 character
extension behaviour that is documented in the documentation for the
Directory.GetFiles method.

Given a directory with files named:

A123-Contract Template.xml
A123-Contract Template.xmla

using:

Directory.GetFiles(path, "*-Contract Template.xml").Length

would result in a count of 2, whereas using a loop will get the correct
count.
"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:37**********************************@i76g2000 hsf.googlegroups.com...
On Jun 29, 9:28 pm, "Stephany Young" <noone@localhostwrote:
Try something like:

Dim _files = Directory.GetFiles(_path)

Dim _count = 0

For Each _file In _files
If _file.EndsWith("-Contract Template.xml") then _count += 1
Next

"Andy B" <a_bo...@sbcglobal.netwrote in message

news:e1**************@TK2MSFTNGP04.phx.gbl...
I need to count files in a certain directory that has the
string -Contract
Template.xml at the end of it. How would I do this?
Or you could do it without the loop:

/////////////////////////
Dim fileCount As Integer = Directory.GetFiles(path, "*-Contract
Template.xml").Length
/////////////////////////

You also have the option to search sub directories if need be:

/////////////////////////
Dim totalFileCount As Integer = Directory.GetFiles(path, "*-Contract
Template.xml", SearchOption.AllDirectories).Length
/////////////////////////

Thanks,

Seth Rowe [MVP]

Jun 30 '08 #4
Yes, correct Seth, but you could get bitten by the undesirable 3 character
extension behaviour that is documented in the documentation for the
Directory.GetFiles method.

Given a directory with files named:

* A123-Contract Template.xml
* A123-Contract Template.xmla

using:

* Directory.GetFiles(path, "*-Contract Template.xml").Length

would result in a count of 2, whereas using a loop will get the correct
count.
Ahh, very interesting. I guess I would have known about that one if I
ever read the documentation :-)

That also explain why you posted the for...loop and not a sample using
the overload, as with your experience I knew you would know about the
passing in the search pattern. Thanks again for explaining why the
overload is dangerous and also why you posted what you did.

Thanks,

Seth Rowe [MVP]
Jun 30 '08 #5
This is right. I needed something with the EndsWith method since I wanted to
ignore files with extensions like xmla and all those oddballs that would
possibly pose as a security problem as well as invalid file formats. Only
files that end in "-Contract Template.xml" are valid filenames. The next
part of a valid file for loading is to make sure the files with the
"-Contract Template.xml" ending conform to a particular xml schema that is
created with a dataset. Just having a particular ending won't work. Since I
have the filename test done (it did work btw), now it's time to move on to
the valid xml schema is/is not valid test. Another post said to just try
loading the files into a dataset in a try...catch block. Is this right? or
is there a better way to do it?
"Stephany Young" <noone@localhostwrote in message
news:uz**************@TK2MSFTNGP05.phx.gbl...
Yes, correct Seth, but you could get bitten by the undesirable 3 character
extension behaviour that is documented in the documentation for the
Directory.GetFiles method.

Given a directory with files named:

A123-Contract Template.xml
A123-Contract Template.xmla

using:

Directory.GetFiles(path, "*-Contract Template.xml").Length

would result in a count of 2, whereas using a loop will get the correct
count.
"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:37**********************************@i76g2000 hsf.googlegroups.com...
On Jun 29, 9:28 pm, "Stephany Young" <noone@localhostwrote:
>Try something like:

Dim _files = Directory.GetFiles(_path)

Dim _count = 0

For Each _file In _files
If _file.EndsWith("-Contract Template.xml") then _count += 1
Next

"Andy B" <a_bo...@sbcglobal.netwrote in message

news:e1**************@TK2MSFTNGP04.phx.gbl...
>I need to count files in a certain directory that has the
string -Contract
Template.xml at the end of it. How would I do this?

Or you could do it without the loop:

/////////////////////////
Dim fileCount As Integer = Directory.GetFiles(path, "*-Contract
Template.xml").Length
/////////////////////////

You also have the option to search sub directories if need be:

/////////////////////////
Dim totalFileCount As Integer = Directory.GetFiles(path, "*-Contract
Template.xml", SearchOption.AllDirectories).Length
/////////////////////////

Thanks,

Seth Rowe [MVP]

Jun 30 '08 #6
On Jun 30, 9:48*am, "Andy B" <a_bo...@sbcglobal.netwrote:
This is right. I needed something with the EndsWith method since I wanted to
ignore files with extensions like xmla and all those oddballs that would
possibly pose as a security problem as well as invalid file formats. Only
files that end in "-Contract Template.xml" are valid filenames. The next
part of a valid file for loading is to make sure the files with the
"-Contract Template.xml" ending conform to a particular xml schema that is
created with a dataset. Just having a particular ending won't work. Since I
have the filename test done (it did work btw), now it's time to move on to
the valid xml schema is/is not valid test. Another post said to just try
loading the files into a dataset in a try...catch block. Is this right? or
is there a better way to do it?

"Stephany Young" <noone@localhostwrote in message

news:uz**************@TK2MSFTNGP05.phx.gbl...
Yes, correct Seth, but you could get bitten by the undesirable 3 character
extension behaviour that is documented in the documentation for the
Directory.GetFiles method.
Given a directory with files named:
*A123-Contract Template.xml
*A123-Contract Template.xmla
using:
*Directory.GetFiles(path, "*-Contract Template.xml").Length
would result in a count of 2, whereas using a loop will get the correct
count.
"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message
news:37**********************************@i76g2000 hsf.googlegroups.com...
On Jun 29, 9:28 pm, "Stephany Young" <noone@localhostwrote:
Try something like:
Dim _files = Directory.GetFiles(_path)
Dim _count = 0
For Each _file In _files
If _file.EndsWith("-Contract Template.xml") then _count += 1
Next
"Andy B" <a_bo...@sbcglobal.netwrote in message
>news:e1**************@TK2MSFTNGP04.phx.gbl...
I need to count files in a certain directory that has the
string -Contract
Template.xml at the end of it. How would I do this?
Or you could do it without the loop:
/////////////////////////
Dim fileCount As Integer = Directory.GetFiles(path, "*-Contract
Template.xml").Length
/////////////////////////
You also have the option to search sub directories if need be:
/////////////////////////
Dim totalFileCount As Integer = Directory.GetFiles(path, "*-Contract
Template.xml", SearchOption.AllDirectories).Length
/////////////////////////
Thanks,
Seth Rowe [MVP]
.NET actually exposes a class / method to validate an Xml file against
a schema. It's been a long, long time since I've used it, but it's
called something like XmlSchemaValidation. If you can't find it let me
know and I'll try to find the project I used it on. This should be
substantially lighter weight than creating a dataset/datatable and
using a try...catch block.

Thanks,

Seth Rowe [MVP]
Jun 30 '08 #7
On Jun 30, 11:34*am, rowe_newsgroups <rowe_em...@yahoo.comwrote:
On Jun 30, 9:48*am, "Andy B" <a_bo...@sbcglobal.netwrote:
This is right. I needed something with the EndsWith method since I wanted to
ignore files with extensions like xmla and all those oddballs that would
possibly pose as a security problem as well as invalid file formats. Only
files that end in "-Contract Template.xml" are valid filenames. The next
part of a valid file for loading is to make sure the files with the
"-Contract Template.xml" ending conform to a particular xml schema that is
created with a dataset. Just having a particular ending won't work. Since I
have the filename test done (it did work btw), now it's time to move on to
the valid xml schema is/is not valid test. Another post said to just try
loading the files into a dataset in a try...catch block. Is this right? or
is there a better way to do it?
"Stephany Young" <noone@localhostwrote in message
news:uz**************@TK2MSFTNGP05.phx.gbl...
Yes, correct Seth, but you could get bitten by the undesirable 3 character
extension behaviour that is documented in the documentation for the
Directory.GetFiles method.
Given a directory with files named:
*A123-Contract Template.xml
*A123-Contract Template.xmla
using:
*Directory.GetFiles(path, "*-Contract Template.xml").Length
would result in a count of 2, whereas using a loop will get the correct
count.
"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message
>news:37**********************************@i76g200 0hsf.googlegroups.com....
On Jun 29, 9:28 pm, "Stephany Young" <noone@localhostwrote:
>Try something like:
>Dim _files = Directory.GetFiles(_path)
>Dim _count = 0
>For Each _file In _files
>If _file.EndsWith("-Contract Template.xml") then _count += 1
>Next
>"Andy B" <a_bo...@sbcglobal.netwrote in message
>>news:e1**************@TK2MSFTNGP04.phx.gbl...
>I need to count files in a certain directory that has the
>string -Contract
>Template.xml at the end of it. How would I do this?
Or you could do it without the loop:
/////////////////////////
Dim fileCount As Integer = Directory.GetFiles(path, "*-Contract
Template.xml").Length
/////////////////////////
You also have the option to search sub directories if need be:
/////////////////////////
Dim totalFileCount As Integer = Directory.GetFiles(path, "*-Contract
Template.xml", SearchOption.AllDirectories).Length
/////////////////////////
Thanks,
Seth Rowe [MVP]

.NET actually exposes a class / method to validate an Xml file against
a schema. It's been a long, long time since I've used it, but it's
called something like XmlSchemaValidation. If you can't find it let me
know and I'll try to find the project I used it on. This should be
substantially lighter weight than creating a dataset/datatable and
using a try...catch block.

Thanks,

Seth Rowe [MVP]
Or maybe it was XmlValidatingReader, I don't really remember :-(

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Jun 30 '08 #8
Stephany,
I would consider combining the methods:

Dim _files = Directory.GetFiles(_path, "*-Contract Template.xml")
Dim _count = 0

For Each _file In _files
If _file.EndsWith("-Contract Template.xml") then _count += 1
Next

As this lets the OS do the heavy lifting for the bulk of the files names in
a folder allowing the for each to iterate over a subset of all files on
disk. Read Directory.GetFiles will return a smaller array...

I would also consider using LINQ:

Dim _count = Aggregate file In ( _
From file In Directory.GetFiles(path, "*" &
"-Contract Template.xml") _
Where file.EndsWith("-Contract Template.xml") _
) Into Count()
--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stephany Young" <noone@localhostwrote in message
news:eW**************@TK2MSFTNGP06.phx.gbl...
Try something like:

Dim _files = Directory.GetFiles(_path)

Dim _count = 0

For Each _file In _files
If _file.EndsWith("-Contract Template.xml") then _count += 1
Next
"Andy B" <a_*****@sbcglobal.netwrote in message
news:e1**************@TK2MSFTNGP04.phx.gbl...
>>I need to count files in a certain directory that has the string -Contract
Template.xml at the end of it. How would I do this?
Jun 30 '08 #9
Well, yes Jay. You could also consider a myriad of other techniques as
well. Not that I had said 'try something like', meaning 'similar to' or 'a
variation of'.

I attempted to post a follow up to another post of mine on another branch of
this thread, but for some reason or another it doesn't appear in the thread
(in my reader anyway), that pointed out the gotcha when using the * wildcard
character with Directory.GetFiles(_path, _pattern) and the _pattern
specifies a 3 character extension, e.g., Directory.GetFiles(_path, "*.xml").
This gotcha is spelled out in the documentation for the
Directory.GetFiles(_path, _pattern) method.

I had always interpreted that as meaning that the gotcha applied if one used
the * wildcard character anywhere to the left of the . character. However, a
test I executed a few hours ago shows that it only apply when the pattern
is, in fact, in the form "*.<3 character extension>".

In the OP's case, this renders the loop redundant because:

Directory.GetFiles(_path, "*-Contract Template.xml")

will return the correct result.

If the call was:

Directory.GetFiles(_path, "*.xml")

then the result would be incorrect if, and only if, the directory contained
other files with extensions that start with .xml, e.g., .xmla, .xmlb, etc.

Even we old hands can stand educated sometimes :)

Now I certainly appreciate your enthusiasm and evangelism and although it is
a valid technique, don't you think LINQ is a bit over the top for the task
at hand. :)
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:uj****************@TK2MSFTNGP02.phx.gbl...
Stephany,
I would consider combining the methods:

Dim _files = Directory.GetFiles(_path, "*-Contract Template.xml")
Dim _count = 0

For Each _file In _files
If _file.EndsWith("-Contract Template.xml") then _count += 1
Next

As this lets the OS do the heavy lifting for the bulk of the files names
in a folder allowing the for each to iterate over a subset of all files on
disk. Read Directory.GetFiles will return a smaller array...

I would also consider using LINQ:

Dim _count = Aggregate file In ( _
From file In Directory.GetFiles(path, "*" &
"-Contract Template.xml") _
Where file.EndsWith("-Contract Template.xml") _
) Into Count()
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stephany Young" <noone@localhostwrote in message
news:eW**************@TK2MSFTNGP06.phx.gbl...
>Try something like:

Dim _files = Directory.GetFiles(_path)

Dim _count = 0

For Each _file In _files
If _file.EndsWith("-Contract Template.xml") then _count += 1
Next
"Andy B" <a_*****@sbcglobal.netwrote in message
news:e1**************@TK2MSFTNGP04.phx.gbl...
>>>I need to count files in a certain directory that has the
string -Contract Template.xml at the end of it. How would I do this?
Jul 1 '08 #10
Directory.GetFiles(_path, "*-Contract Template.xml")

If this is a better way of doing things, then I probably will try this way
instead. Makes more sense to use it this way since I have to find all the
files with that partial name and then validate them to make sure they aren't
a fake of some kind.

"Stephany Young" <noone@localhostwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Well, yes Jay. You could also consider a myriad of other techniques as
well. Not that I had said 'try something like', meaning 'similar to' or
'a variation of'.

I attempted to post a follow up to another post of mine on another branch
of this thread, but for some reason or another it doesn't appear in the
thread (in my reader anyway), that pointed out the gotcha when using the *
wildcard character with Directory.GetFiles(_path, _pattern) and the
_pattern specifies a 3 character extension, e.g.,
Directory.GetFiles(_path, "*.xml"). This gotcha is spelled out in the
documentation for the Directory.GetFiles(_path, _pattern) method.

I had always interpreted that as meaning that the gotcha applied if one
used the * wildcard character anywhere to the left of the . character.
However, a test I executed a few hours ago shows that it only apply when
the pattern is, in fact, in the form "*.<3 character extension>".

In the OP's case, this renders the loop redundant because:

Directory.GetFiles(_path, "*-Contract Template.xml")

will return the correct result.

If the call was:

Directory.GetFiles(_path, "*.xml")

then the result would be incorrect if, and only if, the directory
contained other files with extensions that start with .xml, e.g., .xmla,
.xmlb, etc.

Even we old hands can stand educated sometimes :)

Now I certainly appreciate your enthusiasm and evangelism and although it
is a valid technique, don't you think LINQ is a bit over the top for the
task at hand. :)
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:uj****************@TK2MSFTNGP02.phx.gbl...
>Stephany,
I would consider combining the methods:

Dim _files = Directory.GetFiles(_path, "*-Contract Template.xml")
Dim _count = 0

For Each _file In _files
If _file.EndsWith("-Contract Template.xml") then _count += 1
Next

As this lets the OS do the heavy lifting for the bulk of the files names
in a folder allowing the for each to iterate over a subset of all files
on disk. Read Directory.GetFiles will return a smaller array...

I would also consider using LINQ:

Dim _count = Aggregate file In ( _
From file In Directory.GetFiles(path, "*" &
"-Contract Template.xml") _
Where file.EndsWith("-Contract Template.xml") _
) Into Count()
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stephany Young" <noone@localhostwrote in message
news:eW**************@TK2MSFTNGP06.phx.gbl...
>>Try something like:

Dim _files = Directory.GetFiles(_path)

Dim _count = 0

For Each _file In _files
If _file.EndsWith("-Contract Template.xml") then _count += 1
Next
"Andy B" <a_*****@sbcglobal.netwrote in message
news:e1**************@TK2MSFTNGP04.phx.gbl...
I need to count files in a certain directory that has the
string -Contract Template.xml at the end of it. How would I do this?


Jul 1 '08 #11
In the OP's case, this renders the loop redundant because:
>
* Directory.GetFiles(_path, "*-Contract Template.xml")

will return the correct result.

If the call was:

* Directory.GetFiles(_path, "*.xml")

then the result would be incorrect if, and only if, the directory contained
other files with extensions that start with .xml, e.g., .xmla, .xmlb, etc.
Not sure about everyone else, but I am now thoroughly confused!

I guess I'll just wander back to my web development world and be happy
I rarely have to deal with Directory.GetFiles(...)

:-)

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/

Jul 1 '08 #12
Sorry to get you confused *sigh*... Didn't want to do anything like that...
"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:2b**********************************@m44g2000 hsc.googlegroups.com...
In the OP's case, this renders the loop redundant because:

Directory.GetFiles(_path, "*-Contract Template.xml")

will return the correct result.

If the call was:

Directory.GetFiles(_path, "*.xml")

then the result would be incorrect if, and only if, the directory
contained
other files with extensions that start with .xml, e.g., .xmla, .xmlb, etc.
Not sure about everyone else, but I am now thoroughly confused!

I guess I'll just wander back to my web development world and be happy
I rarely have to deal with Directory.GetFiles(...)

:-)

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Jul 1 '08 #13
I would keep the loop for the day when Andy's successor changes the criteria
and breaks the algorithm. Of course there is a chance Andy's successor

I don't consider the LINQ over the top as I tend to prefer stating what I
want to do (aggregate the count of files) rather then how specifically to do
it... Also using LINQ its now easy to add other accumulators, such as total
size of the files...

Of course there's the danger of "learn how to use a hammer, and every thing
is a nail"...

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stephany Young" <noone@localhostwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Well, yes Jay. You could also consider a myriad of other techniques as
well. Not that I had said 'try something like', meaning 'similar to' or
'a variation of'.

I attempted to post a follow up to another post of mine on another branch
of this thread, but for some reason or another it doesn't appear in the
thread (in my reader anyway), that pointed out the gotcha when using the *
wildcard character with Directory.GetFiles(_path, _pattern) and the
_pattern specifies a 3 character extension, e.g.,
Directory.GetFiles(_path, "*.xml"). This gotcha is spelled out in the
documentation for the Directory.GetFiles(_path, _pattern) method.

I had always interpreted that as meaning that the gotcha applied if one
used the * wildcard character anywhere to the left of the . character.
However, a test I executed a few hours ago shows that it only apply when
the pattern is, in fact, in the form "*.<3 character extension>".

In the OP's case, this renders the loop redundant because:

Directory.GetFiles(_path, "*-Contract Template.xml")

will return the correct result.

If the call was:

Directory.GetFiles(_path, "*.xml")

then the result would be incorrect if, and only if, the directory
contained other files with extensions that start with .xml, e.g., .xmla,
.xmlb, etc.

Even we old hands can stand educated sometimes :)

Now I certainly appreciate your enthusiasm and evangelism and although it
is a valid technique, don't you think LINQ is a bit over the top for the
task at hand. :)
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:uj****************@TK2MSFTNGP02.phx.gbl...
>Stephany,
I would consider combining the methods:

Dim _files = Directory.GetFiles(_path, "*-Contract Template.xml")
Dim _count = 0

For Each _file In _files
If _file.EndsWith("-Contract Template.xml") then _count += 1
Next

As this lets the OS do the heavy lifting for the bulk of the files names
in a folder allowing the for each to iterate over a subset of all files
on disk. Read Directory.GetFiles will return a smaller array...

I would also consider using LINQ:

Dim _count = Aggregate file In ( _
From file In Directory.GetFiles(path, "*" &
"-Contract Template.xml") _
Where file.EndsWith("-Contract Template.xml") _
) Into Count()
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stephany Young" <noone@localhostwrote in message
news:eW**************@TK2MSFTNGP06.phx.gbl...
>>Try something like:

Dim _files = Directory.GetFiles(_path)

Dim _count = 0

For Each _file In _files
If _file.EndsWith("-Contract Template.xml") then _count += 1
Next
"Andy B" <a_*****@sbcglobal.netwrote in message
news:e1**************@TK2MSFTNGP04.phx.gbl...
I need to count files in a certain directory that has the
string -Contract Template.xml at the end of it. How would I do this?

Jul 1 '08 #14
Jay
>
Of course there's the danger of "learn how to use a hammer, and every
thing is a nail"...
Very good one, I did not know it, I will for sure use it in future,

Cor
Jul 2 '08 #15
Actually, I won't have a successor...
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:uM**************@TK2MSFTNGP06.phx.gbl...
>I would keep the loop for the day when Andy's successor changes the
criteria and breaks the algorithm. Of course there is a chance Andy's
successor

I don't consider the LINQ over the top as I tend to prefer stating what I
want to do (aggregate the count of files) rather then how specifically to
do it... Also using LINQ its now easy to add other accumulators, such as
total size of the files...

Of course there's the danger of "learn how to use a hammer, and every
thing is a nail"...

--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stephany Young" <noone@localhostwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>Well, yes Jay. You could also consider a myriad of other techniques as
well. Not that I had said 'try something like', meaning 'similar to' or
'a variation of'.

I attempted to post a follow up to another post of mine on another branch
of this thread, but for some reason or another it doesn't appear in the
thread (in my reader anyway), that pointed out the gotcha when using the
* wildcard character with Directory.GetFiles(_path, _pattern) and the
_pattern specifies a 3 character extension, e.g.,
Directory.GetFiles(_path, "*.xml"). This gotcha is spelled out in the
documentation for the Directory.GetFiles(_path, _pattern) method.

I had always interpreted that as meaning that the gotcha applied if one
used the * wildcard character anywhere to the left of the . character.
However, a test I executed a few hours ago shows that it only apply when
the pattern is, in fact, in the form "*.<3 character extension>".

In the OP's case, this renders the loop redundant because:

Directory.GetFiles(_path, "*-Contract Template.xml")

will return the correct result.

If the call was:

Directory.GetFiles(_path, "*.xml")

then the result would be incorrect if, and only if, the directory
contained other files with extensions that start with .xml, e.g., .xmla,
.xmlb, etc.

Even we old hands can stand educated sometimes :)

Now I certainly appreciate your enthusiasm and evangelism and although it
is a valid technique, don't you think LINQ is a bit over the top for the
task at hand. :)
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:uj****************@TK2MSFTNGP02.phx.gbl...
>>Stephany,
I would consider combining the methods:

Dim _files = Directory.GetFiles(_path, "*-Contract Template.xml")
Dim _count = 0

For Each _file In _files
If _file.EndsWith("-Contract Template.xml") then _count += 1
Next

As this lets the OS do the heavy lifting for the bulk of the files names
in a folder allowing the for each to iterate over a subset of all files
on disk. Read Directory.GetFiles will return a smaller array...

I would also consider using LINQ:

Dim _count = Aggregate file In ( _
From file In Directory.GetFiles(path, "*" &
"-Contract Template.xml") _
Where file.EndsWith("-Contract Template.xml") _
) Into Count()
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stephany Young" <noone@localhostwrote in message
news:eW**************@TK2MSFTNGP06.phx.gbl...
Try something like:

Dim _files = Directory.GetFiles(_path)

Dim _count = 0

For Each _file In _files
If _file.EndsWith("-Contract Template.xml") then _count += 1
Next
"Andy B" <a_*****@sbcglobal.netwrote in message
news:e1**************@TK2MSFTNGP04.phx.gbl...
>I need to count files in a certain directory that has the
>string -Contract Template.xml at the end of it. How would I do this?
>
>
>


Jul 2 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ben | last post by:
Greetings, I am looking for a way to search for and delete files based on a pattern mask. For example, the search method would find all files matching a certain pattern containing wildcards (e.g....
1
by: hokiegal99 | last post by:
This is not really a Python-centric question, however, I am using Python to solve this problem (as of now) so I thought it appropiate to pose the question here. I have some functions that search...
3
by: Ron Nolan | last post by:
Re: Access 2000 How can I loop through a folder on the user's hard drive, and check to see if it is an .mdb file. If it is an .mdb, do some stuff, else go to the next filename? Can this be...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
2
by: Tim Zych | last post by:
How would I go about storing data files on my web host? I have an Access database driven website and I would like to save files and be able to download them. Can somebody point me in the right...
8
by: Edward Bird | last post by:
Greetings, I'm writing an application that will rename any files on my harddrive that contain the underscore character _ and replace it with a space character ' '. However I've looked in the...
5
by: Jim | last post by:
Hello, I am working on a small windows application for a client, and as one of the functions they want a search that will let them enter a search string, then search a directory for all flies...
4
by: tim | last post by:
Hi all! How to create array of string, which contains names of all files with full path in special folder (e.g. c:\windows). In this folder it may be subfolders of various levels.
7
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.