Solved

Cutting the first three characters of each line

Posted on 2011-02-21
8
337 Views
Last Modified: 2012-06-21
Hi Experts,

I have a text file that includes different lines and an each line there is some elements divided by a comma (,):
ABC1235,ASD45,A2R45546=4
V5R48654=,TR4548
R58sgd%$,YT841d4,ABC127

What I want is that, I would to load that text file and then cut each element after the third character. So, the final result should be something like this:
ABC,ASD,A2R
V5R,TR4
R58,YT8,ABC

As you can see the above result is cutting each element (the elements divided by comma ,) and then rewrite the output data to the same file.
Thanks in advance
Regards
0
Comment
Question by:Sat80
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 34943651
Several steps to perform:
1. Read the file line by line
2. use the string.substring-method on that line http://msdn.microsoft.com/en-us/library/aka44szs.aspx
3. save everything back into the file


Dim sFileName As String
Dim srFileReader As System.IO.StreamReader
Dim sInputLine As String

dim sOutput as String

sFileName = "test.txt"
srFileReader = System.IO.File.OpenText(sFileName)
sInputLine = srFileReader.ReadLine()
Do Until sInputLine is Nothing
    sOutput += sInputLine.Substring(3, sInputLine.Length() - 3)
    sInputLine = srFileReader.ReadLine()
Loop

Dim objWriter As New System.IO.StreamWriter(sFileName)
objWriter.Write(sOutput)
'Close objectwriter:
objWriter.Close()

Open in new window

0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 200 total points
ID: 34943672
Try this:
Public Sub RewriteFile(ByVal filename As String)
	Dim temp As String = System.IO.Path.GetTempFileName()

	Using reader As New System.IO.StreamReader(filename)
		Using writer As New System.IO.StreamWriter(temp)
			While Not reader.EndOfStream
				Dim line As String = reader.ReadLine()
				Dim parts() As String = line.Split(","c)

				For i As Integer = 0 To parts.Length - 1
					parts(i) = parts(i).Substring(0, 3)
				Next

				writer.WriteLine(String.Join(",", parts))
			End While
		End Using
	End Using

	System.IO.File.Move(temp, filename)
End Sub

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 34943677
Sorry misread, I understood the string-requirement.
You need to adjust this:
Do Until sInputLine is Nothing
    sOutput += sInputLine.Substring(3, sInputLine.Length() - 3)
    sInputLine = srFileReader.ReadLine()
Loop



into

dim sInput as string()
Do Until sInputLine is Nothing
    sInput = sinputLine.split(',')
    For i = 0 To UBound(aryTextFile)
         sInput(i) = sInput(i).Substring(3, sInput(i)Length() - 3)
     Next i

    sOutput += String.Join(",", sInput)
    sInputLine = srFileReader.ReadLine()
Loop

0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 200 total points
ID: 34943683
Still made a fault
sInput(i) = sInput(i).Substring(3, sInput(i)Length() - 3)
must be
sInput(i) = sInput(i).Substring(0, 3)

Dim sFileName As String
Dim srFileReader As System.IO.StreamReader
Dim sInputLine As String

dim sOutput as String

sFileName = "test.txt"
srFileReader = System.IO.File.OpenText(sFileName)
sInputLine = srFileReader.ReadLine()
dim sInput as string()
Do Until sInputLine is Nothing
    sInput = sinputLine.split(',')
    For i = 0 To UBound(aryTextFile)
         sInput(i) = sInput(i).Substring(0, 3)
     Next i

    sOutput += String.Join(",", sInput)
    sInputLine = srFileReader.ReadLine()
Loop


Dim objWriter As New System.IO.StreamWriter(sFileName)
objWriter.Write(sOutput)
'Close objectwriter:
objWriter.Close()

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 9

Assisted Solution

by:Hawkvalley1
Hawkvalley1 earned 100 total points
ID: 34943731
Dim newList As New List(Of String)
Using sr As New StreamReader(<path>)
 While Not sr.EndOfStream
  Dim str() As String = sr.Readline.Split(","c)
  Dim line As String
  For Each s As String In str
      line &= s.Remove(3) & ","
  Next
  line = line.TrimEnd(","c)
 newLine.Add(line)
 End While
End Using
 
0
 

Author Closing Comment

by:Sat80
ID: 34943794
Thank you all
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34944043
NP. Glad to help  : )
0
 
LVL 9

Expert Comment

by:Hawkvalley1
ID: 34948489
That was fun, how many ways???
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now