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

Delete line from text file 3

Status
Not open for further replies.

Ed Andres

IS-IT--Management
Mar 27, 2001
142
US
I need to delete the first line of a .txt file. Can this be done thru foxpro? If so, how? This seems so simple but I am stumpted.

Thanks,
Ed
 
Hi Ed,

Just for giggles try this.

tempString = FILETOSTR("anyfile.txt")
tempString = RIGHT(tempString, LEN(tempString) - ;
AT(CHR(13)) - 1)
STRTOFILE(tempString, "newfile.txt")

Regards,

Mike
 
Mike,
Seems to work with the following changes.

tempString = RIGHT(tempString,;
LEN(tempString)-AT(CHR(13),tempstring)- 1)

Thanks for your help
Ed
 
Hi Ed,

Oops, you're right.[ponder]

Forgot the other argument for AT().

Glad you got it working.

Regards,

Mike
 
Suppose you need to delete the last line in a text file( i.e., a trailer record with totals)? I would like to be able to delete both the first line AND the last line in an ASCII file, so I can import it into a dbf. How do you do that?
 
You could do

Code:
tempString = FILETOSTR("anyfile.txt")
lnLines = ALINES(laLines, tempString)
lcOut = ""
IF lnLines < 3
   *-- Not enough lines
   lcOut = "DEFAULT VALUE"  && Replace with default value
ELSE
   *-- SKIP first and last
   FOR ln = 2 TO lnLines - 1
       lcOut = lcOut + laLines(ln) + CHR(13)
   NEXT
ENDIF
STRTOFILE(lcOut, "anyfile.txt")



Jean
 
Oups.. If you wanted to import into a DBF you would simply issue an Insert statement in the FOR - NEXT Loop : Insert into mytable (MyLine) VALUES (laLines(ln))




Jean
 
RogerRuntings,
In Mike's code, subsequent to the line

tempString = RIGHT(tempString,;
LEN(tempString)-AT(CHR(13),tempstring)- 1)

include:
Code:
TempString = LEFT(tempstring,RAT(CHR(13),tempString,1)-1)
In the above code, it is assumed that the last line does not end with chr(13). If it does, Change the above line to:

Code:
TempString = LEFT(tempstring,RAT(CHR(13),tempString,[COLOR=red][b]2[/b][/color])-1)

Rajesh
 
Simplest way I know of doing this is to use a UDF:

TempString = ATXRIGHT(TempString, CHR(13))


See FAQ182-5975 for the code for this UDF.


mmerlinn

"Political correctness is the BADGE of a COWARD!"

 
As usual, you guys always seem to have the answer to all us newbies sometimes silly questions. Makes me wonder why people keep saying that Foxpro is outdated, but the code to me always looks shorter (and simpler) than comparable VB or dot.net code.

Well,off to try the(se)code(s)before I dish out the deserving STARS!!

Thanks in advance - this is going to save my life!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top