
Quote from Rob Maggs on November 10, 2022, 4:17 pmHi guys, I've searched high and low through the documentation and am struggling to find how to create a csv file and append data to the file. I assume this would be done by creating an array and then writing the array to the file. I hate having to ask as I always try to work things out for myself, but assistance would be greatly appreciated.
Many thanks
Rob
Hi guys, I've searched high and low through the documentation and am struggling to find how to create a csv file and append data to the file. I assume this would be done by creating an array and then writing the array to the file. I hate having to ask as I always try to work things out for myself, but assistance would be greatly appreciated.
Many thanks
Rob

Quote from Vadim on November 10, 2022, 6:56 pm@rob-maggs
Good time! A CSV file is a simple text file whose data mimic a table. This means that each line of the file is a single table row, and the values of the table fields (columns) are separated by commas (or tabulation mark, or other delimiter). You can write data to such a file like to any other text file (e.g. .txt).
An example of the contents of such a file with a table of three rows and three columns:
Year, Name, City
1975, Vadim, Tyumen
1965, Andrey, Ivanovo
Good time! A CSV file is a simple text file whose data mimic a table. This means that each line of the file is a single table row, and the values of the table fields (columns) are separated by commas (or tabulation mark, or other delimiter). You can write data to such a file like to any other text file (e.g. .txt).
An example of the contents of such a file with a table of three rows and three columns:
Year, Name, City
1975, Vadim, Tyumen
1965, Andrey, Ivanovo

Quote from Rob Maggs on November 10, 2022, 8:19 pmThanks for the reply @vadim, I understand what a CSV file is, I just want to know how I can take a line of data and write it to a text file, and then append more data sequentially. So for example I create a string as a variable, write that variable to a text file...and then append lines, sorry maybe I did not descrive what I'm trying to do very well :)
Thanks for the reply @vadim, I understand what a CSV file is, I just want to know how I can take a line of data and write it to a text file, and then append more data sequentially. So for example I create a string as a variable, write that variable to a text file...and then append lines, sorry maybe I did not descrive what I'm trying to do very well :)

Quote from Vadim on November 10, 2022, 8:26 pm@rob-maggs
The general idea is this (an example of writing one string with three variables):
SetVar "[Year]" "1975"
SetVar "[Name]" "Vadim"
SetVar "[City]" "Tyumen"
FileWrite "[PubDir]Test.csv" "Append" "[Year],[Name],[City]"Or so:
SetVar "[var]" "1975, Vadim, Tyumen"
FileWrite "[PubDir]Test.csv" "Append" "[var]"
The general idea is this (an example of writing one string with three variables):
SetVar "[Year]" "1975"
SetVar "[Name]" "Vadim"
SetVar "[City]" "Tyumen"
FileWrite "[PubDir]Test.csv" "Append" "[Year],[Name],[City]"
Or so:
SetVar "[var]" "1975, Vadim, Tyumen"
FileWrite "[PubDir]Test.csv" "Append" "[var]"




Quote from Rob Maggs on November 11, 2022, 7:57 pmHey guys, @vadim any update on this or link to a resource that will help me, many thanks. I see I can write an initial variable to file by using:
VarToLocalFile [comment] "comment.txt"But how can I append sequential variables to the file created?
Many thanks Rob
Hey guys, @vadim any update on this or link to a resource that will help me, many thanks. I see I can write an initial variable to file by using:
VarToLocalFile [comment] "comment.txt"
But how can I append sequential variables to the file created?
Many thanks Rob

