Jump to content

Reading from ini's and using the results later


Recommended Posts

I created a program with some hard coded values, but would like to be able to update an .ini file in the future. I basically want to have the use choose what kind of computer he is working with. I created a For loop to try to pull out all of the different models from the ini file, but it stops at the second one (Device1). So my first problem is having the loop go all the way to the end, no matter how many computers are in the list. My next problem is assigning some kind of value (the GHO= in the ini file) so when the user hits "OK" it takes that value and does it's function. Now I have it looking at the control.

My ini code is below:

[Device0]
Type=Laptop 1
GHO=ABC123
[Device1]
Type=Laptop 2
GHO=ABC234
[Device2]
Type=Desktop 1
GHO=XYZ123
[Device3]
Type=Desktop 2
GHO=XYZ234

The ini loop code is below (this stops after the 2nd computer):

;Create the list of servers
$deviceGroup = GUICtrlCreateGroup("Devices", 210, 10, 190, 210)
$deviceList = GUICtrlCreateListView("Select a Device", 230, 30, 147, 161)
;Run through the ini file
For $m = 0 To IniReadSection("x.ini", "Device")
    $devType = IniRead("x.ini", "Device" & $m, "Type", "NotFound")
    GUICtrlCreateListViewItem("" & $devType, $deviceList)
Next

This is the code now that looks at each individual option that may be selected and hard codes the value to use. I want to replace this with something that just grabs the value of what is selected in the list and go from there:

Case $msg = $deviceSelButton
  Select
    Case GUICtrlRead($deviceList) = 15
      $setDeviceType = "Laptop 1"
      $setDevice = "ABC123"
      MsgBox(0, "Device Selected", "Device Selected:" & @CRLF & $setDeviceType)
    Case GUICtrlRead($deviceList) = 16
      $setDeviceType = "Laptop 2"
      $setDevice = "ABC234"
      MsgBox(0, "Device Selected", "Device Selected:" & @CRLF & $setDeviceType)
    Case GUICtrlRead($deviceList) = 17
      $setDeviceType = "Desktop 1"
      $setDevice = "XYZ123"
      MsgBox(0, "Device Selected", "Device Selected:" & @CRLF & $setDeviceType)
    Case GUICtrlRead($deviceList) = 18
      $setDeviceType = "Desktop 2"
      $setDevice = "XYZ234"
      MsgBox(0, "Device Selected", "Device Selected:" & @CRLF & $setDeviceType)
    Case Else
      MsgBox(0, "No Device", "Please select a device")
  EndSelect
Link to comment
Share on other sites

Just use IniRead() and save the return to a variable, then use it whenever or however you like

sorry that may have been a bit of an over simplification.... instead of looping your iniread's, you could create an array that stores all of the values, then loop through the array to populate your controls if you want, or just access the individual values in the array as needed
Link to comment
Share on other sites

See Help: IniReadSection

"Success: Returns a 2 dimensional array where element[n][0] is the key and element[n][1] is the value. "

In other words your

For $m = 0 To IniReadSection("x.ini", "Device")

is like

For $m = 0 To 1 Because "Device" does not exists. You only got "DeviceX" and X is a number....

What the other posted was like this:

#include <Array.au3>


$Sections_Names=IniReadSectionNames ("x.ini" )
_ArrayDisplay($Sections_Names)


    For $i = 0 To $Sections_Names[0]
        $Section_Vals = IniReadSection("x.ini", $Sections_Names[$i])
         _ArrayDisplay($Section_Vals)
    Next

_ArrayDisplay is just used to make the purpose of the two read functions clear.

And if you use the reference of the selected index and the array you will also get the referring ini section.....

Edited by Tankbuster
Link to comment
Share on other sites

@Tankbuster, I figured that out last night and changed the ini to work better. The ini file now looks like this:

[Devices]
Computer1=Laptop 1|ABC123
Computer2=Laptop 2|ABC234
Computer3=Desktop 1|XYZ123
Computer4=Desktop 2|XYZ234

I now get all four on my list, but as soon as I select one, I get my message that the first model was selected. It always says the first one was selected no matter which one I click on. Also, I don't have any code to display the message box when I click on the list. So something else is going on.

My revised code for making the list is below:

;Run through the ini file
Dim $var = IniReadSection("x.ini", "Devices")
Local $deviceListItem[20]
If @error Then
    MsgBox(4096, "", "An error has occurred loading the INI file.")
Else
    For $i = 1 To $var[0][0]
        $deviceListItem[$i] = GUICtrlCreateListViewItem("" & $var[$i][1], $deviceList)
    Next
EndIf

My other question is how can I read what is actually selected in a list, so I can check the array against what is selected for the ABC123 value instead of the name.

Link to comment
Share on other sites

Could you please post your complete script?

because in your question you refer also to other parts ("I click on...").

Even if I'm not able to post you the answer, others will do.

But reading a complete script is showing "us" your "fault".

It's not to say "haha, what a dumb a...", no in fact you will get a fast solution.

Or if your script is already too long, strip it down to a running version with your problem inside.

//edit:

In the meantime I tried to understand what you are searching for.....

