A
Andy B
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?
Template.xml at the end of it. How would I do this?
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
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
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.
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?
.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]
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.
rowe_newsgroups said: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.
Very good one, I did not know it, I will for sure use it in future,Of course there's the danger of "learn how to use a hammer, and every
thing is a nail"...
Jay B. Harlow said: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