|
-
Nov 26th, 1999, 11:45 PM
#1
Thread Starter
Hyperactive Member
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
-
Nov 27th, 1999, 02:12 AM
#2
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: [email protected] 
-
Nov 27th, 1999, 02:21 AM
#3
Try this:
Code:
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
-
Nov 27th, 1999, 12:02 PM
#4
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
[email protected]
[email protected]
www.YellowBlazer.com
-
Nov 27th, 1999, 12:20 PM
#5
Thread Starter
Hyperactive Member
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.
-
Nov 27th, 1999, 12:20 PM
#6
Thread Starter
Hyperactive Member
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.
-
Nov 27th, 1999, 12:22 PM
#7
Thread Starter
Hyperactive Member
why the hell is everything that I post going on the boards twice?
-
Nov 27th, 1999, 12:34 PM
#8
Addicted Member
maybe you hit send twice????!!!!!!......:-)
-
Nov 27th, 1999, 12:55 PM
#9
Hyperactive Member
Again, if you are using VB6, then you can use the Split Function:
Code:
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
[email protected]
ICQ: 15743470 Add Me ICQ Me
AIM: TomY10
PERL, JavaScript and VB Programmer
[This message has been edited by Compwiz (edited 11-27-1999).]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|