aclogics 0 Newbie Poster

I have this routine in VB that takes one pipe delimited text file and splits it into multiple text files according to one key field. The records in the text file look something like this:
WTX_2.0|O|F|1234|andre.couturier@rrs4.ca |5678|NBSID09876|O_20071121.TXT||1511:AE00001R||11152007||SAMPLE COMMENTS WILL GO HERE ||599|12 |401
WTX_2.0|O|F|1234|andre.couturier@rrs4.ca |5678|NBSID09876|O_20071121.TXT||1511:AE00002R||11152007||SAMPLE COMMENTS WILL GO HERE ||599|12 |401

I need to remove all the trailing spaces from each field. So if a pipe is preceded by a space it needs to go. The code I have to split the file is as follows:

[ Dim inti As Integer 'compteur de lignes
Dim strSet() As String 'variable pour lignes dynamic
Dim intCnt As Integer
Dim intCnt2 As Integer
Dim intFile As Integer
Dim intSemi(6) As Integer

Private Sub Command1_Click()
Open "F:\watertrax\original.txt" For Input As #1 'open source file
inti = 0
Do While EOF(1) = False
inti = inti + 1 'compte les lignes
ReDim Preserve strSet(inti) As String
Line Input #1, strSet(inti) 'ecris la ligne dans variable
Loop
Close #1 'close source file
'les lignes sont toutes dans des variable memoire

’ I am thinking we could loop through the array of lines and delete the spaces one by one but only if encountered right before a pipe
‘For intCnt = 1 To inti
‘Dim intj As Integer
‘intj = 0 'compteur de sections
‘If strSet(intCnt) > "" Then 'voir si la ligne existe
‘For intCnt2 = 1 To Len(strSet(intCnt))
‘If Mid(strSet(intCnt), intCnt2, 1) = " " Then
‘here I would like to delete the space but only if it is a trailing space and not one separating '2 words
‘it would be useful if we could maybe start from the end of the string going to the beginning, keep each pipe in memory and if a space is
‘found after a pipe, not copy the space
‘End If
‘End If
‘Next intCnt2
‘Else: GoTo EndLoop
‘End If
EndLoop:
‘Next intCnt

Dim intFile As Integer
intFile = 1
For intCnt = 1 To inti
Dim intj As Integer
intj = 0 'compteur de sections
If strSet(intCnt) > "" Then 'voir si la ligne existe
For intCnt2 = 1 To Len(strSet(intCnt))
If Mid(strSet(intCnt), intCnt2, 1) = "|" Then
intj = intj + 1
intSemi(intj) = intCnt2 'trouver la location des pipes dans la string
If intj = 6 Then 'les 6 premieres pipe sont trouvees
Dim decVar As String
decVar = Mid(strSet(intCnt), intSemi(5) + 1, intSemi(6) - intSemi(5) - 1) 'prends la valeur entre le 5ieme et 6ieme pipe
GoTo FoundDiv
End If
End If
Next intCnt2
Else: GoTo EndLoop
End If
FoundDiv:
iFile = FreeFile
Open "F:\watertrax\" & intFile & ".txt" For Output As #iFile
For intCnt2 = 1 To inti
If Mid(strSet(intCnt2), intSemi(5) + 1, Len(decVar)) = decVar Then 'si la valeur du champs cle est le meme
Print #iFile, strSet(intCnt2) 'ecris dans la nouvelle file
strSet(intCnt2) = "" 'delete la ligne pour liberer et ne plus la lire
End If
Next intCnt2 'prochaine ligne
intFile = intFile + 1
Close #iFile
EndLoop:
Next intCnt
Kill "F:\watertrax\original.txt" 'replace with location of file
MsgBox Trim$("Succesful operatrion!")
End Sub]

Any suggestions would be much appreciated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.