Jump to content

get INI key not value


Recommended Posts

i have this: and am able to reference the KEY VALUE with $iPing.

how can i reference the KEY ?

$arr[$i][0] right? doesnt work

Func APing($iKey)

$iPing = IniRead($Ini, "ASSETS", $iKey, "")

If $iPing Then

Ping($iPing)

If @error Then

MsgBox(16, "Shot", $iPing & " is offline")

Else

Call("ARights")

EndIf

Else

MsgBox(16, "Select Asset..", "Select Asset first.")

EndIf

EndFunc ;APing

Func LoadCombo()

Local $arr = IniReadSection($Ini, "ASSETS"), $sAssets

If Not @error Then

For $i = 1 To $arr[0][0]

$sAssets &= $arr[$i][0] & "|"

Next

Return StringTrimRight($sAssets, 1)

EndIf

Return "No assets loaded"

EndFunc ;LoadCombo

Edited by gcue
Link to comment
Share on other sites

i have this: and am able to reference the KEY VALUE with $iPing.

how can i reference the KEY ?

$arr[$i][0] right? doesnt work

Func APing($iKey)

$iPing = IniRead($Ini, "ASSETS", $iKey, "")

If $iPing Then

Ping($iPing)

If @error Then

MsgBox(16, "Shot", $iPing & " is offline")

Else

Call("ARights")

EndIf

Else

MsgBox(16, "Select Asset..", "Select Asset first.")

EndIf

EndFunc ;APing

Func LoadCombo()

Local $arr = IniReadSection($Ini, "ASSETS"), $sAssets

If Not @error Then

For $i = 1 To $arr[0][0]

$sAssets &= $arr[$i][0] & "|"

Next

Return StringTrimRight($sAssets, 1)

EndIf

Return "No assets loaded"

EndFunc ;LoadCombo

$arr[$i][0] is the Key, and $arr[$i][1] is the Value. Put "#include <Array.au3>" at the top of your script, and then put "_ArrayDisplay($arr, "Debug: $arr")" on the next line after IniReadSection(). You'll probably find you are not reading what you thought you were.

Also, how big is this section? AutoIt is currently limited to 32KB of data per section for backward compatibility with Win9x. If the section is huge, you may get clipping of the data.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

hmm adding that to the include didnt help

MsgBox(0,"",$arr[$i][0]) gets me an error (trying to reference this value outside of these functions.)

the arraydisplay shows what i suspected were the arrays.

its only a 6kb au3

thanks.

$arr[$i][0] is the Key, and $arr[$i][1] is the Value. Put "#include <Array.au3>" at the top of your script, and then put "_ArrayDisplay($arr, "Debug: $arr")" on the next line after IniReadSection(). You'll probably find you are not reading what you thought you were.

Also, how big is this section? AutoIt is currently limited to 32KB of data per section for backward compatibility with Win9x. If the section is huge, you may get clipping of the data.

:)

Edited by gcue
Link to comment
Share on other sites

but im trying to reference this value outside of these functions.

Then don't declare the array as Local inside the function. Declare $arr as Global outside the functions, and remove the "Local" keyword. The Global array $arr will be usable from any context then.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

ok no idea what im doing wrong - but i thought i was following ur instructions right

#Include <GuiConstants.au3>

#Include <Constants.au3>

#include <file.au3>

#include <GuiConstantsEx.au3>

#include <Date.au3>

#include <INet.au3>

#include <Array.au3>

Global $arr, $sAssets

$toolsdir="C:\CRS\Tools"

If Not FileExists($toolsdir) Then

DirCreate($toolsdir)

EndIf

If Not FileExists($toolsdir & "\ETP_Logs") Then

DirCreate($toolsdir & "\ETP_Logs")

EndIf

If Not FileExists($toolsdir & "\ETP_Logs\assets.ini") Then

MsgBox(262144, "ETP Logs", "assets.ini is missing. One has been created for you.")

IniWrite($toolsdir & "\ETP_Logs\assets.ini", "ASSETS", "Asset_Label1","Asset1")

IniWrite($toolsdir & "\ETP_Logs\assets.ini", "ASSETS", "Asset_Label2","Asset2")

IniWrite($toolsdir & "\ETP_Logs\assets.ini", "ASSETS", "Asset_Label3","Asset3")

ShellExecute($toolsdir & "\ETP_Logs\assets.ini")

EndIf

Global $Ini = $toolsdir & "\ETP_Logs\assets.ini", $iPing

GUICreate("ETP Logs v0.12", 350,120); WS_EX_ACCEPTFILES

$combo = GUICtrlCreateCombo("Select Asset...", 50, 40, 200, 20, $CBS_DROPDOWNLIST); create combobox and set first item

GUICtrlSetData(-1, LoadCombo(), "Select Asset...")

$tCur = _Date_Time_GetSystemTime()

$fech = _Date_Time_SystemTimeToDateTimeStr($tCur)

$fecha = StringSplit($fech," ")

