Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

counting files that contain a certain string in the filename?

3 views
Skip to first unread message

Andy B

unread,
Jun 29, 2008, 9:07:50 PM6/29/08
to
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?

Stephany Young

unread,
Jun 29, 2008, 9:28:47 PM6/29/08
to
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_b...@sbcglobal.net> wrote in message
news:e127w2k2...@TK2MSFTNGP04.phx.gbl...

rowe_newsgroups

unread,
Jun 30, 2008, 7:30:03 AM6/30/08
to
On Jun 29, 9:28 pm, "Stephany Young" <noone@localhost> wrote:
> 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.net> wrote in message

>
> news:e127w2k2...@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]

Stephany Young

unread,
Jun 30, 2008, 7:51:56 AM6/30/08
to
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_...@yahoo.com> wrote in message
news:37fc6de7-a5eb-4506...@i76g2000hsf.googlegroups.com...

rowe_newsgroups

unread,
Jun 30, 2008, 8:28:13 AM6/30/08
to
> 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]


Andy B

unread,
Jun 30, 2008, 9:48:54 AM6/30/08
to
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@localhost> wrote in message
news:uzs7yeq2...@TK2MSFTNGP05.phx.gbl...

rowe_newsgroups

unread,
Jun 30, 2008, 11:34:48 AM6/30/08
to
On Jun 30, 9:48 am, "Andy B" <a_bo...@sbcglobal.net> wrote:
> 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@localhost> wrote in message
>
> news:uzs7yeq2...@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.com> wrote in message

.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]

rowe_newsgroups

unread,
Jun 30, 2008, 11:40:59 AM6/30/08
to

Or maybe it was XmlValidatingReader, I don't really remember :-(

Thanks,

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

Jay B. Harlow [MVP - Outlook]

unread,
Jun 30, 2008, 7:04:54 PM6/30/08
to
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@localhost> wrote in message

news:eWIWmCl2...@TK2MSFTNGP06.phx.gbl...

Stephany Young

unread,
Jun 30, 2008, 9:35:54 PM6/30/08
to
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]" <Jay_Har...@tsbradley.net> wrote in
message news:ujzD%23Ww2I...@TK2MSFTNGP02.phx.gbl...

Andy B

unread,
Jul 1, 2008, 8:24:35 AM7/1/08
to
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@localhost> wrote in message

news:%23tOKPrx...@TK2MSFTNGP04.phx.gbl...

rowe_newsgroups

unread,
Jul 1, 2008, 9:23:18 AM7/1/08
to
> 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(...)

:-)

Andy B

unread,
Jul 1, 2008, 6:00:54 PM7/1/08
to
Sorry to get you confused *sigh*... Didn't want to do anything like that...


"rowe_newsgroups" <rowe_...@yahoo.com> wrote in message

news:2b7f1a95-85e6-4c1e...@m44g2000hsc.googlegroups.com...

Jay B. Harlow [MVP - Outlook]

unread,
Jul 1, 2008, 7:16:24 PM7/1/08
to
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@localhost> wrote in message

news:%23tOKPrx...@TK2MSFTNGP04.phx.gbl...

Cor Ligthert [MVP]

unread,
Jul 2, 2008, 6:35:28 AM7/2/08
to
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


Andy B

unread,
Jul 2, 2008, 5:07:47 PM7/2/08
to
Actually, I won't have a successor...


"Jay B. Harlow [MVP - Outlook]" <Jay_Har...@tsbradley.net> wrote in

message news:uM2uGC92...@TK2MSFTNGP06.phx.gbl...

0 new messages