Click to See Complete Forum and Search --> : space delimited text file
DrewDog_21
Nov 26th, 1999, 10:45 PM
I am working on a subroutine to convert space-delimited files to semicolon-delimited files. Right now the file looks something like this:
Adam Smith 45 Cherry St. 789-0098
Susan Sarandon 123 123rd Avenue 555-5555
Walter Matthau 54 Hollywood Blvd. 123-4567
Right now I am using the code
Open CommonDialog1.Filename For Input as #File1
Line Input #File1, strText
Open CommonDialog2.Filename For Output as #File2
Print #File2, Texto
Obviously all this does is copy the file but unfortunately that is about the extent of my VB experience. I need a subroutine that will read the file, replace the white spaces between columns with a ";" and also return the column length (e.g. the number of spaces between the A of Adam Smith and the first number of his phone number).
any suggestions will be GREATLY appreciated!
Thanks,
Andrew
where i work, all we do is generate comma/tab delimited files or fixed width files. i would be very interested to find out why you use space delimited files, and why you would want to use semi-colon as a delimiter. Maybe i can give you some insight into my specialty subject.
please mail me if you have the time
------------------
Wossname,
Email me: wossnamex@talk21.com :)
MartinLiss
Nov 27th, 1999, 01:21 AM
Try this:
Dim sText As String
Dim nPos As Integer
sText = "Susan Sarandon 123 123rd Avenue 555-5555"
nPos = InStrRev(sText, " ")
sText = Left(sText, nPos - 1) & ";" & Right(sText, Len(sText) - nPos)
MsgBox sText
MsgBox "Name and address is " & nPos - 1 & " characters long"
This will only work if the telephone number has no spaces, so it can't be something like "(408) 555-1212". If it can, that you could first use instr to find the first occurrence of "(" and if found, use a modified version of the above.
------------------
Marty
Joacim Andersson
Nov 27th, 1999, 11:02 AM
If you have VB6 then you can use the Replace function:
strText = Replace(strText, " ", ";")
If you dont have VB6 then use the following function:
Public Function ReplaceText(strText$, strFind$, strReplaceWith$)
Dim strRetVal$, iPos%
strRetVal = strText
Do
iPos = InStr(strRetVal, strFind)
If iPos > 0 Then
strRetVal = Left$(strRetVal, iPos -1) & strReplaceWith & Mid$(strRetVal, iPos + Len(strFind))
End If
Loop While iPos > 0
ReplaceText = strRetVal
End Function
Good luck!
------------------
Joacim Andersson
joacim@programmer.net
joacim@yellowblazer.com
www.YellowBlazer.com (http://www.YellowBlazer.com)
DrewDog_21
Nov 27th, 1999, 11:20 AM
The problem when I use the Replace function is that it replaces all spaces with a ";". This will not work when the entry in one of the columns is less than the column length, e.g. if the column length is 12 and the text entered is length 7, the Replace function will add 5 semicolons instead of one.
How can I return the location of a character? I think that I could use this to get around the above problem.
DrewDog_21
Nov 27th, 1999, 11:20 AM
The problem when I use the Replace function is that it replaces all spaces with a ";". This will not work when the entry in one of the columns is less than the column length, e.g. if the column length is 12 and the text entered is length 7, the Replace function will add 5 semicolons instead of one.
How can I return the location of a character? I think that I could use this to get around the above problem.
DrewDog_21
Nov 27th, 1999, 11:22 AM
why the hell is everything that I post going on the boards twice?
XxEvilxX
Nov 27th, 1999, 11:34 AM
maybe you hit send twice????!!!!!!......:-)
Compwiz
Nov 27th, 1999, 11:55 AM
Again, if you are using VB6, then you can use the Split Function:
Dim FileNameOPEN, FileNameSAVE As String
Private Sub Command1_Click()
On Error GoTo CDCancelError
With CommonDialog1
.Filter = "Space Delimited Text Files (*.txt)|*.txt"
.Flags = cdlOFNHideReadOnly Or cdlOFNFileMustExist
.CancelError = True
.DialogTitle = "OPEN Space Delimited File"
.ShowOpen
FileNameOPEN = .FileName
.Filter = "Semi-Colon Delimited Text Files (*.txt)|*.txt"
.Flags = cdlOFNPathMustExist Or cdlOFNHideReadOnly
.CancelError = True
.DialogTitle = "SAVE Semi-Colon Delimited File"
.ShowSave
FileNameSAVE = .FileName
End With
Open FileNameOPEN For Input As #1
Open FileNameSAVE For Output As #2
Do While Not EOF(1)
Dim Temp As String
Line Input #1, Temp
Dim Lines() As String
Lines = Split(Temp, " ", -1, vbTextCompare)
Print #2, Lines(0) & ";" & Lines(1) & ";" & Lines(2) & ";" & Lines(3) & ";" & Lines(4) & ";" & Lines(5)
Loop
Close
CDCancelError:
End Sub
------------------
Tom Young, 14 Year Old
tyoung@stny.rr.com
ICQ: 15743470 (http://wwp.icq.com/15743470) Add Me (http://wwp.icq.com/scripts/search.dll?to=15743470) ICQ Me (http://wwp.icq.com/scripts/contact.dll?msgto=15743470)
AIM: TomY10 (http://www.aol.com/aim/aim30.html)
PERL, JavaScript and VB Programmer
[This message has been edited by Compwiz (edited 11-27-1999).]
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.