Solved

Cutting the first three characters of each line

Posted on 2011-02-21
8
355 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

739 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