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

VFP file help

Status
Not open for further replies.

miller8b

MIS
Jul 5, 2011
9
US
What would be the simplest way to open a csv file, read the first line, find a ~ and replace with a comma, write that line to a new file and then do the same for each line in the file?

Haven't used VFP in years and am looking for some advice before spending hours with the manuals.

Any help appreciated...I think VFP 8 is the version I have.
 
Hi

Try this:
Code:
StrToFile(StrTran(FileToStr("C:\MyFile.CSV"),"~",","),"C:\MyFile.CSV")

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Thanks GriffMG, you did in a couple of minutes what would have taken me hours of pouring through books and google hits. I appreciate it.

A followup if you don't mind. If I wanted to delete the original file at some point would it be better to just delete the dir where it resides and recreate it or would it make more sense to just delete the file directly. I believe the original file will either have the same name everyday or might be a date name i.e. 7052011.csv.
 
I don't quite understand your question.

The code I gave would overwrite the original file.

Generally, if I want a file zapped I delete it, rather than the folder it resides in - but perhaps yours exists in a new file for each day... in which case I would delete it, then the folder.

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Oh OK. I didn't realize that would overwrite. I read and then gave a new name so I didn't notice that it would overwrite. I'll test that now. Thank you so much.
 
Good luck.

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Griff, how that overwrote like you said. How do you overwrite without getting a prompt telling you that the file already exists...do you want to overwrite?
 
Because the code is somewhat low level, it just overwrites.

You could put a prompt in there or make it more readable...
(it is a bit terse - like me)

Code:
m.MyString = FileToStr("C:\MyFile.CSV")

m.MyString = StrTran(m.MyString,"~",",")

If MessageBox("OverWrite File",52,"Are You Sure") = 7
  StrToFile(m.MyString,"C:\MyFile.CSV")
EndIf



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
The original code pops a prompt and asks if it is OK to overwrite. I think they want this to happen in the background without any user intervention so I was hoping there was a parameter that tells it to "just overwrite and don't ask me" :)
 
Right, you are pushing the intent of TT a little, but here is how you would do that...

Code:
CleanCode("c:\MyFile.csv",.t.)

Function CleanCode
  Parameters m.FileName, m.BeQuiet
  Private m.FileName, m.BeQuiet
  Private m.MyString
  m.MyString = FileToStr(m.FileName)
  m.MyString = StrTran(m.MyString,"~",",")
  If m.BeQuiet
      StrToFile(m.MyString,m.MyString)
  Else
    If MessageBox("OverWrite File",52,"Are You Sure") = 7
      StrToFile(m.MyString,m.MyString)
    EndIf
  EndIf
Return(.f.)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Thank you. I have a lot of things scheduled and this has a deadline. I knew there were Foxpro people here that could save me hours and hours of going through a language I haven't dabbled in in more than 10 years. VFP is just so different than the VBA, VBScript, etc. that I'm used to.

I appreciate your responses.
 
Good luck again

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
How do you overwrite without getting a prompt telling you that the file already exists...do you want to overwrite?

VFP has an environment setting SET SAFETY ON/OFF
Look in your VFP Help by typing HELP SET SAFETY in your Command Window.

Your encountering the prompt asking about over-writing an existing file - it is due to SET SAFETY ON 'seeing' the existence of the file and prompting the user.

If you proceed your command to create/write the file anywhere with a SET SAFETY OFF you will no longer get the prompt - as long as somewhere else between your setting this OFF and the create/write the setting is not turned ON by something else.

Many developers choose to intentionally SET SAFETY ON if the command will be executed by users so that they knowingly over-write a file and not do so accidently.

If the over-write might be done programatically (such as automatically creating some output file), then many developers SET SAFETY OFF, since the program already 'knows' what it needs to do.

Good Luck,
JRB-Bldr






 
Thanks for the feedback JRB. I'm just now getting back on that project, due tomorrow. This is the final piece that will get me accross the finish line :)
 
All the advice above works great. I had a curve thrown to me in the 11th hour. Initially I just needed to cycle through a directory and pick up all file names and once I did I would open the first file, find the ~, replace with a , and write out the entire file, overwriting the original, pick up the 2nd file name, etc.

Now I need to open a file, read in each line, change some of the data position and write back out to a new file. I understand how to pull the line apart, format and put back together in a different sequence as that is similar to visual basic.

How do you write all the lines of data to the new file so if the old file had 1000 lines the new file does also instead of overwriting each line with the next and ending up with a 1 line file?

 
One way would be to use FILETOSTR() to 'read' the original file contents into a single memory variable (char string).

Then change what you need to change in the memory variable char string in whatever manner meets your needs STRTRAN(), STUFF(), etc. And then write the string back out to a file using STRTOFILE(). All of those VFP functions are detailed in your VFP Help.

There are other ways including use of VFP low-level functions such as FOPEN() {open the file}, FGETS() {read a line}, make the changes, FPUTS() {write a line}, FCLOSE() {close the file}, etc. (again look into your VFP Help for details).

And you could consider creating a recipient VFP data table and import the data into that table's fields with APPEND FROM <your file> SDF or DELIMITED. Then change what you need and write it back out using COPY TO <your new file> DELIMITED or whatever. Again look into your VFP Help for details.

Good Luck,
JRB-Bldr
 
I've tried all the fputs, fgets, fopen etc. and I am able to parse through my file one line at a time, reorganize the data but I'm getting a blank file using fputs. I've used VB, VBA, VBscript for years but VFP is just a different creature, at least to me.

Visual Foxpro won't be installed on the target machine. It is an app that uses VFP tables but VFP isn't actually installed as an application.
 
Well, FPUTs is the right function to write a line including CRLF. FGETS also expects CRLF, if your input file just has LF, then you're out of luck, then it reads in the whole file as one line, discarding the LFs. Maybe that's the root of your problem?

Bye, Olaf.
 
Alos, miller8b, you better make a new thread out of this, I already regarded this thread as closed, as your first question was answered and only came by again out of curiosity. Others might overlook this.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top