Jump to content

listview to csv


Recommended Posts

I have been attempting to learn how to move data from a listview to CSV.

Came up with the below code BEFORE I found guiness's code (function).

Since I need to discover the how's and why's of coding could someone please look at mine and tell me why it returns a blank file??   The listview has "n" rows but only three columns.  Errors are screened at the listview control.  I actual have another similar code in which I used FileWriteLine  along with other coding, so I thought I would try a different approach thus the below one.

I would appreciate it very much.

Thanks.

Local $hFileOpen = FileOpen ("c:\documents\saveT sample" &".CSV",1)
local $savecount = _GUICtrlListView_GetItemCount ( $list1 )
for $i = 0 to $savecount  -1
    for $j = 0 to 2
$temp  = _GUICtrlListView_GetItemText (  $list1, $i,$j)
if $j <2 Then
    $temp = $temp&","
EndIf
Next
MsgBox($MB_SYSTEMMODAL, "Information", $temp )

$temp = $temp& @crlf
 Next

 FILEWRITE($hFileOpen, $temp )
  FileClose ($hFileOpen)
Edited by Hobbyist
Link to comment
Share on other sites

You're overwriting the contents of $temp each time you go through the inner loop, and then not writing it until you've finished the outer loop. So the contents of $temp is only going to have the last item from the listview.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

BrewManNH

So then should it really be $temp($i) in order to preserve each value of $temp???? 

I guess I don't see how to chain them together, so I write item(0),item(1),item(2) as a line of record.

Thanks for the quick response.

Link to comment
Share on other sites

Here's a corrected version of your script, see the comments to see if that's what you're attempting.

Local $hFileOpen = FileOpen("c:\documents\saveT sample.CSV", 1)
Local $savecount = _GUICtrlListView_GetItemCount($list1)
$temp = "" ; set $temp to an empty string
For $i = 0 To $savecount - 1
    For $j = 0 To 2
        $temp &= _GUICtrlListView_GetItemText($list1, $i, $j) ; add the new text to the end of the $temp string
        If $j < 2 Then
            $temp &= "," ; add a comma to the end of the $temp string if $j < 2
        EndIf
    Next
    MsgBox($MB_SYSTEMMODAL, "Information", $temp) ; display what $temp looks like in the message box
    $temp &= @CRLF ; add a CRLF to the end of the $temp string
Next

FileWrite($hFileOpen, $temp) ; write the contents of the $temp variable to the file
FileClose($hFileOpen)

If this is what you're attempting here's another way of doing it as well, it saves a few lines.

Local $hFileOpen = FileOpen("c:\documents\saveT sample.CSV", 1)
Local $savecount = _GUICtrlListView_GetItemCount($list1)
Global $temp
For $i = 0 To $savecount - 1
    For $j = 0 To 2
        $temp = _GUICtrlListView_GetItemText($list1, $i, $j) ; get the text and put it into $temp, this erases the old $temp contents and adds new content
        If $j < 2 Then
            $temp &= "," ; add a comma to the end of the $temp string if $j < 2
        EndIf
    Next
    MsgBox($MB_SYSTEMMODAL, "Information", $temp) ; display what $temp looks like in the message box
    FileWriteLine($hFileOpen, $temp) ; write $temp to the file, automatically adds CRLF to the end of the written line
Next

FileClose($hFileOpen)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

BrewManNH

AW - much more clear to me now.  Thanks for straightening me out.

And your documented comments are beneficial to a noo bee trying to understand the whys/hows

My big take aways are:

1. better understanding of loops (and consequences)

2. now know the power of the operator &=

Thanks 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...