I extended the listview example with your ini read.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#include <Array.au3>
Opt('MustDeclareVars', 1)

    dim $deviceList, $button, $item1, $item2, $item3, $input1, $msg
    dim $deviceListItem[20]
    Dim $var
    GUICreate("listview items", 420, 350, 100, 100, -1, $WS_EX_ACCEPTFILES)
    GUISetBkColor(0x00E0FFFF)  ; will change background color

    $deviceList = GUICtrlCreateListView("INI line| PCName  |col2", 10, 10, 300, 200);,$LVS_SORTDESCENDING)
    $button = GUICtrlCreateButton("Value?", 75, 270, 170, 20)
    GUISetState()
    GetIni()
    Do
        $msg = GUIGetMsg()

        Select
            Case $msg = $button
;~                 MsgBox(0, "listview item", GUICtrlRead(GUICtrlRead($deviceList)), 2)
                GetComputerName( GUICtrlRead(GUICtrlRead($deviceList)))
            Case $msg = $deviceList
                MsgBox(0, "listview", "clicked=" & GUICtrlGetState($deviceList), 2)
        EndSelect
    Until $msg = $GUI_EVENT_CLOSE


func GetIni()
    ;Run through the ini file
 $var = IniReadSection("x.ini", "Devices")

If @error Then
    MsgBox(4096, "", "An error has occurred loading the INI file.")
Else
    For $i = 1 To $var[0][0]
        $deviceListItem[$i] = GUICtrlCreateListViewItem($i&"|" & $var[$i][1], $deviceList)
    Next
EndIf

    EndFunc

func GetComputerName($ListViewSelection)
    local $Fields=StringSplit($ListViewSelection,"|")
    msgbox(0,"PC Name","PCNAME"&@TAB&$var[$Fields[1]][0]&@CRLF&"Values:"&@TAB&$ListViewSelection)
    EndFunc

As you wrote about GHO I think you try to build up a GHOST utility to download a special GHOst file for a model to a PC based on the name? My code is just a guess what you trying to do....

Edited by Tankbuster
Link to comment
Share on other sites

Could you please post your complete script?

Here's my code and I put your stuff into it. I'm trying to launch Ghost from within a WinPE by passing the file name which will be set when the computer type is selected. When I try to run it, it errors out with the following error message:

Subscript used with non-Array variable.:

$deviceListItem[$i] = GUICtrlCreateListViewItem($i&"|" & $var[$i][1], $deviceList)

$deviceListItem^ ERROR

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

Opt('MustDeclareVars', 1)
    
    ;Device items
    Global $setDevice, $setDeviceType, $deviceSelButton, $deviceGroup, $deviceList, $deviceListItem

_Main()

Func _Main()
    ;Main items
    Local $filemenu, $fileitem, $recentfilesmenu, $separator1
    Local $exititem, $helpmenu, $aboutitem, $okbutton, $cancelbutton
    Local $msg, $file, $GUI_EVENT_CLOSE
    #forceref $separator1
    
    ;Server items
    Local $ghost, $setServer, $mapDrvButton, $serverGroup, $serverList
    Local $serverListItem1, $serverListItem2, $serverListItem3, $serverListItem4, $serverListItem5, $serverListItem6, $serverListItem7
    
    ;Drive Mapping
    Local $mapped, $userName, $password, $err, $success, $repeat, $repeatGhost, $errorList, $answer, $startGhost, $ghosting

    GUICreate("Ghosting Utility", 410, 300, (@DesktopWidth - 410) / 2, (@DesktopHeight - 300) / 2, -1, $WS_EX_ACCEPTFILES)
    
    ;Create the list of servers
    $serverGroup = GUICtrlCreateGroup("Servers", 10, 10, 190, 210)
    $serverList = GUICtrlCreateListView("Select a Server              ", 30, 30, 147, 161)
    $serverListItem1 = GUICtrlCreateListViewItem("Server 1", $serverList); #5
    $serverListItem2 = GUICtrlCreateListViewItem("Server 2", $serverList); #6
    $serverListItem3 = GUICtrlCreateListViewItem("Server 3", $serverList); #7
    $serverListItem4 = GUICtrlCreateListViewItem("Server 4", $serverList); #8
    $serverListItem5 = GUICtrlCreateListViewItem("Server 5", $serverList); #9
    $serverListItem6 = GUICtrlCreateListViewItem("Server 6", $serverList); #10
    $serverListItem7 = GUICtrlCreateListViewItem("Custom Server", $serverList); #11
    $mapDrvButton = GUICtrlCreateButton("&Map Drive", 30, 195, 90, 20)
    
    ;Create the list of servers
    $deviceGroup = GUICtrlCreateGroup("Devices", 210, 10, 190, 210)
    $deviceList = GUICtrlCreateListView("Select a Device             ", 230, 30, 147, 161)
    
    ;INI file items
    GUISetState()
    GetIni()
    Do
        $msg = GUIGetMsg()

        Select
            Case $msg = $button