Quote from Darbdenral on November 12, 2022, 12:39 am@rob-maggs,
There's more then one way to do this..
I wasn't sure if you mean update values or append the whole new record.. so
Here's a few samples I believe would work.
First a simple update that initially changes the first value. Then we expand upon this to change all values too. Then add whole new line to CSV. I think you can see how to add new values now?
Simple approach updating values:
. could be variables here SetVar [csv] "ITEM1,ITEM2,ITEM3,ITEM4" . add item SetVar [csv] "[csv],ITEM5" CreateEmptyArray [csvArr] StrParse "[csv]" "," [csvArr] ArrayLen [csvArr] [totalRecords] If [totalrecords] != 0 jsAlert "[csvArr]" .now save your CSV to disk VarToLocalFile [csvArr] "myfile.txt" Else AlertBox "ERROR: " "No Records!" "" EndIf
Expanded approach updating values:
. could be variables here SetVar [csv] "ITEM1,ITEM2,ITEM3,ITEM4" CreateEmptyArray [csvArr] StrParse "[csv]" "," [csvArr] jsAlert "CSV = [csvArr(0)], [csvArr(1)], [csvArr(2)], [csvArr(3)]" ArrayLen [csvArr] [totalRecords] If [totalrecords] != 0 .change specific value SetVar [csvArr(0)] "NEW VALUE{0}" jsAlert "CSV = [csvArr(0)], [csvArr(1)], [csvArr(2)], [csvArr(3)]" Loop 0 [totalRecords -1] [num] If [num] = 0 .change specific value .SetVar [csvArr(0)] "NEW VALUE{[num]}" .jsAlert "CSV = [csvArr(0)], [csvArr(1)], [csvArr(2)], [csvArr(3)]" EndIf .change all values SetVar [csvArr([num])] "NEW VALUE{[num]}" EndLoop jsAlert "CSV = [csvArr(0)], [csvArr(1)], [csvArr(2)], [csvArr(3)]" .now save your CSV to disk VarToLocalFile [csvArr] "myfile.txt" Else AlertBox "ERROR: " "No Records!" "" EndIfAppend a new record and save to file:
. could be variables here SetVar [csv] "ITEM1,ITEM2,ITEM3,ITEM4" . add new record SetVar [csv_new] "[csv]\nITEM5,ITEM6,ITEM7,ITEM8" CreateEmptyArray [csvArr] StrParse "[csv_new]" "," [csvArr] ArrayLen [csvArr] [totalRecords] If [totalrecords] != 0 jsAlert "[csvArr]" .now save your CSV to disk VarToLocalFile [csvArr] "myfile.txt" Else AlertBox "ERROR: " "No Records!" "" EndIf
There's more then one way to do this..
I wasn't sure if you mean update values or append the whole new record.. so
Here's a few samples I believe would work.
First a simple update that initially changes the first value. Then we expand upon this to change all values too. Then add whole new line to CSV. I think you can see how to add new values now?
Simple approach updating values:
. could be variables here SetVar [csv] "ITEM1,ITEM2,ITEM3,ITEM4" . add item SetVar [csv] "[csv],ITEM5" CreateEmptyArray [csvArr] StrParse "[csv]" "," [csvArr] ArrayLen [csvArr] [totalRecords] If [totalrecords] != 0 jsAlert "[csvArr]" .now save your CSV to disk VarToLocalFile [csvArr] "myfile.txt" Else AlertBox "ERROR: " "No Records!" "" EndIf
Expanded approach updating values:
. could be variables here
SetVar [csv] "ITEM1,ITEM2,ITEM3,ITEM4"
CreateEmptyArray [csvArr]
StrParse "[csv]" "," [csvArr]
jsAlert "CSV = [csvArr(0)], [csvArr(1)], [csvArr(2)], [csvArr(3)]"
ArrayLen [csvArr] [totalRecords]
If [totalrecords] != 0
.change specific value
SetVar [csvArr(0)] "NEW VALUE{0}"
jsAlert "CSV = [csvArr(0)], [csvArr(1)], [csvArr(2)], [csvArr(3)]"
Loop 0 [totalRecords -1] [num]
If [num] = 0
.change specific value
.SetVar [csvArr(0)] "NEW VALUE{[num]}"
.jsAlert "CSV = [csvArr(0)], [csvArr(1)], [csvArr(2)], [csvArr(3)]"
EndIf
.change all values
SetVar [csvArr([num])] "NEW VALUE{[num]}"
EndLoop
jsAlert "CSV = [csvArr(0)], [csvArr(1)], [csvArr(2)], [csvArr(3)]"
.now save your CSV to disk
VarToLocalFile [csvArr] "myfile.txt"
Else
AlertBox "ERROR: " "No Records!" ""
EndIf
Append a new record and save to file:
. could be variables here SetVar [csv] "ITEM1,ITEM2,ITEM3,ITEM4" . add new record SetVar [csv_new] "[csv]\nITEM5,ITEM6,ITEM7,ITEM8" CreateEmptyArray [csvArr] StrParse "[csv_new]" "," [csvArr] ArrayLen [csvArr] [totalRecords] If [totalrecords] != 0 jsAlert "[csvArr]" .now save your CSV to disk VarToLocalFile [csvArr] "myfile.txt" Else AlertBox "ERROR: " "No Records!" "" EndIf

Quote from fkapnist on November 12, 2022, 2:41 am@rob-maggs
Here is a sample pub that might help you.
Load File. Append File. Save File.
Then Load File again to see the changes...
Here is a sample pub that might help you.
Load File. Append File. Save File.
Then Load File again to see the changes...

