Solved

Change Text File

Posted on 2004-08-19
7
136 Views
Last Modified: 2010-05-02
I have a Text file that is space delimited and want to add a digit (1) to a list (column) of fields. the field is changing from a five digit field to a six digit field.  the database only allows 5 digits on the original field. I need to save it to a different file as a text delimited file.

0
Comment
Question by:lopezt1
[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
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:Enlade
ID: 11849141

I'm not sure I can help you with this problem, but I just have to ask.  What is a "text delimited file"?  Hee hee.

I think you need to restate your question differently.  Maybe give people an example of the input file (a line or two) and then give them an example of the output file (using the line or two from your input file).  I think if you do that you will get an answer quickly as your solution is probably just a loop, a line input, a split statement, and a print statement.
0
 
LVL 12

Accepted Solution

by:
Bob Lamberson earned 250 total points
ID: 11849193
Hi lopezt1,

Add the following code to a form code, and add a command button to the form.
Insert your path and file names where I have indicated (2 places) then run the form, click the button and open your output file to see the results.

Option Explicit
Dim fileName As String
Dim fso, ts
Dim inText As String
Dim newText As String
Dim txtArray() As String
Dim txtArrayLine() As String
Dim txtLine As String

Private Function getLine()
fileName = "C:\AAA testing\TextDocument\inputText.txt"      ' insert  your input path and filename here
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(fileName, ForReading, True)
If ts.AtEndOfStream <> True Then
   inText = ts.ReadAll
End If
ts.Close
addCharacter (inText)    'passing the file contents to addCharacter function.
Set fso = Nothing
Set ts = Nothing
End Function

Private Function addCharacter(newText As String)
Dim i As Integer
Dim outFileName As String
outFileName = "C:\AAA testing\TextDocument\outputText.txt"      ' insert  your output path and filename here
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(outFileName, ForWriting, True)
txtArray() = Split(newText, vbCrLf)       'put each Line in an Array element
For i = 0 To UBound(txtArray)
   txtArrayLine() = Split(txtArray(i), Chr(32))            'split out the columns
   txtArrayLine(3) = txtArrayLine(3) & "1"             'add the 1 here
   txtLine = Join(txtArrayLine, Chr(32))           'put the line back together
   ts.WriteLine (txtLine)               'write the line to the new file
Next i
ts.Close
Set fso = Nothing
Set ts = Nothing
End Function

Private Sub Command1_Click()
getLine
End Sub


Bob
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11851107

I don't know how you wrote any code from that specification, but since you have a loop, a Split, and a WriteLine, it probably will work.  Hee hee.

0
 
LVL 12

Expert Comment

by:Bob Lamberson
ID: 12378127
lopezt1,

I think the code I posted answered your question. If not post the problem you are having with it and someone can probably help.
Bob
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 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