VB.net help -- finding files with certain naming criteria

Homerboy

Lifer
Mar 1, 2000
30,890
5,001
126
I have a directory with many multiple sub directories, and within those sub-directories many files (pdf and tifs)

I'm in need of a method to search the directories for any file names that contain 16 numerical characters in a row. trick is there are many different naming conventions for the files:

XXXX-1111111111111111-(Y).pdf
2012-1111111111111111_NAME.tif
1111111111111111.pdf
20120828-1111111111111111.pdf
etc etc etc

So I want to identify ANY files with the 16 digits in a row AND pull that portion of file name so I can use it in a SQL look up.

Does that make any sense?
 

KLin

Lifer
Feb 29, 2000
30,210
559
126
1. Iterate through file names of each directory.
2. Check to see if file name is longer than 16 characters
3. iterate through 16 characters of the filename until you get to the last character of the filename and checking to see if it's a number.
4. If 16 character string is a number, do your sql lookup.
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
System.IO.directoryInfo and System.IO.fileInfo are the classes you'd be looking at for directory and file exploration. Making a procedure that takes a single directory, compares all files, then recursively calls itself on subdirectories should be simple enough with those classes.


A regex [0-9]{16} compared to the file name should match 16 digit in a row numbers. Regexes should also make it easy to pull it out of the filename from the fileinfo class. Do you also want to match 17, 18, ect row digits?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
System.IO.directoryInfo and System.IO.fileInfo are the classes you'd be looking at for directory and file exploration. Making a procedure that takes a single directory, compares all files, then recursively calls itself on subdirectories should be simple enough with those classes.


A regex [0-9]{16} compared to the file name should match 16 digit in a row numbers. Regexes should also make it easy to pull it out of the filename from the fileinfo class. Do you also want to match 17, 18, ect row digits?

Make sure that you check the directory names to ignore the current and parent directory.

Otherwise you get into a never ending recursion loop
 

ThinkOfTheCode

Junior Member
Aug 6, 2012
5
0
66
You didn't mention which version of .NET you are using. This post has a nice solution in the comments if your framework supports LINQ. This post answers the second part of your question.

I've modified the comment a bit to fit your need and I stole some from the previous posts. (I'm a C# guy so you'll need to change it over to VB.)

Code:
var regexTest = new Func<string, bool>(i => Regex.IsMatch(i, @"\d{16}", RegexOptions.Compiled));
var files = Directory.GetFiles(@"c:\path\to\folder", "*", System.IO.AllDirectories).Where(regexTest);
foreach (string file in files)
{
    //matches will have the digits you're looking for
    MatchCollection matches = Regex.Matches(file, @"\d{16}");
}
Here's something you'll find useful. http://converter.telerik.com/
 
Last edited: