/* -------------------------------------------------------- Filename.....: quoteclose.prg ClassName....: Purpose......: Create character fields of .csv files for importing / appending to tables Programmer...: Lee Grant Date.........: 4/11/2019 Note........: File in must be named "Test.csv", output file will be named "Testconv.csv". Running the program again will delete the previous "Testconv.csv" and create a new one Written for..: Practice and because of a question in the dBASE Newsgroups Rev. History.: 1.0 finished working version after trial and errors Dependencies.: HelpFile.....: Example:.....: -------------------------------------------------------- Notes: this isn't fancy, it takes a file in the current directory and will process it creating another file with the converted .csv file. dBASE won't append mixed data, so this converts the fields in the file to charcter fields by enclosing them in quotes. To append them into a new database, you need to create the fields in the format in a new database that you want them before appending with this file ..its really basic and handles only the CR/LF combo of CHR(13)+CHR(10) to read in characters and process it. -------------------------------------------------------- */ // Create the constants so we know what filenames we are going to work on. The // incoming file will be named cTest, and the output file, cConv. cTest = "test.csv" cConv = "testconv.csv" // We need two file objects. One to read in the importing file and one to // write out the result after transformation. fIn = new file() fOut = new file() // open the incoming file fIn.open(cTest , "R") // for this time, we're just going to create an arbitrary file to copy into // so if it exists, delete it and create a new one. if fOut.exists(cConv) fOut.delete(cConv) endif fOut.create(cConv, "W") // Need a variable to check for a "," and if so, file.write() a open/close quote // and then the variable holding the comma delimiter. local cIn, cOut cOut = '"' // Now the file needs to be parsed per character and processed per comma entry // so start at begining of input file, start the clean file with an open quote // to start us off fIn.seek(0) fOut.write(cOut) do while not fIn.eof() cIn = fIn.read(1) if cIn == "," // Comma-seperated value, fOut.write(cOut) // close the quote, fOut.write(cIn) // write comma, fOut.write(cOut) // open quote. elseif cIn == CHR(13) // CR/LF pair, put in close quote here before feed fOut.write(cOut) fOut.write(cIn) elseif cIn == CHR(10) // CR/LF pair, put in open quote here after feed. fOut.write(cIn) fOut.write(cOut) else fOut.write(cIn) // Write the current character, it's part of the data. endif enddo fOut.write(cOut) // We're at the end of the incoming file, so..let's close the // the last field up with a closing quote. // We're done copying the file over position by position, so now close the files // release the variables and exit. fOut.close() fIn.close() release fIn, fOut, cIn, cOut // done return