14 Replies - 842 Views - Last Post: 13 April 2015 - 02:03 PM Rate Topic: -----

#1 NepSyn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-April 15

Read in from files with different file extensions

Posted 13 April 2015 - 11:54 AM

I have written a small program that goes to a folder on my desktop named "Text_Files" and takes any files inside with a file extension of ".txt". They are read in and all output together within one larger file named "CombinedTextFiles". What I want to do is now be able to read in files with any other file extension too e.g. ".xml" or ".html" at the same time. My question is, how do I add this into my code?

            string pathToLogs = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + ("/Text_Files");

            string[] txtFileList = Directory.GetFiles(pathToLogs, "*.txt");

            if (txtFileList != null)
            {
                    StringBuilder strFile = new StringBuilder();

                    foreach (string txt in txtFileList)
                    {
                        using (StreamReader sr = new StreamReader(txt))
                        {
                            strFile.AppendLine(txt.ToString());
                            strFile.AppendLine("***********************");
                            strFile.Append(sr.ReadToEnd());
                            strFile.AppendLine();
                            strFile.AppendLine();
                        }
                    }
                    using (StreamWriter outfile =
                        new StreamWriter(pathToLogs + @"\\CombinedTextFiles.txt"))
                    {
                        outfile.Write(strFile.ToString());
                    }
                }
             }



I tried adding in the following but it doesn't work.
string[] txtFileList = Directory.GetFiles(pathToLogs, "*.txt" || "*.xml" || "*.html");



Any suggestions? All help is greatly appreciated. Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Read in from files with different file extensions

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Read in from files with different file extensions

Posted 13 April 2015 - 11:59 AM

I'll take a guess that you could use "*.txt;*.xml;*.html" but I haven't searched in detail to confirm this.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Read in from files with different file extensions

Posted 13 April 2015 - 12:02 PM

You can't just say "any other file". You will have to write a method for each file type because the contents of every kind of file is different. After all, you don't want to just spew out raw XML or raw HTML do you? It should be parsed and make user-friendly, right?

There is .NET support for the MS Office file types you can start there.
https://msdn.microso...fice.15%29.aspx

Or for XML documents
https://support.micr...en-us/kb/307548

Or for HTML documents
http://www.codeproje...ML-using-Csharp

This post has been edited by tlhIn`toq: 13 April 2015 - 12:06 PM

Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Read in from files with different file extensions

Posted 13 April 2015 - 12:08 PM

As for automatically getting the files you need to loop.
Make a List<string> for all your extensions.
Loop through the list, getting all the files of that type and add it to your master file list.
Now process.
Was This Post Helpful? 1
  • +
  • -

#5 NepSyn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-April 15

Re: Read in from files with different file extensions

Posted 13 April 2015 - 12:10 PM

View Postandrewsw, on 13 April 2015 - 11:59 AM, said:

I'll take a guess that you could use "*.txt;*.xml;*.html" but I haven't searched in detail to confirm this.



Thank you but this doesn't work.It doesn't throw up any error, it just doesn't do anything. Thank you anyway.

View PosttlhIn`toq, on 13 April 2015 - 12:02 PM, said:

You can't just say "any other file". You will have to write a method for each file type because the contents of every kind of file is different. After all, you don't want to just spew out raw XML or raw HTML do you? It should be parsed and make user-friendly, right?

There is .NET support for the MS Office file types you can start there.
https://msdn.microso...fice.15%29.aspx

Or for XML documents
https://support.micr...en-us/kb/307548

Or for HTML documents
http://www.codeproje...ML-using-Csharp


OK, thanks.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Read in from files with different file extensions

Posted 13 April 2015 - 12:13 PM

I just wrote some code to list files of different extensions. Unfortunately, I forgot this was for C#, not VB, Doh!
        For Each f As String In Directory. _
            GetFiles("C:\users\andrew\documents\", "*.*").Where(Function(x)
                                                                    Dim y = System.IO.Path.GetExtension(x)
                                                                    Return (y = ".txt" OrElse y = ".xlsx")
                                                                End Function)

            Console.WriteLine(f)
        Next

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Read in from files with different file extensions

Posted 13 April 2015 - 12:17 PM

If you look up the function you're using (directory.GetFiles) you'd see what you can and can't do.
The description for search pattern clearly states that it only understands the wildcards * and ? but not regular expressions.
That tells you it is very limited and so anything beyond its built-in simplicity is something you'll have to write.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Read in from files with different file extensions

Posted 13 April 2015 - 12:23 PM

View Postandrewsw, on 13 April 2015 - 01:13 PM, said:

I just wrote some code to list files of different extensions. Unfortunately, I forgot this was for C#, not VB, Doh!
        For Each f As String In Directory. _
            GetFiles("C:\users\andrew\documents\", "*.*").Where(Function(x)
                                                                    Dim y = System.IO.Path.GetExtension(x)
                                                                    Return (y = ".txt" OrElse y = ".xlsx")
                                                                End Function)

            Console.WriteLine(f)
        Next


While that's a fun challenge its not very friendly or manageable. There should never be that use of hardcoded extensions like that.

You'd really want a method that takes the parent directory, the list of extensions and a bool for whether or not to look in the subdirectories.

List<string> GetFilePaths(string parentDirectory, List<string> extensionList, bool searchSubdirectories)
{
   List<string> returnList = new List<string>();
   // do your work here to get all the files
   return returnList;
}


The rest of the code doesn't know or care about the how and where. Just that is a list of paths were obtained. The list of extensions can be built in any number of ways... Checkboxes, configuration XML... etc.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Read in from files with different file extensions

Posted 13 April 2015 - 12:50 PM

It is fun, although it took more effort to convert to C# than I expected:
    string[] extns = { ".txt", ".xlsx" };

    var files = Directory.EnumerateFiles("c:\\users\\andrew\\documents\\", "*.*")
        .Where(x => extns.Contains(System.IO.Path.GetExtension(x)));

    foreach (string file in files) {
        Console.WriteLine(file);
    }

I am not presenting this as complete or professional code, just as an example of how this could be achieved.
Was This Post Helpful? 0
  • +
  • -

#10 NepSyn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-April 15

Re: Read in from files with different file extensions

Posted 13 April 2015 - 01:32 PM

Thanks AndrewSW. I have found this and it works!
string[] txtFileList = Directory.GetFiles(@"C:\").Where(fileExt => fileExt.EndsWith(".txt") || fileExt.EndsWith(".xml")).ToArray();

This post has been edited by andrewsw: 13 April 2015 - 01:37 PM
Reason for edit:: removed previous quote and added code tags

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Read in from files with different file extensions

Posted 13 April 2015 - 01:38 PM

Glad you sorted. Now your job is to extract it to a method as tlhIn`toq suggests, and not hard-code the values ;). (My approach makes this easier, because the extensions are already in an array.)

Note that you do not have to quote the previous post in full, there is a REPLY button towards the bottom of the page.

This post has been edited by andrewsw: 13 April 2015 - 01:39 PM

Was This Post Helpful? 1
  • +
  • -

#12 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Read in from files with different file extensions

Posted 13 April 2015 - 01:39 PM

I will repeat my concerns about how that is horrible design and a mess to manage.

There is no forward thinking in that implementation. What if you want to run it one time just on text files then another time just on HTML files? What if you want to offer the user a choice? There is no excuse for bad design on a project this simple where is it just to damned easy to do it right the first time. That kind of laziness becomes habit and style real fast.
Was This Post Helpful? 0
  • +
  • -

#13 NepSyn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-April 15

Re: Read in from files with different file extensions

Posted 13 April 2015 - 01:46 PM

View PosttlhIn`toq, on 13 April 2015 - 12:23 PM, said:

View Postandrewsw, on 13 April 2015 - 01:13 PM, said:

I just wrote some code to list files of different extensions. Unfortunately, I forgot this was for C#, not VB, Doh!
        For Each f As String In Directory. _
            GetFiles("C:\users\andrew\documents\", "*.*").Where(Function(x)
                                                                    Dim y = System.IO.Path.GetExtension(x)
                                                                    Return (y = ".txt" OrElse y = ".xlsx")
                                                                End Function)

            Console.WriteLine(f)
        Next


While that's a fun challenge its not very friendly or manageable. There should never be that use of hardcoded extensions like that.

You'd really want a method that takes the parent directory, the list of extensions and a bool for whether or not to look in the subdirectories.

List<string> GetFilePaths(string parentDirectory, List<string> extensionList, bool searchSubdirectories)
{
   List<string> returnList = new List<string>();
   // do your work here to get all the files
   return returnList;
}


The rest of the code doesn't know or care about the how and where. Just that is a list of paths were obtained. The list of extensions can be built in any number of ways... Checkboxes, configuration XML... etc.


Thank you for your reply. I don't fully understand it, I'm still learning. Thank you anyway.

So there is! Thank you all. :-)

