Question : find filenames in a text file

Hi

In my VB net 2008 app I get a filename from the end user. That file is a text file. The text file contains a lot of information, but it also might contain one or more names of files. these files are all *.bin files.

Now I want to search through the file and build a list of filenames it contains.

I'm thinking that I somehow need to use regular expressions and do something recursive.


The file could look kind of like this:

blah blah blah file1.bin blah blah
blah blah
file2.bin
blah blah anotherfile.bin blah
blah blah

So I want my list to look like:
file1.bin
file2.bin
anotherfile.bin


What is the best and most elegant way for me to do it?
(I can use a C# example. It does not HAVE to be VB)

Thanks in advance
liversen

Answer : find filenames in a text file

Hi Liversen,
I think I know what you want to do, basically it looks like you want to recursive search through files the same way a web crawler searches through web sites.  I've included the code for you to do this (in C#) along with this regular expression:

(?i)\b\S*?\.bin\b

the (?i) is the case insensitive modifier in case you encounter a BIN, Bin or biN.  
\S is the negation for whitespace (i.e. everything, but whitespaces)
*? - is the non-greedy matching, it will match until the first instance of .bin
Here's a good tutorial website that I use for regular expressions:
http://www.codeproject.com/KB/dotnet/regextutorial.aspx

The list of files will be stored in a generic List of string (or List), which is cleared in the CallingFunction and dumped out after ProcessFile is finished its recursive run.  I think you can figure the rest out from this.  :)

Enjoy, hope this helps.
P.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
List lFileNames = new List();

        public void ProcessFile(string strFilePath)
        {
            string strFileContent = "";
            using (StreamReader srInput = new StreamReader(strFilePath))
            {
                strFileContent = srInput.ReadToEnd();
            }
            Regex myRegEx = new Regex(@"(?i)\b\S*?\.bin\b");
            foreach (Match myMatch in myRegEx.Matches(strFileContent))
            {
                if (!lFileNames.Contains(myMatch.Value) && System.IO.File.Exists(myMatch.Value))
                {
                    lFileNames.Add(myMatch.Value);
                    ProcessFile(myMatch.Value);
                }
            }
        }

        public void CallingFunction()
        {
            lFileNames.Clear();
            ProcessFile(@"C:\Temp\myBinFile.bin");
            foreach (string strFileNameEncountered in lFileNames)
            {
                Console.WriteLine(strFileNameEncountered);
            }
        }
Random Solutions  
 
programming4us programming4us