Output ListIconGadget to .csv

Share your advanced PureBasic knowledge/code with the community.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Output ListIconGadget to .csv

Post by Rook Zimbabwe »

Here is the procedure I use to output a ListIconGadget() to a .csv file.

Code: Select all

Procedure ExporttoCSV()
  ;*
  yy$ = FormatDate("%yy%mm%dd",Date()) ; I use this for the output filename
  by$ = GetGadgetText(#Text_REPORT) ; this variable holds my report name portion - point to your name for this file
  FileName$ = yy$+by$+".csv" ; add those two variables and add a .csv for comma saved
    StandardFile$ = "C:\My Documents\"+Filename$+""  ; set initial file+path to display in filerequester
      Pattern$ = "CSV (*.csv)|*.csv|Text (*.txt)|*.txt|All files (*.*)|*.*"
        Pattern = 0    
  File$ = SaveFileRequester("Please choose Where to SAVE...", StandardFile$, Pattern$, Pattern)
  If File$
    MessageRequester("Information", "You have saved following file:" + Chr(10) + File$, 0) ; those running unicode may have to use #CRLF (I think)
  Else
    MessageRequester("Information", "The requester was canceled.", 0) 
  EndIf

  OpenFile(#File, File$) ; you will have to declare a #File
  
  Result = CountGadgetItems(#ListIcon_BIG) ; name of my ListIcon() thingee
  For cc = 0 To COLFLAG - 1
    tit$ = GetGadgetItemText(#ListICon_BIG, -1,cc) ; get text of column headers
      tit$ = tit$ +","
      title$ = title$ + tit$
        tit$ = ""
  Next
  WriteStringN(#File, title$) ; write the title string
  title$ = ""
  For lin = 0 To Result - 1 ; number of lines
     For col = 0 To COLFLAG - 1 ; number of columns
      plink$ = GetGadgetItemText(#ListIcon_BIG,lin,col) ; get that LOCATION and put it in plink$
      plink$ = plink$+"," ; add a comma (this is a CSV file)
        plonk$ = plonk$ + plink$ ; add plink$ to plonk$
        Debug "PLoNK: "+plonk$
        ; plink$ = "" ; kill value in plink$
          If col = COLFLAG ; EOL reached
            cut = Len(plonk$)  - 1
              plonk$ = Left(plonk$,cut) ; cut off the last ","
          EndIf
       Next
       WriteStringN(#File, plonk$) ; write the entire plonk$
       plonk$ = "" ; kill value in plonk$
    Next
   CloseFile(#File) 
EndProcedure
Documenting now... NOW commented version... 8)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Post by Baldrick »

Very similar to a proc I have recently used in a little project of mine.
Might suggest maybe though that you might be better off to use CreateFile() instead of OpenFile(). This way there is no chance of a user writing a double lot of data to the same file. Also, you might look at your StandardFile$ = "C:\My Documents\"+Filename$+"" & maybe lose the "C:" reference as not every1 will neccessarily be using C drive as the root.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

I have something similar too :)

Couple of points you might consider:

1. Use quotes as some fields might have commas in them and this can create problems for the output, a no-brain method is just quote everything

2. For large exports (several thousand lines + ) this can get very slow due to the string catting, if you write into a large buffer or lines into an array or something you can get dramatic performance increases.
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

:D
Lets start backwards:
1. Use quotes as some fields might have commas in them
Yes, and some fields could have " in them too... If you were going to output as CSV you would need to organize your data a bit. 8)

I hadn't considered the array idea though... nice one. I will let the user code that up. Lazy me!

C
reateFile() instead of OpenFile().
That is my oops, not read the procedure I cut out character by character... I create the file elsewhere... Oy! Being stupid again. :oops:
StandardFile$ = "C:\My Documents\"+Filename$+""
This is actually just a default for the File Requester Gadget... User can click anywhere... OR the programmer can code up a custom location really easy by editing the string. 8)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply