0

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
Keertika
  • 59
  • 6

1 Answers1

3

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.

Richard Schwartz
  • 14,463
  • 2
  • 23
  • 41
  • 1
    Thanks Richard. I got error "File is already open" when I used Open xlFilename$ + ".output.csv" For Output As Filenum2%. I replaced Filenum2% with some random number like Open xlFilename$ + ".output.csv" For Output As 4 and it worked. Thanks for your help – Keertika May 31 '22 at 10:44
  • 1
    @Keertika - The reason you get "File already open" is that you open `Filenum1` and `Filenum2`, but then you don't close any of them, you close `Filenum`. So you are leaving both the input file and the output file open after the code is finished executing. On a side note, try using better variable names. That will make the code easier to read, and it will prevent this kind of errors. I would have used `InFile` and `OutFile` for the two file numbers. There is also no need to use $ and % after the variable names, especially if you give them good and descriptive names. – Karl-Henry Martinsson Jun 10 '22 at 13:14