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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search And Replace in Exisiting Text Files...

Status
Not open for further replies.

efinnen

Technical User
Feb 21, 2000
55
US
Hi All

Standard Comment to Start with any help and or guidance would be greatly appreciated...

I need to take a text file/spreadsheet load it into a collection and take certain values from that to populate a text file.

One the first run there will be about 2000 records (many more to follow, which I want to generate 2000 text files. The output text files have to conform to a format that I optimally want to do a Search and Replace with (any other suggestions??).

What I would like to do is have a template text file, about 12 pages... With text (VARIABLE1, VARIABLE2 etc)defined that will be replaced with the spreadsheet values (about 25 of them), and then save it out with a different file name.

Hopefully the question makes sense. If you have any questions please feel free to post them and I will reply asap.

I'll tell you what I can accomplish

1) Read the Input Files
2) Pull The Variables from the files
3) Generate Text files (with unique Names) containing just the variables. (But NOT with all the other data and header info, from my template)


Thanks
Eric
efinnen@hotmail.com
 

Sounds like you have it just about beat, but what are you looking for? A suggestion on how to create the places that you will want to replace?
[tt]
Welcome <<SAL>> <<LNAME>>...
[/tt]
To use the Replace function like...
[tt]
String = Replace(String,&quot;<<SAL>>&quot;, Variable1)
[/tt]

Did I make any sense?

Good Luck

 
Maybe I'm just slow here... Or having a brainfart.

Any guidance on Reading that text file in and then replacing it with my variables. The best approach I've had so far is with this But that is having some issues as well (go figure).

I must confess I'm not that proficient with the Replace function. It seems that I can edit the string variable but then I have a hell of a time writing that back out to the text file.

Thanks
-e
 

Taking my example from above lets say you have the following text file as a master...
[tt]
Dear <<SAL>> <<LNAME>>,

This is a reminder that on <<APTDATE>> we will arrive to start work on...
[/tt]
and you have a database that has the following fields...
[tt]
CustID,Sal,LName,FName,MI,APTDate
[/tt]

You can do one of two things to read in the master file. You can read it all in at once and then do your replace on that string or you can read it in line by line and do your replace on that string. The following is read it in at once.
[tt]
Public Function ReadInMaster(PathFileToMaster As String) As String

Dim FName As String, FNumb As Integer

FName = PathFileToMaster
If Dir(FName) = &quot;&quot; Then Exit Function
FNumb = FreeFile
Open FName For Binary Access Read As #FNumb
ReadInMaster = Input(FileLen(FName), #FNumb)
Close #FNumb
Exit Function
[/tt]

Now you have the master file in a string from the procedure that called the read in, say...
[tt]
StringVariable = ReadInMaster(&quot;Your Path To The Master File&quot;)
[/tt]

Now you want to replace your markers with values from your fields, so you could...
[tt]
OutPutString = Replace(StringVariable, &quot;<<SAL>>&quot;, adoRs.Fields(&quot;Sal&quot;))
OutPutString = Replace(OutPutString, &quot;<<LName>>&quot;, adoRs.Fields(&quot;LName&quot;))
OutPutString = Replace(OutPutString, &quot;<<APTDate>>&quot;, adoRs.Fields(&quot;APTDate&quot;))
[/tt]

Then you want to output the entire new string to a new file.
[tt]
Public Sub OutPutFile(StringToPrint As String, PathFileName As String)

Dim FName As String, FNumb As Integer
FName = PathFileName
FNumb = FreeFile
Open FName For OutPut As #FNumb
Print #FNumb, StringToPrint
Close #FNumb
End Sub
[/tt]

I belive this is what you wanted to do. Am I closer?

Good Luck

 
Ok, I know this is a reply to an old post, but I'm trying to do this kind of thing, but am having a little trouble. I am following along the lines of what vb5 said above, but when I try to write to a new file, I have problems. My problem is that when I do a replace to get my new output string, I get an error (&quot;The document name or path is not valid&quot;) and cannot open it. If I comment out the replace lines, then the output works fine, but obviously does not do a replace like I need. Here is my code, any help would be greatly appreciated.

Private Sub cmdCreate_Click()
Dim intFileIn As Integer
Dim intFileOut As Integer
Dim strFileIn As String
Dim strFileOut As String
Dim strContents As String
Dim strOutput As String

intFileIn = FreeFile
strFileIn = &quot;C:\test.doc&quot;

Open strFileIn For Binary Access Read As #intFileIn
strContents = Input(FileLen(strFileIn), #intFileIn)
Close #intFileIn

strOutput = Replace(strContents, &quot;<<#Field1#>>&quot;, txtField1.Text)
strOutput = Replace(strOutput, &quot;<<#Field2#>>&quot;, txtField2.Text)

intFileOut = FreeFile
strFileOut = &quot;C:\TestOut.doc&quot;

Open strFileOut For Output As #intFileOut
Print #intFileOut, strOutput
Close #intFileOut
End Sub
 

ok, this code worked fine for me...
[tt]
Dim intFileIn As Integer
Dim intFileOut As Integer
Dim strFileIn As String
Dim strFileOut As String
Dim strContents As String
Dim strOutput As String

intFileIn = FreeFile
strFileIn = &quot;C:\test.doc&quot;

Open strFileIn For Binary Access Read As #intFileIn
strContents = Input(FileLen(strFileIn), #intFileIn)
Close #intFileIn

strOutput = Replace(strContents, &quot;<<#Field1#>>&quot;, &quot;Replaced Field 1&quot;)
strOutput = Replace(strOutput, &quot;<<#Field2#>>&quot;, &quot;Replaced Field 2&quot;)

intFileOut = FreeFile
strFileOut = &quot;C:\TestOut.doc&quot;

Open strFileOut For Output As #intFileOut
Print #intFileOut, strOutput
Close #intFileOut
[/tt]

notice the only changes I made to your code is with the replace functions (instead of reading a value from a text box).

Now my origional text contained the following...
[tt]
<<#Field1#>>
<<#Field2#>>
[/tt]
and my output was...
[tt]
Replaced Field 1
Replaced Field 2
[/tt]

Now I also see that you are using the *.doc extension. Did you use word to view the contents of the output file? If so is word still open? (Check to see if it is in memory.) If it is still open then you may be experiencing a file locking issue but at this point I doubt it. Do you have permission to write the output file to disk?

Good Luck

 
I do have the appropriate permissions, and word is not in memory (I also restarted to make sure). If I take out the textboxes, I can get it to write a file that I can open, but it seems to still be binary, it has all the y's and squares in it. If I set variables to what the textboxes were and then use those variables for the replace, I still get the same error as before. Any further ideas? thanks in advance
 
Is there something weird when opening a word document? If I copy my original text into notepad, and just save as .txt, then I can read it in and write to a word document just fine.
 

Yes there are some major differences between a regular text file and a word document, and if you are using the latest version of word I think I read somewhere that it will save files in an XML format instead of the OLE 2.0 file format (or something like that(Could be wrong)).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top