tlhIn`toq your last post was a bit harsh. I'm learning. This is not for a real-world program. I'm just trying to see what I can do and asking questions as I go along. There's no need to be rude!
Was This Post Helpful? 0
  • +
  • -

#14 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Read in from files with different file extensions

Posted 13 April 2015 - 01:48 PM

View Postandrewsw, on 13 April 2015 - 01:50 PM, said:

I am not presenting this as complete or professional code, just as an example of how this could be achieved.

Here's the real problem with examples like that:

View PostNepSyn, on 13 April 2015 - 02:32 PM, said:

Thanks AndrewSW. I have found this and it works!
string[] txtFileList = Directory.GetFiles(@"C:\").Where(fileExt => fileExt.EndsWith(".txt") || fileExt.EndsWith(".xml")).ToArray();


Rookies always take it as code they can copy/paste into their solution. There is no effort to understand. There is no effort to expand on the simple example and make it 'production worthy'. If someone with a 'mentor' tag puts it in writing its 'good to go' in their eyes. We either have to be cognoscente of that and make an effort to NOT put out things there are the wrong way to go or code that can't just be copy/pasted. Either pseudo code which makes them do some work to implement a concept we have given them, or stub out the method signature and let them fill in the work.

Quote

List<string> GetFilePaths(string parentDirectory, List<string> extensionList, bool searchSubdirectories)
{
   List<string> returnList = new List<string>();
   // do your work here to get all the files
   return returnList;
}


Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Read in from files with different file extensions

Posted 13 April 2015 - 02:03 PM

View PostNepSyn, on 13 April 2015 - 02:46 PM, said:

tlhIn`toq your last post was a bit harsh. I'm learning. This is not for a real-world program. I'm just trying to see what I can do and asking questions as I go along. There's no need to be rude!


There was nothing rude in any of my posts. I pointed out how the simple example was not something you would want to do nor was it good design and even gave you a method stub of a better way to do it. Even Andrew who gave you the simple way said it was by no means production code.

I'll be real 'man-to-man' here with you. Not rude: Just adult and honest. If you think someone not making every post a case of blowing sunshine up your kilt and not all about stroking your ego, is them being harsh and rude you might be better off changing fields now before you get too financially invested in this path. Software development is not an employee-friendly career industry built on treating everyone like rainbows and unicorns fly out their arses. It is a high pressure, short deadline, "get it done right fraking now" kind of industry. Your bosses will want everything yesterday and perfect. You won't be thanked for what you do because that's your job and what they pay you for: Your paycheck is thanks enough for doing your job. But you will get reamed for screwing up, for taking too long, for voicing your thoughts if they don't match the bosses. You will enter this industry as a grunt doing all the crap work the experienced people with seniority don't want to do. There will be no place for creativity or opinion for at least 3 years. As a paid developer you will be expected to know what you are doing. It is not a place to get paid to learn on the job and have everyone teach you what you failed to learn in school. If that is not an industry you care to work in, or can't handle without constant praise, you should probably be realistic about it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1