user2948119
user2948119

Reputation: 9

Different integers? c#

I'm trying to read and extract specific information from several data files in which the filename format remains the same for every one, the format of my file is, XXXXXX_XXXXXX_PCPDTB_ODT.datafile where X is a random digit.

It represents year, month, day in the first 6 digits and hours, minutes and seconds in the last 6 X's, so 131005_091429_PCPDTB_ODT.datafile would be 2013, 10th month, 5th day and so on, the _PCPDTB_ODT.datafile is always there.

I'm able to gather my desired data (extracting all information after a certain keyword, in this case '#Footer' is my keyword) from a file successfully, but I'm not sure how I'd go about this with lots of files with several changing integers?

Here is my attempt (although it is terrible since I have very little experience of coding), but only seem to be to input 4 digits and no more. Which would only allow to access files like XXXX_PCPDTB_ODT.datafile or 1304_PCPDTB_ODT.datafile.

static void Main(string[] args)
    {
        var path = @"C:\Users\#####\Desktop\";
        var ext = "_PCPDTB_ODT.datafile";
        var range = Enumerable.Range(0,9);
        var filePaths = 
                from i1 in range
                from i2 in range
                from i3 in range
                from i4 in range
                let file = path + i1 + i2 + i3 + i4 + ext
                where File.Exists(file)
                select File.ReadLines(file)
                    .SkipWhile(line => !line.Contains("#Footer"))
                    .Skip(1);
        try
        {
            Console.WriteLine(String.Join(Environment.NewLine,filePaths.SelectMany(f => f)));
        }
        catch (Exception e)
        {
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
            Console.Read();
        }
    }

I've attempted adding more i values, i5, i6, i7 etc. with "_" to space the first six digits from the last 6 but it doesn't seem to do anything when using the larger i values past i4.

Any ideas would help greatly, please keep in mind my coding is most likely rubbish since my knowledge is very little at the moment, thanks.

Upvotes: 0

Views: 116

Answers (4)

unrealsoul007
unrealsoul007

Reputation: 4077

Try this

using System;

public class Test
{
public static void Main()
{
    string str = "131005_091429_PCPDTB_ODT.datafile ";
    int[] date = new int[3];
    int[] time = new int[3];

    string[] arr = str.Split('_');
    for(int i = 0;i<6;i=i+2)
    {
        date[i/2]=Convert.ToInt32(arr[0].Substring(i,2));
    }

    for(int i = 0;i<6;i=i+2)
    {
        time[i/2]=Convert.ToInt32(arr[1].Substring(i,2));
    }
}
}

Upvotes: 0

Jim Mischel
Jim Mischel

Reputation: 133995

If you're trying to process all of the files in a directory, use Directory.EnumerateFiles.

Forexample:

foreach (var filename in Directory.EnumerateFiles(@"c:\mydata\", "*PCPDTB_ODT.data")
{
    var lines = File.ReadLines(filename)
               .SkipWhile(line => !line.Contains("#Footer"))
               .Skip(1);
    // add the rest of your code . . .
}

Upvotes: 3

D Stanley
D Stanley

Reputation: 152556

it doesn't seem to do anything when using the larger i values past i4

Proavbly because it's having to iterate through 1,000,000,000,000 different filenames?

Why not just get a list of files that match the pattern?

    var path = @"C:\Users\#####\Desktop\";
    var pattern = "??????_??????_PCPDTB_ODT.datafile";

     var filePaths = Directory.GetFiles(path, pattern)
                              .Select(file => File.ReadLines(file)
                              .SkipWhile(line => !line.Contains("#Footer"))
                              .Skip(1));

Upvotes: 1

Tim S.
Tim S.

Reputation: 56536

Instead of trying to loop through every possible valid file name, you should just see what files are there. Here's an example using Directory.GetFiles

var filePaths = Directory.GetFiles(path, "*" + ext)
                  .Select(file => File.ReadLines(file)
                    .SkipWhile(line => !line.Contains("#Footer"))
                    .Skip(1));

If you need the date/time also, you can parse that out, too, with DateTime.ParseExact.

Upvotes: 3

Related Questions