$tdate = StringSplit($fecha[1],"/")

$mm_date = $tdate[1]

$dd_date = $tdate[2]

$yyyy_date = $tdate[3]

GUICtrlCreateLabel("Date:", 85, 12)

$month = GUICtrlCreateInput ($mm_date, 120, 10, 30, 20)

GUICtrlCreateLabel("/", 155, 12)

$day = GUICtrlCreateInput ($dd_date, 165, 10, 30, 20)

GUICtrlCreateLabel("/", 200, 12)

$year = GUICtrlCreateInput ($yyyy_date, 210, 10, 40, 20)

$filemenu = GUICtrlCreateMenu("&File")

$fileopenitem = GUICtrlCreateMenuItem("Edit Device List", $filemenu)

$fileexititem = GUICtrlCreateMenuItem("Exit", $filemenu)

$helpmenu = GUICtrlCreateMenu ("&Help")

$helpchangeitem = GUICtrlCreateMenuitem ("Change Log",$helpmenu)

$helpaboutitem = GUICtrlCreateMenuitem ("About",$helpmenu)

$btn = GUICtrlCreateButton ("OK", 255,40,40,25)

GUICtrlSetState(-1, $GUI_FOCUS)

GUISetState()

While 1

$msg = GUIGetMsg()

Switch $msg

Case $GUI_EVENT_CLOSE, $fileexititem

Exit

case $btn

APing(GUICtrlRead($combo))

case $fileopenitem

EditINI()

case $helpchangeitem

Change()

case $helpaboutitem

About()

EndSwitch

WEnd

Func APing($iKey)

$iPing = IniRead($Ini, "ASSETS", $iKey, "")

If $iPing Then

Ping($iPing)

If @error Then

MsgBox(16, "Shot", $iPing & " is offline")

Else

Call("ARights")

EndIf

Else

MsgBox(16, "Select Asset..", "Select Asset first.")

EndIf

EndFunc ;APing

Func EditINI()

ShellExecuteWait("notepad.exe", $Ini)

GUICTrlSetdata($combo,"")

GUICTrlSetdata($combo, "Select Asset...|" & LoadCombo(), "Select Asset...")

EndFunc ;EditINI

Func LoadCombo()

$arr = IniReadSection($Ini, "ASSETS")

If Not @error Then

For $i = 1 To $arr[0][0]

$sAssets &= $arr[$i][0] & "|"

Next

Return StringTrimRight($sAssets, 1)

EndIf

Return "No assets loaded"

EndFunc ;LoadCombo

Func ARights()

RegRead ( "\\" & $iPing & "\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion", "ProgramFilesDir" )

If @error Then

MsgBox(16, "ETP Logs", "You do not have local admin rights on " & $iPing)

Else

Call("Get_Logs")

EndIf

EndFunc ;ARights

Func Get_Logs()

$stats=GUICtrlCreateLabel("Copying Logs...", 10, 75)

If Not FileExists("\\" & $iPing & "\c$\program files\ETP\rolling*.*") Then

MsgBox(0, "ETP Logs", "There are no ETP Logs in " & $iPing)

Return

EndIf

If FileExists($toolsdir & "\ETP_Logs\*.zip") Then

FileDelete($toolsdir & "\ETP_Logs\*.zip")

EndIf

