Microsoft KB Archive/113278

= How to replace a string in a text file =

Article ID: 113278

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft FoxPro 2.0
 * Microsoft FoxPro 2.5b for MS-DOS
 * Microsoft FoxPro 2.5a
 * Microsoft FoxPro 2.5b for MS-DOS
 * Microsoft FoxPro 2.6 for MS-DOS
 * Microsoft FoxPro 2.5b
 * Microsoft FoxPro 2.5a
 * Microsoft FoxPro 2.5b
 * Microsoft FoxPro 2.6 Standard Edition
 * Microsoft FoxPro 2.5b for Macintosh
 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 7.0 Professional Edition
 * Microsoft Visual FoxPro 8.0 Professional Edition
 * Microsoft Visual FoxPro 9.0 Professional Edition

-



This article was previously published under Q113278



2.00 2.50 2.50a 2.50b 2.60 | 2.50 2.50a 2.50b 2.60 3.00 | 2.50b MS-DOS | WINDOWS | MACINTOSH kbprg





SUMMARY
FoxPro does not contain a function for replacing a character string in an ASCII text file. One way to do this is to use low-level file functions. A simpler way is to copy the text file to a memo field, make the change using STRTRAN, and copy the memo field back to an ASCII file, as demonstrated below.



MORE INFORMATION
The following steps demonstrate this procedure:  Create an ASCII text file that contains the following sentence: This sentence demonstrates how a character string can be changed. Create a database that contains only a memo field. Append a blank record to that database.  Create a program that contains the following code. USE APPEND MEMO FROM  OVERWRITE Temp=STRTRAN( ,"demonstrates","shows") REPLACE WITH Temp COPY MEMO TO   Run the program.</li> Open the ASCII file; the word "demonstrates" should now read "shows".</li></ol>

If the same ASCII filename is used in the APPEND MEMO and the COPY MEMO commands, the file will be rewritten with the changes. The OVERWRITE parameter on the APPEND MEMO command is needed so that the current text file overwrites the existing contents of the memo field. This technique can also be used to remove escape codes and page formatting from files by placing the characters to remove in the STRTRAN function.

Additional query words: VFoxWin FoxMac FoxDos FoxWin

Keywords: KB113278

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.