Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing text file

Status
Not open for further replies.

camidon

Programmer
May 9, 2000
268
US
I'm having some problems with one of my scripts. I am taking a text file and is asterisk delimited with different sections delimited with a tilde. there are no carriage returns or line feeds in the file (took care of that with a different script). Now what I would like to do is put a carriage return in front of each tilde. I have a script to do this, but when I execute it, it just fills up the file with a bunch of CRLF's. Here is my code:

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set fso = CreateObject("Scripting.FileSystemObject")
Set ReadFile = fso_OpenTextFile("c:\temporary\ms2.txt", ForReading, TristateFalse)
thisTxt = ReadFile.ReadAll
Readfile.close

replacetxt = replace(thistxt, "~", vbCR & "~", 1, -1, 1)

set WriteFile = fso.CreateTextFile("c:\temporary\msparsed.txt", ForWriting, True)
WriteFile.Write(replacetxt)
WriteFile.Close
Msgbox "Done parsing data"

Any ideas as to what I'm doing wrong?? :)

Thanks in advance,

Chris
 
loop the reading of txt file 1 and the writing of txt file 2 in a FOR... NEXT
 
Try this :
Code:
tmptxt = Replace(thistxt, "~", Chr(1))
replacetxt = Replace(tmptxt, Chr(1), vbCR & "~")

Hope This Help
PH.
 
Every time I run the script, I get a "ÿþI" as the first 3 characters and then the rest of the file filled with carriage return line feeds. Any other ideas on this one?

I'm interested in the logic required to do the looping that smithsco was talking about doing.

thanks for the replies so far guys!

Chris
 
Another try:
Code:
myArray = Split(thistxt, "~", -1, 0)
replacetxt = Join(myArray, vbCR & "~")

Hope This Help
PH.
 
Same exact output for some reason. If I can determine the position of each ~ what code can I use to put a CRLF in front of the ~

Lets assume that I know the position of every ~ in the file. I just want to know how to insert a character.
 
Brute force method:
Code:
replacetxt = ""
For I = 1 To Len(thistxt)
  X = Mid(thistxt, I, 1)
  If X = "~" Then
    replacetxt = replacetxt & vbCR & "~"
  Else
    replacetxt = replacetxt & X
  End If
Next

Hope This Help
PH.
 
PHV, that concept worked nicely.

Thanks for the help!

Chris
 
Try using the Regular Expression:

**** code begins ***
option explicit
'declare vars
dim objFSO
dim objFile
dim objReadFile
dim strContents
dim strNew
dim patrn
dim objWriteFile
'use this constant to open your file for reading
Const ForReading = 1
Const ForWriting = 2
'instantiate FileSystemObject
set objFSO = CreateObject("Scripting.FileSystemObject")
'get the file name
set objFile = objFSO.GetFile("X:\path\filename.txt")
'check to see that it's not a sparse file
If objFile.Size > 0 Then
'open the file for reading
set objReadFile = objFSO.OpenTextFile_("X:\path\filename.txt",ForReading)
'read in the file and store contents to a single string
strContents = objReadFile.ReadAll
'close the file
objFile.Close
'make the replacement using the ReplText function
strNew = ReplText(~, "~"& vbCr)
'open the file for writing
objWriteFile = objFSO.OpenTextFile_("X:\path\filename.txt", ForWriting)
'write it
objFile.Write strNew
'report completion
Wscript.echo "Replacement complete."
else
Wscript.echo "The file is empty."
End if

Function ReplText(patrn, replStr)
Dim regEx, str1 ' Create variables.
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Make case insensitive.
ReplText = regEx.Replace(strContents, replStr) 'do it.
End Function

Hope this helps.
Rob Mitchell
Server Janitor
AutoZone, Memphis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top