Re: counting files that contain a certain string in the filename?



On Jun 30, 9:48 am, "Andy B" <a_bo...@xxxxxxxxxxxxx> 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:uzs7yeq2IHA.3780@xxxxxxxxxxxxxxxxxxxxxxx

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...@xxxxxxxxx> wrote in message
news:37fc6de7-a5eb-4506-ac67-69d52c199121@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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...@xxxxxxxxxxxxx> wrote in message

news:e127w2k2IHA.4672@xxxxxxxxxxxxxxxxxxxxxxx

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



Relevant Pages

  • Re: counting files that contain a certain string in the filename?
    ... ignore files with extensions like xmla and all those oddballs that would ... the valid xml schema is/is not valid test. ... Seth Rowe [MVP] ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Documentation
    ... I propose to BAN all extensions included with Tcl distribution that do ... not have detailed documentation plus understandable examples of usage. ... Many of the packages sit there for years and nobody uses them. ...
    (comp.lang.tcl)
  • Re: Is there *any* real documentation to PyWin32?
    ... so perhaps that the documentation that comes with the ... anything which may exist that does a better job of explaining the ... the pywin32 extensions usually make it possible to translate pretty ... much directly into Python. ...
    (comp.lang.python)
  • Re: Tcl Extension Repository
    ... extensions that are not part of the quality-assured ActiveTcl distribution; ... documentation and demos - if I want an extension other than simply to run ... pre-existing code, I will always want the documentation and demos, and ... Getting the source via teapot ...
    (comp.lang.tcl)
  • Re: REPOST: Cannot remove class from schema with Win2k SP3
    ... extensions at will, ... the extensive documentation that says you shouldn't do that. ... > You get it everytime you go to create a new item in the schema, ... > Windows Server MVP ...
    (microsoft.public.win2000.active_directory)