-1

I apologize if the question title is a little confusing, but let's say a text file contains the following data:

Sample         'Line1
 1             'Line2
 2             'ProjectCount
FileA.ext      'Projects( 1)
 0             'SortTermCount
FileB.ext      'Projects( 2)
 0             'SortTermCount
 ...

I know the extension name that exists as a string within the file (i.e. .doc), what I would like to do is get the full name and the extension of the file, the code would return "FileA.ext" and "FileB.ext" (as a string, or array (preferably), or whatever).

I tried to modify code from this SO question: https://stackoverflow.com/a/3152180 to work with text files, but I guess that is only for directories.

EDIT: The value before 'ProjectCount' tell me how many extension files I can expect, so perhaps I don't need to search the entire file?

Community
  • 1
  • 1
Mohammad Ali
  • 353
  • 1
  • 6
  • 18

3 Answers3

1

You could do it like this:

var fileName = @"C:\source.txt";
var files = File
    .ReadAllLines(fileName)
    .Where(line => !line.StartsWith(" "))
    .Skip(1) //Ignore the first line
    .Select(line => line.Substring(0, line.IndexOf(" ")));

Now files contains an IEnumerable<string> containing all of your file names.

So you can loop through the filenames like this:

foreach(string fileName in files)
{
    //fileName is a string variable

}
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • Should I change first var to string, and second to an array of strings? – Mohammad Ali Jun 12 '15 at 16:29
  • You could change first `var` to `string` and the second to `IEnumerable` but there's no need. – DavidG Jun 12 '15 at 16:31
  • How will I get an output/access to the file names? – Mohammad Ali Jun 12 '15 at 16:34
  • I've added an example, it's just a simple loop, or you could convert it to a string array with `files.ToArray()` – DavidG Jun 12 '15 at 16:37
  • With your code, I get the file names, but it also prints some extra stuff: FileA.ext FileB.ext FileC.ext FileD.ext default default default default none False False False False – Mohammad Ali Jun 12 '15 at 16:46
  • Perhaps because you are printing the first index of lines without spaces, instead of the extension files? – Mohammad Ali Jun 12 '15 at 16:51
  • If you can modify your code to add the extension check, while still keeping the code clean (like now), your answer will be selected as it is currently the cleanest. – Mohammad Ali Jun 12 '15 at 17:34
1

If you know the extensions that can show up you can use the following solution with a regex:

//regex for file name with known extensions    
Regex r = new Regex("^.*\\.(ext|doc|PUT_OTHER_EXTENSIONS_HERE)$");
var lines = File.ReadAllLines(FILE_PATH);
var res = lines.Where(x => !string.IsNullOrWhiteSpace(x) && r.Match(GetFirstEntry(x)).Success)
                    .Select(x => GetFirstEntry(x));

where GetFirstEntry :

            /// <summary>
            /// Splits the string by the "'" char and gets the first entry.
            /// </summary>
            /// <param name="x"></param>
            /// <returns></returns>
            private static string GetFirstEntry(string x)
            {
             try{
                return x.Split(new string[] { "'" }, StringSplitOptions.RemoveEmptyEntries)[0].Trim();
                }catch{return string.Empty;}
            }
Alioza
  • 1,690
  • 12
  • 14
0

You can do:

  • Use File.ReadLines to read each file line by line, instead of loading everything in the memory
  • Split each line on white space character
  • Consider first column of split as possible file name
  • Use Path.GetExtension method to get file's extension
  • Compare it against your extensionToCheck with ignoring case
  • Select those matching

Like:

string extensionToCheck = ".ext";
string filePath = @"test.txt";
var fileNames = File.ReadLines(filePath)
    .Select(line => new {PossibleFile = line.Split().First()})
    .Where(p =>
        Path.GetExtension(p.PossibleFile)
            .Equals(extensionToCheck, StringComparison.InvariantCultureIgnoreCase))
    .Select(f => f.PossibleFile);

For output use:

foreach (var fileName in fileNames)
{
    Console.WriteLine(fileNames);
}

In case of above sample file you will get two records

FileA.ext
FileB.ext
Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • Could you add some comments to the code? Edit: I think the ones at the top are sufficient :) – Mohammad Ali Jun 12 '15 at 16:56
  • @MohammadAli, each step is explained in the bullets before, If you have any particular question about a particular part of code then ask – Habib Jun 12 '15 at 16:57