Problem with Loop

chandpuri

Registered User.
Local time
Today, 11:05
Joined
Sep 11, 2007
Messages
13
Hi,
I am opening a text file and finding a string say 001# till EOF and then replacing it. Once the replacement is done I want to find another string say 003# in the complete file and replace it with another string. This way I need to find around 15 strings and replace them. And in the end close the file.

But i do not know what the problem is with the code.

My code is like this

Private Sub Main()
Dim intext As String
Dim outtext As String

Open "C:\test.txt" For Input As #1
Open "C:\certi.txt" For Output As #2

Do While Not EOF(1)
Line Input #1, intext

If InStr(1, intext, "001#") > 0 Then
outtext = Replace(intext, "001#", "|")
Print #2, outtext
End If

If InStr(1, intext, "002#") > 0 Then
outtext = Replace(intext, "002#", "")
Print #2, outtext
End If



Loop

Close


End Sub
 
Only when the strings 001# etc are found the line was written to the output file. Is that what you wanted? If yes, your code works. If not, remove the "if then endifs".
Code:
Private Sub Main()
    Dim intext As String
    Dim outtext As String
    
    Open "C:\temp\test.txt" For Input As #1
    Open "C:\temp\certi.txt" For Output As #2
    
    Do While Not EOF(1)
        Line Input #1, intext
        
        outtext = Replace(intext, "001#", "|")
        Print #2, outtext
        
        outtext = Replace(intext, "002#", "")
        Print #2, outtext
        
    Loop
    
    Close
    
End Sub
 
Hi Thanks for your help. The problem with this code is also the same as before as the program reads the first line in the file and finds 001# and replaces it but next to it is 002# but it does not replace this. On the second line the 001# is not replaced but 002# is replaced. And also extra data is being written in the file.
Here is the sample of the text file I read
|001#AKA|002#STO|
|001#WAR|002#STO|

Here is the result after running the program
||AKA|002#STO|
|001#AKA|STO|
||WAR|002#STO|
|001#WAR|STO|

Please can you see where I am going wrong.
Thanks
 
Hi Thanks for your help. The problem with this code is also the same as before as the program reads the first line in the file and finds 001# and replaces it but next to it is 002# but it does not replace this. On the second line the 001# is not replaced but 002# is replaced. And also extra data is being written in the file.
Here is the sample of the text file I read
|001#AKA|002#STO|
|001#WAR|002#STO|

Here is the result after running the program
||AKA|002#STO|
|001#AKA|STO|
||WAR|002#STO|
|001#WAR|STO|

Please can you see where I am going wrong.
Thanks

You are printing the same line multiple times, the first print is being done after you have replaced the 001#, the second print is being done after you have replaced only 002#

Try:

Code:
Open "C:\temp\test.txt" For Input As #1
    Open "C:\temp\certi.txt" For Output As #2
    
    Do While Not EOF(1)
        Line Input #1, intext
        
        intext = Replace(intext, "001#", "|")
        intext = Replace(intext, "002#", "")
        Print #2, intext
        
    Loop
    
    Close
 

Users who are viewing this thread

Back
Top Bottom