Click to See Complete Forum and Search --> : File Editing and Formatting


tschilbach
July 10th, 2001, 03:55 PM
Hello all,

I have an application that simply takes a formatted text file and replaces a phone number in it. The user just simply adds the phone number and then click on the command button and it reads the file into memory, replaces the string, and then writes it back to file. It does everything Just great!

My problem is the fact that it cuts ALL my whitespaces and formatting from the file. If this file is off by one whitespace, my decoder will not work at all. My file goes from 9k to 2k because this code cuts out all my white spaces. Anyone have any ideas how I can accomplish my mission while keeping the file format intact? Here is my code:

Option Explicit

Private Sub Command1_Click()
Dim strTextFile As String
Dim strTextLine As String
Dim strPhoneNumber As String

strPhoneNumber = Text1.Text ' Get the phone number

Open "C:\temp\Test.txt" For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, strTextLine ' Read line into variable.
strTextFile = strTextFile & strTextLine & vbCrLf ' Collect the input file.
Loop
Close #1 ' Close file.

strTextFile = Replace(strTextFile, "<PhoneNumber>", strPhoneNumber) 'adds the public text vatable that the user input overwriting the <PhoneNumber> text

Open "C:\temp\Test.txt" For Output As #1
Print #1, strTextFile
Close #1

End Sub


Timothy H. Schilbach
Alpha Omega Design Inc.
tschilbach@aodinc.com

Cimperiali
July 11th, 2001, 07:39 AM
TRy:

Dim strTextFile as string
Dim strTextLine as string
strTextFile = "Blabla blabla " & vbCrLf & " ababa 2 ababa ababa "
Open "c:\txtmy.txt" for Output as #1
print #1, strTextFile
Close #1
strTextFile = ""
Open "c:\txtmy.txt" for input as #1
Do While Not EOF(1) ' Loop until end of file.
Line input #1, strTextLine ' Read line into variable.
strTextFile = strTextFile & strTextLine & vbCrLf ' Collect the input file.
Loop
Close #1
'strip last terminator
strTextFile = Left$(strTextFile, len(strTextFile) - 2)
strTextFile = Replace(strTextFile, "2", "024043", , , vbBinaryCompare)
Open "c:\txtmy.txt" for Output as #1
print #1, strTextFile
Close #1




Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.

tschilbach
July 11th, 2001, 11:14 PM
Thank you for the code. I adapted it to my situation and it stille truncated ALL whitespaces out of my file. Would you be interrested in seeing the before and after result of this code? I can zip them and send them too ya.

Here is the script adaption:

Option Explicit

Private Sub Command1_Click()
Dim strTextFile As String
Dim strTextLine As String
Dim strTextInput As String

strTextInput = Text1.Text

strTextFile = ""
Open "c:\temp\dialup\default.set" For Input As #1
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, strTextLine ' Read line into variable.
strTextFile = strTextFile & strTextLine & vbCrLf ' Collect the input file.
Loop
Close #1 'strip last terminator

strTextFile = Left$(strTextFile, Len(strTextFile) - 2)
strTextFile = Replace(strTextFile, "<PhoneNumber>", strTextInput, , , vbBinaryCompare)

Open "c:\temp\dialup\default.set" For Output As #1
Print #1, strTextFile
Close #1
End Sub


Timothy H. Schilbach
Alpha Omega Design Inc.
tschilbach@aodinc.com

Cimperiali
July 12th, 2001, 05:17 AM
interrested in seeing the before and after result of this code:
e-mail it to
cimperiali2000@yahoo.it
I will take a look and give you an answer (succesfull, I hope) within tomorrow.
Cesare

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.

Cimperiali
July 13th, 2001, 02:58 AM
'add a richtextbox control (visible= false) to a form, and try this code.
'Your matter is : you have a lot of chr$(6) in your text. Look at after file
'with your application. Hope this may solve.

private Sub Command1_Click()

With RichTextBox1
.FileName = "C:\before.txt"
'use "<" and ">" as they are readed this way
.Text = Replace(.Text, "<PhoneNumber>", "02020202", , , vbBinaryCompare)
.SaveFile "c:\after.txt", rtfText
'debug:
'.FileName = "c:\after.txt"
End With
End Sub





Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.

tschilbach
July 13th, 2001, 07:58 PM
Thank you for the help. I never even thought of using that pesky RichTextBox :-)

Good thing I have the Enterprise Version :-)

If there is anything I can ever do for you let me know. I will finnish my testing when I go and sdecode the file. I will let you know how it all turns out.


Timothy H. Schilbach
Alpha Omega Design Inc.
tschilbach@aodinc.com

Cimperiali
July 16th, 2001, 02:29 AM
...Just let me know if it worked in your case...
Best regards,
Cesare

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.