Absolute Novice looking for a way to decipher File Name

  • Thread starter Thread starter PSULionRP
  • Start date Start date
P

PSULionRP

An Absolute Novice here in the VB.Net world. I just ordered a couple of books
this week. (Does anyone have any suggestions for some good learning books???)

I have been tasked to enhance this VB.Net Application to decipher the file
names we are picking up to ensure the names follow convention and if the
don't, send an e-mail.

The Application is using these dynamics, I use the word "dynamics" because I
don't know what else to call it, to get the files in their appropriate Path.

Dim files() As String = Directory.GetFiles(_localDir)

Are there similar dynamic VB.Net commands to decipher the Filename then? I
know, also, that I want to use "RegEx" to determine if the file name follows
our conventions and standards.

Any help would be appreciated and Thanks in advance for your review and am
hopeful for a reply.

Thanks.

PSULionRP
 
PSULionRP said:
An Absolute Novice here in the VB.Net world. I just ordered a couple of books
this week. (Does anyone have any suggestions for some good learning books???)

I've always read the manual. [F1]
I have been tasked to enhance this VB.Net Application to decipher the file
names we are picking up to ensure the names follow convention and if the
don't, send an e-mail.

The Application is using these dynamics, I use the word "dynamics" because I
don't know what else to call it, to get the files in their appropriate Path.

Dim files() As String = Directory.GetFiles(_localDir)

Are there similar dynamic VB.Net commands to decipher the Filename then? I
know, also, that I want to use "RegEx" to determine if the file name follows
our conventions and standards.

Yes, you can use regular expressions. Have a look at
System.Text.RegularExpressions.Regex
and it's documentation.
 
Take a look at the System.IO.Path class for routines to break filenames and
paths into components and put them back together.
 
Are there similar dynamic VB.Net commands to decipher the Filename
then? I know, also, that I want to use "RegEx" to determine if the
file name follows our conventions and standards.

You will just have to learn enough Regex to make a pattern that fits the
correct file naming convention. Then use the Regex class, add the
pattern and test the file name(s).

For example, suppose you have DailyFile09222009.txt and have something
like this:

Dim reg as New Regex(^DailyFile[01]\d{1}[0123]\d{1}2\d{3}.txt$")

This one is off the top of my head, and can be simplified. I
purposefully left the full explicit qualification for numbers \d{1} so
you could see that the file \d is 3 digits long.

All of the following are valid with this Regex:

DailyFile01012000.txt
DailyFile12312999.txt

Changing the placement of the date, the header for the file name or the
file extension will end up with a fail, so all of these are incorrect:

Daily09242009.txt
DailyFile09242009.log

etc., as is a short two digit year:

DailyFile092409.txt

You will have to figure out your particular needs. Here is the entire
routine:

Private Function IsFileNameToConvention(ByVal fileName as String) _
As Boolean

Dim reg as New Regex(^DailyFile[01]\d{1}[0123]\d{1}2\d{3}.txt$")
Return reg.Match(fileName).Success

End Function

I am sure it can be improved on.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Do yourself a favor and get Expresso from UltraPico (it's free) to help you
experiment with Regular Expressions.

Bob
 
Back
Top