Solved

read first line of each text file

Posted on 2011-02-23
11
293 Views
Last Modified: 2012-05-11
Hi experts,

I have different txt files (unknown file numbers), I would like to load them and read only the first line of each text file and then save them to a string.

The first line of each file is only a number such as:
test1.txt
1
2
3

Open in new window


test2.txt
3
2
1

Open in new window


test3.txt
4
5
6

Open in new window


So, I wanna load them and put all the first lines to one string like this:
mystring= "1,3,4"
note:
1 is the first line of test1.txt
3 is the first line of test2.txt
4 is the first line of test3.txt

Thanks in advance
Regards
0
Comment
Question by:Sat80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 150 total points
ID: 34959587
Try the following
string[] filePaths = System.IO.Directory.GetFiles(@"C:\test\", "*.txt");
            string output = "";
            foreach (string sFile in filePaths)
            {
                System.IO.StreamReader file = new System.IO.StreamReader(sFile);
                string strLine = file.ReadLine();
                output += strLine + " is the first line of " + sFile + Environment.NewLine;
                file.Close();
            }

Open in new window

0
 

Author Comment

by:Sat80
ID: 34959606
Thanks, I am sry that I didn't say I am using vb.net 2010.
I guess the code in different lang? C#?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 350 total points
ID: 34959607
Try this:
        Dim list As New List(Of String)

        For Each filename As String In Directory.GetFiles("C:\test")
            Using sr As StreamReader = New StreamReader(filename)
                list.Add(sr.ReadLine())
            End Using
        Next

        Dim result As String = String.Join(",", list.ToArray())

Open in new window

0
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 

Author Comment

by:Sat80
ID: 34959645
@carl_tawn, thanks.
For Each filename As String In Directory.GetFiles("C:\test")
Can I specify the ext?
For Each filename As String In Directory.GetFiles("C:\test\test*.txt") --> I got an error "Illegal characters in path"
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 34959652
I'm sorry. I posted indeed in c#

For your last question: try:

For Each filename As String In Directory.GetFiles("C:\test\test, "*.txt")
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34959668
What Dhaest said :)
0
 

Author Comment

by:Sat80
ID: 34959696
Thank you ALL :)
It's not working because the code miss "
For Each filename As String In Directory.GetFiles("C:\test\test, "*.txt")

And when I added " to the code
For Each filename As String In Directory.GetFiles("C:\test\test", "*.txt")

It then return the same error, strange!
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34959705
What error?
0
 

Author Comment

by:Sat80
ID: 34959727
This one:
"Illegal characters in path"

I guess the line is wrong, coz I wanna load files not folder?

For Each filename As String In Directory.GetFiles("C:\test")

I want to load files starting like this:
c:\test\test*.txt

Because there are unknown files number on the test folder such as:
c:\test\test1.txt
c:\test\test2.txt
c:\test\test3.txt
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34959747
If you want to restrict on name as well as extension then that needs to be part of your pattern:
For Each filename As String In Directory.GetFiles("C:\test", "test*.txt")

Open in new window

0
 

Author Comment

by:Sat80
ID: 34959761
Yes you are right, thanks you sir :)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question