• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

read first line of each text file

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
Sat80
Asked:
Sat80
  • 5
  • 4
  • 2
2 Solutions
 
DhaestCommented:
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
 
Sat80Author Commented:
Thanks, I am sry that I didn't say I am using vb.net 2010.
I guess the code in different lang? C#?
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
Sat80Author Commented:
@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
 
DhaestCommented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
What Dhaest said :)
0
 
Sat80Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
What error?
0
 
Sat80Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
Sat80Author Commented:
Yes you are right, thanks you sir :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now