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

Find and replace in Text File 2

Status
Not open for further replies.

sinyce

IS-IT--Management
May 27, 2002
57
US
Every day there are five text files saved to a directory
(these are AS/400 spooled files)
STMT.TXT
JOURNAL.TXT
MESSAGE.TXT
GL0240.TXT
REPORT.TXT


In all the files each record is separated by a line of "----" looks thus, in all cases the "------" are of equal length.

STMT.TXT

REFERENCE STMT STM5040

ASSETS AND LIABILITIES

1050 CASH

CHASE 101010 USD 25000 DR 300000 CR

---------------------------------------------------

1060 INVESTMENTS

.....

---------------------------------------------------


Is is possible to loop thru each text file do a search
and replace the line that contains the "---------"
with a single character say "&" or "@" or "%"


So the above example would look thus:

REFERENCE STMT STM5040

ASSETS AND LIABILITIES

1050 CASH

CHASE 101010 USD 25000 DR 300000 CR

&

1060 INVESTMENTS

.....

&


 
HI,
Try this:
Code:
*** /    INDENT IV  SOURCE INDENTOR                                        *** /
*** /                                                                      *** /
*** /    FILE I.D. [TEKTIPS.PRG]                                           *** /
*** /                                                                      *** /
*** /                                                                      *** /
PARAMETER INFILE, OUTFILE, REPLSTR
PRIVATE HANDLE,TMPLN,EOFTEMP
EOFTEMP = .F.
IF PCOUNT() < 3
  REPLSTR = &quot;@&quot;
ENDIF
IF PCOUNT() < 2
  CLEAR
  ? &quot;USAGE: TEKTIPS INFILE.TXT OUTFILE.TXT [REPLACEMENT STRING]&quot;
ELSE
  IF FILE(INFILE)
    SET ALTE TO (OUTFILE)
    SET ALTE ON
    SET CONSOLE OFF
    HANDLE= FOPEN(INFILE)
    DO WHILE .NOT. EOFTEMP
      TMPLN = FGETSTR(HANDLE,@EOFTEMP)
      IF TMPLN = &quot;------&quot;
        TMPLN = REPLSTR
      ENDIF
      ? TMPLN
    ENDDO
    SET ALTE OFF
    SET ALTE TO
    FCLOSE(HANDLE)
  ENDIF
ENDIF
*** /                                                                      *** /
*** /                    FUNCTION FGETSTR                                  *** /
*** /                                                                      *** /
*** /                                                                      *** /
FUNCTION FGETSTR
  PARAMETER HANDLE,EOFTEMP
  PRIVATE HANDLE,STRING,BUFF,CONTFLG
  CONTFLG = &quot;Y&quot;
  BUFF = &quot; &quot;
  STRING = &quot;&quot;
  DO WHILE CONTFLG = &quot;Y&quot;
    IF FREAD(HANDLE,@BUFF,1) = 1
      IF BUFF <> CHR(13)
        STRING = STRING + BUFF
      ELSE
        FREAD(HANDLE,@BUFF,1)
        CONTFLG = &quot;N&quot;
      ENDIF
    ELSE
      EOFTEMP = .T.
      CONTFLG = &quot;N&quot;
    ENDIF
  ENDDO
RETURN(STRING)
*** / Indented on  [06/24/02]                                              *** /
*** / Errors Found [   0]                                                  *** /
*** /                                                                      *** /
*** / Functions & Procedures :                                             *** /
*** / FUNCTION FGETSTR                                                     *** /
*** / End Of File  [TEKTIPS.PRG]                                           *** /
*** /                                                                      *** /
*** /                                                                      *** /
Regards

Griff
Keep [Smile]ing
 
Your Question
------------------------------------------------------------
Is is possible to loop thru each text file do a search
and replace the line that contains the &quot;---------&quot;
with a single character say &quot;&&quot; or &quot;@&quot; or &quot;%&quot;


Answer.. Yes

Solution:

Option 1======================================
Open the file you wish to convert... Next you will save the contents of that file to a variable. Use StrTran function to convert the lines to the character you want.

EG:

Local cText := &quot;&quot;

// this will open the file and store its contents
// into the ctext varaible
cText := MemoRead(&quot;STMT.txt&quot;)

// this will do the conversion for you..
cText := StrTran(cText,&quot;---------&quot;,&quot;&&quot;)

/* now in the cText variable you have your file without the ----------- seperators but rather you have & in their place */

At this point you can write it back out to another file or overwrite it.... it is your choice... I hope this helps you out.


DarknessVB@aol.com

Richard L. Hankins Jr.
Senior Programmer
Auction Services, Inc.
 
Hi,

using MemoRead is ok - but might be a problem with large files (> 64K)

Regards

Griff
Keep [Smile]ing
 
Hi

Import the file (txt) file to DBF and the replace the unwanted things.
if U need more detail or help send me a small txt file and I'll send U the
prog.

Pls mail me on xpert@vsnl.com

Hemant Pandya

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top