Home > Software design >  How to read and write to the same csv file using lotusscript?
How to read and write to the same csv file using lotusscript?

Time:05-31

I have a CSV file. I need to read each line from this file, perform certain function and once done, I need to append a value something like "updated" at the end of each line. Can someone help me to achieve this using LotusScript ? I am using "open file for input" statement to open and read the file. I want to append the value after the function is performed at each line.

Open xlFilename$  For Input As Filenum%
Do Until Eof(Filenum%)
  Line Input #Filenum%, txt$
  'perform certain function
  **'Would like to append csv here.**
loop
close Filenum

CodePudding user response:

If you were appending new rows at the end, you would need to close the file, and then Open it again to append. I.e., after your loop...

Close Filenum
Open xlFilename$ For Append As Filenum2
Print #Filenum2, stuffYouWantToAppend

But I see you are trying to append new columns at the end of each row. It's going to be much easier if you simply treat your existing file as input and create a new one that you will Print to for output.

Open xlFilename$  For Input As Filenum1%
Open xlFilename$   ".output.csv"  For Output As Filenum2%
Do Until Eof(Filenum%)
  Line Input #Filenum1%, txt$
  'perform certain function
  Print #Filenum2%, txt   ","   additionalColumnsGoHere
loop
close Filenum

And if you want, you could then Kill your input file and then rename your output file back to the original file's name using the Name statement.

Doing it in place is probably possible by opening the file in RANDOM READ WRITE mode, but it just isn't worth the trouble.

  • Related