;~                 MsgBox(0, "listview item", GUICtrlRead(GUICtrlRead($deviceList)), 2)
                GetComputerName(GUICtrlRead(GUICtrlRead($deviceList)))
            Case $msg = $deviceList
                MsgBox(0, "listview", "clicked=" & GUICtrlGetState($deviceList), 2)
        EndSelect
    Until $msg = $GUI_EVENT_CLOSE
    
    $deviceSelButton = GUICtrlCreateButton("Select &Device", 230, 195, 90, 20)
    
    $filemenu = GUICtrlCreateMenu("&File")
    ;$separator1 = GUICtrlCreateMenuItem("", $filemenu)
    $exititem = GUICtrlCreateMenuItem("E&xit", $filemenu)
    $helpmenu = GUICtrlCreateMenu("&Help")
    $aboutitem = GUICtrlCreateMenuItem("&About", $helpmenu)
    

    $okbutton = GUICtrlCreateButton("&OK", 70, 250, 70, 20)

    $cancelbutton = GUICtrlCreateButton("&Cancel", 270, 250, 70, 20)

    GUISetState()

    While 1
        $msg = GUIGetMsg()

        Select
            Case $msg = $cancelbutton; Or $msg = $GUI_EVENT_CLOSE
                ExitLoop

            Case $msg = $exititem
                ExitLoop

            Case $msg = $okbutton
                $repeatGhost = 1
                $startGhost = 1
                While $repeatGhost = 1
                    If $setServer = "" Then
                        MsgBox(0, "No Server Set", "Select a server to begin ghosting")
                        $repeatGhost = 0
                    EndIf
                    If $setDevice = "" Then
                        MsgBox(0, "No Device Selected", "Select a device to begin ghosting")
                        $repeatGhost = 0
                    EndIf
                    If $setServer <> "" And $setDevice <> "" And $startGhost <> 0 Then
                        $ghosting = RunWait("J:\Ghost80\Ghost32\Ghost32.exe -NTIL -Clone,mode=load,src=J:\WXP-Prod\W632\" & $setDevice & ".GHO,dst=1 -batch")
                        $startGhost = 0
                        $repeatGhost = 0
                        Run("Wpeutil reboot", "x:")
                    EndIf
                WEnd

            Case $msg = $aboutitem
                MsgBox(0, "About", "Version 0.6.0" & @CRLF & "Developed by Me")
            
            Case $msg = $mapDrvButton
                Select
                    Case GUICtrlRead($serverlist) = 5
                        $setServer = "\\Server1\ghostimg"
                    Case GUICtrlRead($serverlist) = 6
                        $setServer = "\\Server2\ghostimg"
                    Case GUICtrlRead($serverlist) = 7
                        $setServer = "\\Server3\ghostimg"
                    Case GUICtrlRead($serverlist) = 8
                        $setServer = "\\Server4\ghostimg"
                    Case GUICtrlRead($serverlist) = 9
                        $setServer = "\\Server5\ghostimg"
                    Case GUICtrlRead($serverlist) = 10
                        $setServer = "\\Server6\ghostimg"
                    Case GUICtrlRead($serverlist) = 11
                        While $setServer = ""
                            $setServer = InputBox("Custom Server", "Custom Server Address:")
                        WEnd
                EndSelect
                $repeat = 1
                $success = 1
                While $repeat = 1
                    If DriveMapGet("j:") <> $setServer Then
                        DriveMapDel("j:")
                        sleep(250)
                        $mapped = DriveMapAdd("j:", $setServer, 8)
                        $errorList = " Undefined / Other error|Access to the remote share was denied|The device is already assigned|Invalid device name|Invalid remote share|Invalid password"
                        $errorList = StringSplit($errorList, "|");make an array for error code lookup
                        If $mapped Then
                            MsgBox(0, "Drive Mapped", "Drive mapped to " & @CRLF & $setServer)
                            $repeat = 0
                            ;$setServer = ""
                        ElseIf Not $mapped then
                            $success = 0
                            If $err = 6 or $err = 2 Then
                                $answer = MsgBox(6,"Error", "Username or password is invalid... Please try again.")
                                If $answer <> $IDTRYAGAIN Then $repeat = 0
                            Else
                                $answer = MsgBox(4096,"Error", "Error code: " & $errorList[$err])
                                If $answer <> $IDTRYAGAIN Then $repeat = 0
                            EndIf
                        EndIf
                    Else
                        MsgBox(0, "Already Mapped", "The drive is already mapped to the selected server")
                        $repeat = 0
                    EndIf
                WEnd
                    
            Case $msg = $deviceSelButton
                Select
                    Case GUICtrlRead($deviceList) = 15
                        $setDeviceType = "Laptop 1"
                        $setDevice = "ABC123"
                    Case GUICtrlRead($deviceList) = 16
                        $setDeviceType = "Laptop 2"
                        $setDevice = "ABC234"
                    Case GUICtrlRead($deviceList) = 17
                        $setDeviceType = "Desktop 1"
                        $setDevice = "XYZ123"
                    Case GUICtrlRead($deviceList) = 18
                        $setDeviceType = "Desktop 2"
                        $setDevice = "XYZ234"
                    Case Else
                        MsgBox(0, "No Device", "Please select a device")
                        ;MsgBox(0, "Device Selected", "Device Selected:" & @CRLF & $setDeviceType)
                        
                EndSelect
        EndSelect
    WEnd

    GUIDelete()
    
    Exit
EndFunc   ;==>_Main

func GetIni()
    ;Run through the ini file
    Dim $var = IniReadSection("x.ini", "Devices")

If @error Then
    MsgBox(4096, "", "An error has occurred loading the INI file.")
Else
    For $i = 1 To $var[0][0]
        $deviceListItem[$i] = GUICtrlCreateListViewItem($i&"|" & $var[$i][1], $deviceList)
    Next
EndIf

EndFunc

func GetComputerName($ListViewSelection)
    local $Fields=StringSplit($ListViewSelection,"|")
    msgbox(0,"PC Name","PCNAME"&@TAB&$var[$Fields[1]][0]&@CRLF&"Values:"&@TAB&$ListViewSelection)
EndFunc
Edited by BlackHoleSun
Link to comment
Share on other sites

Ok, I had not much time to look to your code, but the first error is a simple one.

You simply forgot to declare the array $deviceListItem, $button, $var also.

i just added them on top with $$deviceListItem[10]. 10 was just a guess and not dynamic, just to start.

But did not look further, but maybe you are able to continue in the meantime?

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

Opt('MustDeclareVars', 1)

    ;Device items
    Global $setDevice, $setDeviceType, $deviceSelButton, $deviceGroup, $deviceList,$button,$var
    Dim $deviceListItem[10]

_Main()

Func _Main()
    ;Main items
    Local $filemenu, $fileitem, $recentfilesmenu, $separator1
    Local $exititem, $helpmenu, $aboutitem, $okbutton, $cancelbutton
    Local $msg, $file, $GUI_EVENT_CLOSE
    #forceref $separator1

    ;Server items
    Local $ghost, $setServer, $mapDrvButton, $serverGroup, $serverList
    Local $serverListItem1, $serverListItem2, $serverListItem3, $serverListItem4, $serverListItem5, $serverListItem6, $serverListItem7

    ;Drive Mapping
    Local $mapped, $userName, $password, $err, $success, $repeat, $repeatGhost, $errorList, $answer, $startGhost, $ghosting

    GUICreate("Auto Ghosting Utility", 410, 300, (@DesktopWidth - 410) / 2, (@DesktopHeight - 300) / 2, -1, $WS_EX_ACCEPTFILES)

    ;Create the list of servers
    $serverGroup = GUICtrlCreateGroup("Servers", 10, 10, 190, 210)
    $serverList = GUICtrlCreateListView("Select a Server              ", 30, 30, 147, 161)
    $serverListItem1 = GUICtrlCreateListViewItem("Server 1", $serverList); #5
    $serverListItem2 = GUICtrlCreateListViewItem("Server 2", $serverList); #6
    $serverListItem3 = GUICtrlCreateListViewItem("Server 3", $serverList); #7
    $serverListItem4 = GUICtrlCreateListViewItem("Server 4", $serverList); #8
    $serverListItem5 = GUICtrlCreateListViewItem("Server 5", $serverList); #9
    $serverListItem6 = GUICtrlCreateListViewItem("Server 6", $serverList); #10
    $serverListItem7 = GUICtrlCreateListViewItem("Custom Server", $serverList); #11
    $mapDrvButton = GUICtrlCreateButton("&Map Drive", 30, 195, 90, 20)

    ;Create the list of servers
    $deviceGroup = GUICtrlCreateGroup("Devices", 210, 10, 190, 210)
    $deviceList = GUICtrlCreateListView("Select a Device             ", 230, 30, 147, 161)

    ;INI file items
    GUISetState()
    GetIni()
    Do
        $msg = GUIGetMsg()

        Select
            Case $msg = $button
;~                 MsgBox(0, "listview item", GUICtrlRead(GUICtrlRead($deviceList)), 2)
                GetComputerName(GUICtrlRead(GUICtrlRead($deviceList)))
            Case $msg = $deviceList
                MsgBox(0, "listview", "clicked=" & GUICtrlGetState($deviceList), 2)
        EndSelect
    Until $msg = $GUI_EVENT_CLOSE

    $deviceSelButton = GUICtrlCreateButton("Select &Device", 230, 195, 90, 20)

    $filemenu = GUICtrlCreateMenu("&File")
    ;$separator1 = GUICtrlCreateMenuItem("", $filemenu)
    $exititem = GUICtrlCreateMenuItem("E&xit", $filemenu)
    $helpmenu = GUICtrlCreateMenu("&Help")
    $aboutitem = GUICtrlCreateMenuItem("&About", $helpmenu)


    $okbutton = GUICtrlCreateButton("&OK", 70, 250, 70, 20)

    $cancelbutton = GUICtrlCreateButton("&Cancel", 270, 250, 70, 20)

    GUISetState()

    While 1
        $msg = GUIGetMsg()

        Select
            Case $msg = $cancelbutton; Or $msg = $GUI_EVENT_CLOSE
                ExitLoop

            Case $msg = $exititem
                ExitLoop

            Case $msg = $okbutton
                $repeatGhost = 1
                $startGhost = 1
                While $repeatGhost = 1
                    If $setServer = "" Then
                        MsgBox(0, "No Server Set", "Select a server to begin ghosting")
                        $repeatGhost = 0
                    EndIf
                    If $setDevice = "" Then
                        MsgBox(0, "No Device Selected", "Select a device to begin ghosting")
                        $repeatGhost = 0
                    EndIf
                    If $setServer <> "" And $setDevice <> "" And $startGhost <> 0 Then
                        $ghosting = RunWait("J:\Ghost80\Ghost32\Ghost32.exe -NTIL -Clone,mode=load,src=J:\WXP-Prod\W632\" & $setDevice & ".GHO,dst=1 -batch")
                        $startGhost = 0
                        $repeatGhost = 0
                        Run("Wpeutil reboot", "x:")
                    EndIf
                WEnd

            Case $msg = $aboutitem
                MsgBox(0, "About", "Version 0.6.0" & @CRLF & "Developed by Me")

            Case $msg = $mapDrvButton
                Select
                    Case GUICtrlRead($serverlist) = 5
                        $setServer = "\\Server1\ghostimg"
                    Case GUICtrlRead($serverlist) = 6
                        $setServer = "\\Server2\ghostimg"
                    Case GUICtrlRead($serverlist) = 7
                        $setServer = "\\Server3\ghostimg"
                    Case GUICtrlRead($serverlist) = 8
                        $setServer = "\\Server4\ghostimg"
                    Case GUICtrlRead($serverlist) = 9
                        $setServer = "\\Server5\ghostimg"
                    Case GUICtrlRead($serverlist) = 10
                        $setServer = "\\Server6\ghostimg"
                    Case GUICtrlRead($serverlist) = 11
                        While $setServer = ""
                            $setServer = InputBox("Custom Server", "Custom Server Address:")
                        WEnd
                EndSelect
                $repeat = 1
                $success = 1
                While $repeat = 1
                    If DriveMapGet("j:") <> $setServer Then
                        DriveMapDel("j:")
                        sleep(250)
                        $mapped = DriveMapAdd("j:", $setServer, 8)
                        $errorList = " Undefined / Other error|Access to the remote share was denied|The device is already assigned|Invalid device name|Invalid remote share|Invalid password"
                        $errorList = StringSplit($errorList, "|");make an array for error code lookup
                        If $mapped Then
                            MsgBox(0, "Drive Mapped", "Drive mapped to " & @CRLF & $setServer)
                            $repeat = 0
                            ;$setServer = ""
                        ElseIf Not $mapped then
                            $success = 0
                            If $err = 6 or $err = 2 Then
                                $answer = MsgBox(6,"Error", "Username or password is invalid... Please try again.")
                                If $answer <> $IDTRYAGAIN Then $repeat = 0
                            Else
                                $answer = MsgBox(4096,"Error", "Error code: " & $errorList[$err])
                                If $answer <> $IDTRYAGAIN Then $repeat = 0
                            EndIf
                        EndIf
                    Else
                        MsgBox(0, "Already Mapped", "The drive is already mapped to the selected server")
                        $repeat = 0
                    EndIf
                WEnd

            Case $msg = $deviceSelButton
                Select
                    Case GUICtrlRead($deviceList) = 15
                        $setDeviceType = "Laptop 1"
                        $setDevice = "ABC123"
                    Case GUICtrlRead($deviceList) = 16
                        $setDeviceType = "Laptop 2"
                        $setDevice = "ABC234"
                    Case GUICtrlRead($deviceList) = 17
                        $setDeviceType = "Desktop 1"
                        $setDevice = "XYZ123"
                    Case GUICtrlRead($deviceList) = 18
                        $setDeviceType = "Desktop 2"
                        $setDevice = "XYZ234"
                    Case Else
                        MsgBox(0, "No Device", "Please select a device")
                        ;MsgBox(0, "Device Selected", "Device Selected:" & @CRLF & $setDeviceType)

                EndSelect
        EndSelect
    WEnd

    GUIDelete()

    Exit
EndFunc   ;==>_Main

func GetIni()
    ;Run through the ini file
    Dim $var = IniReadSection("x.ini", "Devices")

If @error Then
    MsgBox(4096, "", "An error has occurred loading the INI file.")
Else
    For $i = 1 To $var[0][0]
        $deviceListItem[$i] = GUICtrlCreateListViewItem($i&"|" & $var[$i][1], $deviceList)
    Next
EndIf

EndFunc

func GetComputerName($ListViewSelection)
    local $Fields=StringSplit($ListViewSelection,"|")
    msgbox(0,"PC Name","PCNAME"&@TAB&$var[$Fields[1]][0]&@CRLF&"Values:"&@TAB&$ListViewSelection)
EndFunc

BTW:

Maybe you shoudl search the forum for AutoIt3Wrapper

Compiling your script gave me in the console window of Scite:

+>16:03:57 Starting AutoIt3Wrapper v.2.0.1.24    Environment(Language:0407  Keyboard:00000407  OS:WIN_7/  CPU:X64 OS:X64)
>Running AU3Check (1.54.19.0)  from:C:\Program Files (x86)\AutoIt3
X:\trans\trans\fsdf - Kopie.au3(192,91) : WARNING: $deviceListItem possibly not declared/created yet
        $deviceListItem[$i] = GUICtrlCreateListViewItem($i&"|" & $var[$i][1], $deviceList)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
X:\trans\trans\fsdf - Kopie.au3 - 0 error(s), 1 warning(s)
->16:03:57 AU3Check ended.rc:1
>Running:(3.3.6.1):C:\Program Files (x86)\AutoIt3\autoit3.exe "X:\trans\trans\fsdf - Kopie.au3"    
X:\trans\trans\fsdf - Kopie.au3 (192) : ==> Expected a "=" operator in assignment statement.:
$deviceListItem[$i] = GUICtrlCreateListViewItem($i&"|" & $var[$i][1], $deviceList)
$deviceListItem^ ERROR
->16:03:58 AutoIT3.exe ended.rc:1
>Exit code: 1    Time: 1.651

You see the : "WARNING: $deviceListItem possibly not declared/created yet" - simple to solve if you get the message

Maybe this give you in the future also a better debug view?

Not a must, but I like this stuff.

Edited by Tankbuster
Link to comment
Share on other sites

So finaly I'm done with your script.....

uhhh, it looks like the code comes a long way from try and error. Hard to read, but I guess some of mine look he same before cleaning it up from time to time ;)

I hope I got it right, and understood your purpose of each sections:

So here is the X.INI first:

[Servers]
Server1=\\Server1\ghostimg
Server2=\\Server2\ghostimg
Server3=\\Server3\ghostimg
Custom=
[Devices]
Computer1=Laptop 1|ABC123
Computer2=Laptop 2|ABC234
Computer3=Desktop 1|XYZ123
Computer4=Desktop 2|XYZ234

As you can see, now also the servers are inside. Makes it easier for you to add and remove servers and paths.

The code of the script:

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

Opt('MustDeclareVars', 1)

    ;Device items
    Global $deviceGroup,$mapDrvButton, $serverGroup,$mapped
    Global $ServerList, $DeviceList
    Global $StatusMappedDrive
    Global $strServerPath
    global $DriveMapLetter="j:"
    Dim $arrDevice[10],$DeviceListItem[10]
    Dim $arrServer[10],$ServerListItem[10]
_Main()

Func _Main()
    ;Main items
    Local $filemenu
    Local $exititem, $helpmenu, $aboutitem, $okbutton, $cancelbutton
    Local $msg, $file
    local $SelectedListViewDeviceItem,$SelectedListViewServerItem


    GUICreate("Auto Ghosting Utility", 410, 300, (@DesktopWidth - 410) / 2, (@DesktopHeight - 300) / 2, -1, $WS_EX_ACCEPTFILES)

    ;Create the list of servers
    $serverGroup = GUICtrlCreateGroup("Servers", 10, 10, 190, 210)
    $serverList = GUICtrlCreateListView("No|Server|Server UNC Path", 30, 30, 147, 161)

    $mapDrvButton = GUICtrlCreateButton("&Map Drive", 30, 195, 90, 20)
    $StatusMappedDrive = GUICtrlCreateLabel("---", 140,195,100,20)

    ;Create the list of servers
    $deviceGroup = GUICtrlCreateGroup("Devices", 210, 10, 190, 210)
    $deviceList = GUICtrlCreateListView("No|Model|Ghost", 230, 30, 147, 161)

    $filemenu = GUICtrlCreateMenu("&File")
    $exititem = GUICtrlCreateMenuItem("E&xit", $filemenu)
    $helpmenu = GUICtrlCreateMenu("&Help")
    $aboutitem = GUICtrlCreateMenuItem("&About", $helpmenu)


    $okbutton = GUICtrlCreateButton("&OK", 70, 250, 70, 20)

    $cancelbutton = GUICtrlCreateButton("&Cancel", 270, 250, 70, 20)

    ;INI file items
    GetIni()
    CheckMappedDrive()

    GUISetState()

    while 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $cancelbutton; Or $msg = $GUI_EVENT_CLOSE
                ExitLoop

            Case $msg = $exititem
                ExitLoop

            Case $msg = $okbutton
                if CheckMappedDrive()=2 Then
                    MsgBox(0, "Drive Mapping", "Drive "&$DriveMapLetter&" is not mapped")
                Else
                    $SelectedListViewDeviceItem=GUICtrlRead(GUICtrlRead($deviceList))
                    if $SelectedListViewDeviceItem==0 Then
                        MsgBox(0, "Device Selection", "No device selected")
                    Else
                        consolewrite(":"&$SelectedListViewDeviceItem&":")
                         local $Fields=StringSplit($SelectedListViewDeviceItem,"|")
                        Ghosting($Fields[2],$Fields[3])
                    EndIf
                EndIf

            Case $msg = $aboutitem
                MsgBox(0, "About", "Version 0.6.0" & @CRLF & "Developed by Me")
            Case $msg = $mapDrvButton
                $SelectedListViewServerItem=GUICtrlRead(GUICtrlRead($serverList))

                if $SelectedListViewServerItem==0 Then
                        MsgBox(0, "Server Selection", "No server selected")
                    Else
                        MapDrive(GUICtrlRead(GUICtrlRead($serverList)))
                endif
           EndSelect
    WEnd

    GUIDelete()

    Exit
EndFunc   ;==>_Main

func Ghosting($SelectedDeviceType,$SelectedDevice)
    msgbox(0,"Ghosting","ghosting will start with"&@CRLF&$DriveMapLetter&"\Ghost80\Ghost32\Ghost32.exe -NTIL -Clone,mode=load,src=J:\WXP-Prod\W632\" & $SelectedDevice & ".GHO,dst=1 -batch")
    local $ghosting = RunWait($DriveMapLetter&"\Ghost80\Ghost32\Ghost32.exe -NTIL -Clone,mode=load,src=J:\WXP-Prod\W632\" & $SelectedDevice & ".GHO,dst=1 -batch")
    Run("Wpeutil reboot", "x:")
EndFunc
;~ =============================================================
func GetIni()
    $DeviceListItem = IniReadSection("x.ini", "Devices")
    $ServerListItem = IniReadSection("x.ini", "Servers")
        For $i = 1 To $DeviceListItem[0][0]
            $arrDevice[$i] = GUICtrlCreateListViewItem($i&"|" & $DeviceListItem[$i][1], $deviceList)
        Next
        For $i = 1 To $ServerListItem[0][0]
            $arrServer[$i] = GUICtrlCreateListViewItem($i&"|" & $ServerListItem[$i][0]&"|" & $ServerListItem[$i][1], $serverList)
        Next

EndFunc

;~ =============================================================
func MapDrive($SelectedListViewServerItem)
    local $err,$errorList,$answer
    consolewrite($SelectedListViewServerItem)
    $strServerPath=GetComputerName($SelectedListViewServerItem)
    consolewrite($strServerPath)
                if $strServerPath == "" Then
                    $strServerPath = InputBox("Custom Server", "Custom Server Address:")
                EndIf

                local $repeat = 1
                local $success = 0
                While $repeat = 1
                    If DriveMapGet($DriveMapLetter) <> $strServerPath Then
                        DriveMapDel($DriveMapLetter)
                        sleep(250)
                        $mapped = DriveMapAdd($DriveMapLetter, $strServerPath, 8)
                        $errorList = " Undefined / Other error|Access to the remote share was denied|The device is already assigned|Invalid device name|Invalid remote share|Invalid password"
                        $errorList = StringSplit($errorList, "|");make an array for error code lookup
                        If $mapped Then
                            MsgBox(0, "Drive Mapped", "Drive mapped to " & @CRLF & $strServerPath)
                            $repeat = 0

                        ElseIf Not $mapped then
                            $success = 0
                            If $err = 6 or $err = 2 Then
                                $answer = MsgBox(6,"Error", "Username or password is invalid... Please try again.")
                                If $answer <> $IDTRYAGAIN Then $repeat = 0
                            Else
                                $answer = MsgBox(4096,"Error", "Error code: " & $errorList[$err])
                                If $answer <> $IDTRYAGAIN Then $repeat = 0
                            EndIf
                        EndIf
                    Else
                        MsgBox(0, "Already Mapped", "The drive is already mapped to the selected server")
                        $repeat = 0
                    EndIf
                WEnd
EndFunc
;~ =============================================================
func GetComputerName($SelectedListViewServerItem)
    local $Fields=StringSplit($SelectedListViewServerItem,"|")
    return $Fields[2]
EndFunc

;~ =============================================================
Func CheckMappedDrive()
    local $tempResult,$returnEL

    $tempResult=DriveMapGet($DriveMapLetter)
    $returnEL=@error
    if $tempResult="" Then
        Guictrlsetdata($StatusMappedDrive,$DriveMapLetter & " not mapped")
    Else
        Guictrlsetdata($StatusMappedDrive,$DriveMapLetter & " is mapped")
    EndIf
    return $returnEL


    EndFunc

As you can see I re-wrote a lot of code, maybe it is also easier for you now to read.

I added some functions to shrink the While loop of the GUI. I removed some stuff (not needed). Added some stuff like the "Mapped drive status".

But actually I was of course not able to test the main function (shame on me), so please try now your mapping and ghosting....

These functions are mainly left as they were before. Without the check for success with a global var.

Edited by Tankbuster
Link to comment
Share on other sites

As you can see I re-wrote a lot of code, maybe it is also easier for you now to read.

I added some functions to shrink the While loop of the GUI. I removed some stuff (not needed). Added some stuff like the "Mapped drive status".

But actually I was of course not able to test the main function (shame on me), so please try now your mapping and ghosting....

These functions are mainly left as they were before. Without the check for success with a global var.

Thanks a million! I got it working, just had to change 1 number in the array for the server list. In the "GetComputerName" function I had to change "Return $Fields[2]" to "Return $Fields[3]". I also added "CheckMappedDrive()" after it mapped the drive to update the label. The complete finished (and working) code is below:

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

Opt('MustDeclareVars', 1)

    ;Device items
    Global $deviceGroup,$mapDrvButton, $serverGroup,$mapped
    Global $ServerList, $DeviceList
    Global $StatusMappedDrive
    Global $strServerPath
    Global $DriveMapLetter="J:"
    Dim $arrDevice[10],$DeviceListItem[10]
    Dim $arrServer[10],$ServerListItem[10]
_Main()

Func _Main()
    ;Main items
    Local $filemenu
    Local $exititem, $helpmenu, $aboutitem, $okbutton, $cancelbutton
    Local $msg, $file
    local $SelectedListViewDeviceItem,$SelectedListViewServerItem


    GUICreate("Auto Ghosting Utility", 410, 300, (@DesktopWidth - 410) / 2, (@DesktopHeight - 300) / 2, -1, $WS_EX_ACCEPTFILES)

    ;Create the list of servers
    $serverGroup = GUICtrlCreateGroup("Servers", 10, 10, 190, 210)
    $serverList = GUICtrlCreateListView("No|Server|Server UNC Path", 30, 30, 147, 161);

    $mapDrvButton = GUICtrlCreateButton("&Map Drive", 30, 195, 90, 20)
    $StatusMappedDrive = GUICtrlCreateLabel("---", 125, 197, 100, 20)

    ;Create the list of devices
    $deviceGroup = GUICtrlCreateGroup("Devices", 210, 10, 190, 210)
    $deviceList = GUICtrlCreateListView("No|Model|Ghost", 230, 30, 147, 161)

    $filemenu = GUICtrlCreateMenu("&File")
    $exititem = GUICtrlCreateMenuItem("E&xit", $filemenu)
    $helpmenu = GUICtrlCreateMenu("&Help")
    $aboutitem = GUICtrlCreateMenuItem("&About", $helpmenu)


    $okbutton = GUICtrlCreateButton("&OK", 70, 250, 70, 20)

    $cancelbutton = GUICtrlCreateButton("&Cancel", 270, 250, 70, 20)

    ;INI file items
    GetIni()
    CheckMappedDrive()

    GUISetState()

    while 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $cancelbutton; Or $msg = $GUI_EVENT_CLOSE
                ExitLoop

            Case $msg = $exititem
                ExitLoop

            Case $msg = $okbutton
                if CheckMappedDrive()=2 Then
                    MsgBox(0, "Drive Mapping", "Drive "&$DriveMapLetter&" is not mapped")
                Else
                    $SelectedListViewDeviceItem=GUICtrlRead(GUICtrlRead($deviceList))
                    if $SelectedListViewDeviceItem==0 Then
                        MsgBox(0, "Device Selection", "No device selected")
                    Else
                        consolewrite(":"&$SelectedListViewDeviceItem&":")
                        Local $Fields=StringSplit($SelectedListViewDeviceItem,"|")
                        Ghosting($Fields[2],$Fields[3])
                    EndIf
                EndIf

            Case $msg = $aboutitem
                MsgBox(0, "About", "Version 1.0.0" & @CRLF & "Developed by Me")
            Case $msg = $mapDrvButton
                $SelectedListViewServerItem=GUICtrlRead(GUICtrlRead($serverList))

                if $SelectedListViewServerItem=0 Then
                        MsgBox(0, "Server Selection", "No server selected")
                    Else
                        MapDrive(GUICtrlRead(GUICtrlRead($serverList)))
                endif
           EndSelect
    WEnd

    GUIDelete()

    Exit
EndFunc   ;==>_Main

func Ghosting($SelectedDeviceType,$SelectedDevice)
    msgbox(0,"Ghosting","ghosting will start with"&@CRLF&$DriveMapLetter&"\Ghost80\Ghost32\Ghost32.exe -NTIL -Clone,mode=load,src=J:\WXP-Prod\W632\" & $SelectedDevice & ".GHO,dst=1 -batch")
    local $ghosting = RunWait($DriveMapLetter&"\Ghost80\Ghost32\Ghost32.exe -NTIL -Clone,mode=load,src=J:\WXP-Prod\W632\" & $SelectedDevice & ".GHO,dst=1 -batch")
    Run("Wpeutil reboot", "x:")
EndFunc
;~ =============================================================
func GetIni()
    $DeviceListItem = IniReadSection("x.ini", "Devices")
    $ServerListItem = IniReadSection("x.ini", "Servers")
        For $i = 1 To $DeviceListItem[0][0]
            $arrDevice[$i] = GUICtrlCreateListViewItem($i&"|" & $DeviceListItem[$i][1], $deviceList)
        Next
        For $i = 1 To $ServerListItem[0][0]
            $arrServer[$i] = GUICtrlCreateListViewItem($i&"|" & $ServerListItem[$i][0]&"|" & $ServerListItem[$i][1], $serverList);
        Next

EndFunc

;~ =============================================================
func MapDrive($SelectedListViewServerItem)
    local $err,$errorList,$answer
    consolewrite($SelectedListViewServerItem)
    $strServerPath=GetComputerName($SelectedListViewServerItem)
    consolewrite($strServerPath)
                if $strServerPath == "" Then
                    $strServerPath = InputBox("Custom Server", "Custom Server Address:")
                EndIf

                local $repeat = 1
                local $success = 0
                While $repeat = 1
                    If DriveMapGet($DriveMapLetter) <> $strServerPath Then
                        DriveMapDel($DriveMapLetter)
                        sleep(250)
                        $mapped = DriveMapAdd($DriveMapLetter, $strServerPath, 8)
                        $errorList = " Undefined / Other error|Access to the remote share was denied|The device is already assigned|Invalid device name|Invalid remote share|Invalid password"
                        $errorList = StringSplit($errorList, "|");make an array for error code lookup
                        If $mapped Then
                            CheckMappedDrive()
                            MsgBox(0, "Drive Mapped", "Drive mapped to " & @CRLF & $strServerPath)
                            $repeat = 0

                        ElseIf Not $mapped then
                            $success = 0
                            If $err = 6 or $err = 2 Then
                                $answer = MsgBox(6,"Error", "Username or password is invalid... Please try again.")
                                If $answer <> $IDTRYAGAIN Then $repeat = 0
                            Else
                                $answer = MsgBox(4096,"Error", "Error code: " & $errorList[$err])
                                If $answer <> $IDTRYAGAIN Then $repeat = 0
                            EndIf
                        EndIf
                    Else
                        MsgBox(0, "Already Mapped", "The drive is already mapped to the selected server")
                        $repeat = 0
                    EndIf
                WEnd
EndFunc
;~ =============================================================
func GetComputerName($SelectedListViewServerItem)
    Local $Fields=StringSplit($SelectedListViewServerItem,"|")
    Return $Fields[3]
EndFunc

;~ =============================================================
Func CheckMappedDrive()
    local $tempResult,$returnEL

    $tempResult=DriveMapGet($DriveMapLetter)
    $returnEL=@error
    if $tempResult="" Then
        Guictrlsetdata($StatusMappedDrive,$DriveMapLetter & " not mapped")
    Else
        Guictrlsetdata($StatusMappedDrive,$DriveMapLetter & " is mapped")
    EndIf
    Return $returnEL

    EndFunc
Link to comment
Share on other sites

Thanks a million! I got it working, just had to change 1 number in the array for the server list. In the "GetComputerName" function I had to change "Return $Fields[2]" to "Return $Fields[3]". I also added "CheckMappedDrive()" after it mapped the drive to update the label. The complete finished (and working) code is below:

;) Thx, your're welcome.

Yes, I thought yesterday also about the Fields but actually I was to tiered to figure it out in the correct way. But hey, now you completed it by yourself.

Of course there is still some room for improvements, like reading the mapped drive letter by INI, Running process progressbar for the GHOSTING and so on and on...

But you will find now help for each section in the forum. CU

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...