Solved

Insert text at the of a text file line

Posted on 2006-10-27
11
290 Views
Last Modified: 2013-12-25
Hi experts,

I have a text file with thousands of lines. Some lines are shorter than others. To accomplish what i want i need all lines to be the same lenght which is (601 characters). The code below reads each line and subtracts the difference from the goal of 601. The next step is to fill out with spaces (hence my character variable) the missing characters.
The program adds spaces to the text file but it adds at the bottom after the last line instead of adding it to the right side or end of each line.

Does anyone have any sugestions?

Thanks
Andre

Dim filename As String
Dim rowlenght As Long
Dim stringlenght As Integer
Dim i As Integer
Dim character As String

character = Chr$(13)

rowlenght = 10

filename = txtFilename.Text

Dim Line As String
Open filename For Append As #1
Open filename For Input As #1
Do While Not EOF(1)
  Line Input #1, Line
  stringlenght = rowlenght - Val(Len(Line))
  Close (1)
  Open filename For Append As #1
  For i = 0 To stringlenght
  'Line  #1, character
  Write #1, character
  Next
  'MsgBox ("Lenght :" & stringlenght & " Text: " & Line)
Loop
0
Comment
Question by:virgilar
[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
11 Comments
 
LVL 3

Expert Comment

by:J_K_M_A_N
ID: 17822461
I would create a loop and use line input instead. Measure the length of the line and add the needed number of spaces to it. Then write that line out to a different file. It would be easier I think. That is going to be one monster file if you have thousands of lines and 601 chars per line. Good luck.

J_K_M_A_N
0
 
LVL 3

Assisted Solution

by:taycuong76
taycuong76 earned 250 total points
ID: 17825131
This program assume that you want to change the length of all line in a file to 10
You create a file on c:\ and named as "test.txt" with the content not equal, ex:
123456789
1234567890
1234
12345
1234122
1

Create a new VB project and add the code:
Private Sub Form_Load()
Dim filename As String
Dim i As Integer
Const rowlenght = 10

filename = "c:\test.txt"
filename2 = "c:\temp$$$.Txt"

Dim Line As String
Open filename For Input As #1
Open filename2 For Output As #2
Do While Not EOF(1)
Line Input #1, Linex
    If Len(Trim(Linex)) < 10 Then
    trang = 10 - Len(Linex)
    Linex = Linex & Space(trang)
'   MsgBox Len(Linex) & "-" & (10 - Len(Linex)) & "-" & Linex
    End If
Print #2, Linex
Loop
Close (1)
Close (2)
Kill filename
Name filename2 As filename
Msgbox "Done !"
End Sub

This will create a temp file, ajust the line thength (add space (" ") to the line which have length less than 10) then kill old file and named temp file back to original file.

Note: Make a copy of your file before test.
Hope this help, tested on VB 6 - XP sp2
0
 
LVL 3

Expert Comment

by:taycuong76
ID: 17825135
Change the Const rowlenght = 10 to Const rowlenght = 610 to do with your data file (Make a copy of your file first.)

0
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!

 
LVL 3

Expert Comment

by:taycuong76
ID: 17825153
There is a situation: if your one of your lines longer than 10?????

In this case, you have to add some code like that:
  ....
  If Len(Trim(Linex)) > 10 Then
   'Only select 10 char from original line
    Linex = left(Linex,10)
  End If
 ......

But in fact, this is funny, because your data will be truncated to 10 and you'll lost the rest of your data !
0
 
LVL 19

Accepted Solution

by:
BrianGEFF719 earned 250 total points
ID: 17827289
This should do it for you:


Dim strLine As String
Dim strBuffer As String
Dim strInputFile As String
Dim strTempFile As String

strInputFile = "c:\test.txt"
strTempFile = "c:\test.tmp"


Open strInputFile For Input As #1
    Open strTempFile For Output As #2
     While Not EOF(1)
      Line Input #1, strLine
      strBuffer = Space(601 - Len(strLine))
      Print #2, strLine & strBuffer
     Wend
    Close #2
Close #1

Kill strInputFile
FileCopy strTempFile, strInputFile
Kill strTempFile
0
 
LVL 3

Expert Comment

by:taycuong76
ID: 17828094
BrianGEFF719, just the same mine. Nothing special.
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 17829366
I like to think mine is a little more organized.
0
 
LVL 3

Expert Comment

by:taycuong76
ID: 17830990
You 'd better find a new way and solution to help peoples.
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17832134
or try this

Private Sub Command1_Click()
Dim oFso, oText
Dim sFile, sContent As String
    Set oFso = CreateObject("Scripting.FileSystemObject")
    sFile = "C:\Your_file.txt"
    If Not oFso.FileExists(sFile) Then
        MsgBox "File does not exist"
        Exit Sub
    End If
    Set oText = oFso.OpenTextFile(sFile)
    sContent = oText.ReadAll
    oText.Close
    'Filling spaces
    sContent = FillSpaces(sContent, 601)
    'Writing new data
    sFile = "C:\Your_New_file.txt"
    Set oText = oFso.OpenTextFile(sFile, ForWriting, True)
    oText.Write sContent
    oText.Close
    MsgBox "Done"
End Sub

Private Function FillSpaces(ByVal Content As String, ByVal Length As Integer) As String
Dim oReg
Dim oMat
Dim oMCol
    Set oReg = CreateObject("VBScript.RegExp")
    With oReg
        .IgnoreCase = True
        .Global = True
        .Pattern = "(.{1," & Length & "})\r\n"
    End With
    Content = Content & vbCrLf 'Adding new line to get last line
    Set oMCol = oReg.Execute(Content)
    For Each oMat In oMCol
        Content = Replace(Content, oMat.Value, oReg.Replace(oMat.Value, "$1" & Space(Length - Len(oMat.Value) + 2) & vbCrLf))
    Next
    FillSpaces = Left(Content, Len(Content) - 2) 'Removing last new line
End Function
0
 
LVL 3

Expert Comment

by:taycuong76
ID: 17832383
Good training job using FileSystemObject.
0
 

Author Comment

by:virgilar
ID: 17833522
taycuong76 and BrianGEFF719 and everybody else,

Thaks a lot for your inputs, i was able to accomplish exactly what i wanted.

I will split the point between taycuong76 and BrianGEFF719  since their answers helped me directly with my needs.

Thanks guys,

Andre
0

Featured Post

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

Question has a verified solution.

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

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

688 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