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

removing commas from text files automatically

Status
Not open for further replies.

dougerstew

Programmer
May 30, 2003
54
US
I have been manually finding/replacing commas from textfiles before I import into foxpro 2.6, as it cuts off imported numbers if I don't remove commas. I would think there should be a way for a program to automatically remove them without having to stop the program to do it manually. Does anyone have a suggestion?

Thanks!
 
Dave,

I should have clarified. I am importing as in the system data format (SDF) and I only want numbers to have their commas eliminated before importing.

Thanks,

Doug
 
If all you want to do is replace ALL commas, this will get you started:
Code:
STORE 0 TO nInHandle, nOutHandle
STORE '' TO cTmpStr
STORE 'InFile.TXT' TO cInFile

nInHandle = FOPEN(cInFile)
nOutHandle = FCREATE('OutFile.TXT')

IF nInHandle < 0 .OR. nOutHandle < 0
   ?'Error'
   RETURN
ENDIF

DO WHILE !FEOF(nInHandle)
   cTmpStr = FGETS(nInHandle)
   cTmpStr = ChrTran(cTmpStr , ',', '')
   =FPUTS(nOutHandle, cTmpStr )
ENDDO
=FCLOSE(nInHandle)
=FCLOSE(nOutHandle)
ERASE (cInFile)
RENAME outfile.txt TO (cInFile)

If you need to replace commas at a certain position, you will have to do some parsing and replace a portion of it.
You can use LEFT(), SUBSTR(), RIGHT(), etc., for that sort of stuff.
Let us know if you need more help.


-Dave S.-
[cheers]
Even more Fox stuff at:
 
Thanks, Dave. I'll give it a try and let you know if I have any problems.

Doug
 
Dave,
I think it's a bit more complicated than this. With fixed width columns, you'd need to not only kill the commas, but put back leading spaces in the data &quot;column&quot; for any removed. i.e.
Code:
ABCDEF 1,234,567GHIJ ... (before)
ABCDEF 1234567GHIJ ... (your code's after)

ABCDEF 1,234,567GHIJ ... (before)
ABCDEF   1234567GHIJ ... (expected after?)
Rick
 
You're absolutely right. That's why I said in my post ...this will get you started.
One would probably want to grab a chunk of the string, check the length, remove commas, pad it with spaces, reattach, ....
Repeat as needed.


-Dave S.-
[cheers]
Even more Fox stuff at:
 
Dave,
Even I missed the &quot;.. this will get your started.&quot; part. (Maybe this &quot;short&quot; week has me off balance a bit. :)) I guess I just thought it was important to point out that this was not a &quot;canned&quot; solution like we sometimes post here.

Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top