Solved

Cutting the first three characters of each line

Posted on 2011-02-21
8
358 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
[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
  • 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 75

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 
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 75

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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

705 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