If Not FileExists("\\" & $iPing & "\c$\temp\ETP_Logs\") Then

DirCreate("\\" & $iPing & "\c$\temp\ETP_Logs\")

EndIf

$date_request = GUICtrlRead($month) & "/" & GUICtrlRead($day) & "/" & GUICtrlRead($year)

$source = ("\\" & $iPing & "\c$\program files\etp\")

$destination = ("\\" & $iPing & "\c$\temp\ETP_Logs")

$fileList = _FileListToArray($source, "rolling*.*", 1)

For $X = 1 to $fileList[0]

$Date = FileGetTime($source & "\" & $fileList[$X], 0, 0)

$fDate = StringFormat("%s/%s/%s", $Date[1],$Date[2],$Date[0])

If $fDate = $date_request Then

FileCopy($source & "\" & $fileList[$X], $destination, 1)

EndIf

Next

If Not FileExists($destination & "\rolling*.*") Then

MsgBox(0, "ETP Logs", "There were no log files dated " & $date_request & " on " & $iPing)

EndIf

$zsource = ($destination & "\rolling*.*")

$zdestination = ($toolsdir & "\ETP_Logs\" & $iPing & "_" & GUICtrlRead($month) & "-" & GUICtrlRead($day) & "-" & GUICtrlRead($year) & ".zip")

$stats=GUICtrlCreateLabel("Zipping Logs...", 10, 75)

RunWait("c:\program files\winzip\wzzip.exe " & $zdestination & " " & $zsource)

$stats=GUICtrlCreateLabel("Complete. ", 10, 75)

Cleanup()

Run(@WindowsDir & "\explorer.exe " & $toolsdir & "\ETP_Logs")

MsgBox(0,"",$arr[$i][0])

_INetMail("", "Logs for", "Here are the logs you requested for")

EndFunc ;GetLogs

Func Cleanup()

EndFunc ;Cleanup

Func Change()

EndFunc ;Change

Func About()

EndFunc ;About

Edited by gcue
Link to comment
Share on other sites

cant see why it doesnt work the way you say it would psalty.

any idea?

MsgBox(0,"",$arr[$i][0])

You Global-ized your $arr array variable, which had to be done to use its contents in other functions. So far so good. But you didn't Global-ize $i. How do the other functions know which row of the 2D array to reference?

All this may be moot, however, because I can't for life of me figure out why you put that MsgBox() there. Nowhere else in the code you've shown do you reference $arr outside of LoadCombo(), the function that uses it for IniReadSection. The return value from LoadCombo() is a string. What do want global access to the array for...? This little wild-goose chase started with you saying:

MsgBox(0,"",$arr[$i][0]) gets me an error (trying to reference this value outside of these functions.)

:)
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

i globalized $i and i dont get the right value returned - i get some random number (18) instead of the actual key value

the reason im trying to access this key is because i want to rename the file something else.

i also want to start an email and have that email subject and body include that key name.

i just put in that msgbox as a check to see what its going to output ;-)

MsgBox(0,"",$arr[$i][0])

You Global-ized your $arr array variable, which had to be done to use its contents in other functions. So far so good. But you didn't Global-ize $i. How do the other functions know which row of the 2D array to reference?

All this may be moot, however, because I can't for life of me figure out why you put that MsgBox() there. Nowhere else in the code you've shown do you reference $arr outside of LoadCombo(), the function that uses it for IniReadSection. The return value from LoadCombo() is a string. What do want global access to the array for...? This little wild-goose chase started with you saying:

:)

Link to comment
Share on other sites

i globalized $i and i dont get the right value returned - i get some random number (18) instead of the actual key value

the reason im trying to access this key is because i want to rename the file something else.

i also want to start an email and have that email subject and body include that key name.

i just put in that msgbox as a check to see what its going to output ;-)

When a variable is used in a For/Next loop it is automatically LOCAL. Even if you name a GLOBAL variable the same thing, the one in the loop will be local. Demo:
Global $i = -1

Function()
; Show the value of the Global $i
ConsoleWrite("Debug: Done, $i = " & $i & @LF)

Func Function()
; The $i used in this loop is not the same variable as the Global $i
    For $i = 1 to 5
; Show the values of the local $i
        ConsoleWrite("Debug: $i = " & $i & @LF)
    Next
EndFunc

Now, the $i in your For/Next loop is used to walk through all the entries in $arr -- how many are there? Once that loop is done, how do you know which one you want to access elsewhere? As far as I know, $arr might have hundreds of entries. Which one do you want to use?

This is why just using $i doesn't make sense. What value do want $i to have at the time you reference that array in the other functions? Another demo:

Global $arr[1][2], $idx

Function1()
Function2()
Function3()

Func Function1()
; Set some values in the 2D $arr array
    ReDim $arr[10][2]
    $arr[0][0] = 9; number of entries
    For $i = 1 to $arr[0][0]
; Store digit, and double the digit
        $arr[$i][0] = $i
        $arr[$i][1] = $i * 2
    Next
EndFunc

Func Function2()
; Set the Global index to 3
    $idx = 3
EndFunc

Func Function3()
; Use the Global array and index
    MsgBox(64, "Results", "Entry no." & $idx & " is:   " & $arr[$idx][0] & " * 2 = " & $arr[$idx][1])
EndFunc

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

gotya - makes sense.

i can reference $iPing which is the KEYVALUE. Can i use this to get to the KEY itself using INIREAD?

Not directly, but you could IniReadSection(), then search the array for the matching value and return the key that goes with it using a function like this:
; ------------------------------------------------------------------
; Function:  _IniFindKeyByValue
; Purpose:  Find an ini file key within a section by its value
; Syntax:  _IniFindKeyByValue($sIniFile, $sIniSection, $sValue)
;   Where:  $sIniFile = Full path to ini file
;           $sIniSection = Section name within ini file
;           $sValue = String value to search for
; Returns:  On Success = string key name that had matching value
;           On Failure = Sets @error and returns 0
; Author:   PsaltyDS
; ------------------------------------------------------------------
Func _IniFindKeyByValue($sIniFile, $sIniSection, $sValue)
    If Not FileExists($sIniFile) Then Return SetError(1, 0, 0)
    Local $avSection = IniReadSection($sIniFile, $sIniSection)
    If @error Then Return SetError(2, 0, 0)
    For $n = 1 To $avSection[0][0]
        If $avSection[$n][1] = $sValue Then Return $avSection[$n][0]
    Next
    Return SetError(3, 0, 0)
EndFunc  ;==>_IniFindKeyByValue

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...