check if file names in folders meet criteria

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
Hello All -

What will be the best way to to check a directory of files (all in subfolders) to see if each filename has XXXXXX_XXXXXXXXX_ as a filename?

Example: All my files are named with the same amount of letters and numbers (ofcourse all the numbers are different) "AAABBB_999999999_pending". I need a way to to verify that all files have 6 letters followed by an underscore followed by 9 numbers and then followed by another underscore.

Would it be possible for someone to show me an exmaple :)

or is there a free program out there that can do this for me?

Thanks in advanced

daveofgv
 
Thank you for the reply. I have not used RegEx before... seems a little confusing. If I were to use - "([A-Z]|[a-z]){6}_[0-9]{9}_pending" and I took off the "pending" would it still validate all files that meet the criteria? Example: not all files end with "pending" (mulitple other words apply), but should have the same amount of letters and numbers before it along with the underscores.

also, with the supplied link you sent - where do I put the directory to check to make sure all files that reside in the directory and subfolders are validated?


VB.NET:
Dim sr As New StreamReader(filename)
Dim input As String 
Dim pattern As String = "([A-Z]|[a-z]){6}_[0-9]{9}_pending"
Do While sr.Peek() >= 0
   input = sr.ReadLine()
   Dim rgx As New Regex(pattern, RegexOptions.IgnoreCase)
   Dim matches As MatchCollection = rgx.Matches(input)
   If matches.Count > 0 Then
      Console.WriteLine("{0} ({1} matches):", input, matches.Count)
      For Each match As Match In matches
         Console.WriteLine("   " + match.Value)
      Next    
   End If 
Loop
sr.Close()

Thanks in advanced
 
Hi,

If I were to use - "([A-Z]|[a-z]){6}_[0-9]{9}_pending" and I took off the "pending" would it still validate all files that meet the criteria?

Yes

where do I put the directory to check to make sure all files that reside in the directory and subfolders are validated?

As your code stands, this is reading the CONTENTS of a single file and testing each line of that particular file to see if that line conforms to the specified RegEx expression??.

You have therefore confused me since you originally asked to iterate the contents of a Directory and validate the file names in that directory.

To iterate a directory of files, have a look at this:-

Directory Class (System.IO)

That said, has your original question changed?

Cheers,

Ian
 
Back
Top