Sign in to follow this  
Followers 0
JohnWilson

Line 8 always showing in Array

4 posts in this topic

I have a script that searches a CSV file for any lines containing a string and then builds checkboxes out of those links to allow a user to install the files. Everything is working as intended, except that line 8 of the CSV always becomes part of the array even though the string is not found in that line. I am sure it is something simple in my code, but I am not seeing what I need to change. Any help is much appreciated.

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstants.au3>
#include <File.au3>
#include <InetConstants.au3>
#include <GuiButton.au3>
#include <WindowsConstants.au3>

#RequireAdmin

Func Octet()

; Creates array called parsed ip, with each octet of the IP address as different items
Global $parsedip = StringSplit(@IPAddress1, ".")

; Creates variable called third which grabs only the third octet
Global $third = $parsedip[1] & "." & $parsedip[2] & "." & $parsedip [3] & "."

EndFunc

Octet()

Local $sFileName = "\\rowanads\Common\Openarea\NSS\Wilson\test.csv"
Local $searchStr = $third
; MsgBox(0, "GUI Event", $third)
$arr = _LineNumsOfSearchStr($sFileName, $searchStr, False)


Global $ControlIDs[UBound($arr)]

; _ArrayDisplay($arr)

GUICreate("Printers Near You", 600, 300)
GUICtrlCreateLabel("Printers Near You", 30, 30)

For $item = 0 to UBound($arr) -1
   $tester = FileReadLine ( $sFileName, $arr[$item] )
   $firstpart = StringSplit($tester, ",")
   $final = $firstpart[1]
   $finale = StringStripWS($final, 8)
   $secondpart = $firstpart[2]
   $ControlIDs[$Item] = GUICtrlCreateCheckbox($finale, 10, $item * 20, 250, 20) ; create checkboxes and store their control IDs
   $downloader = "http://uniprint2/Uniprint/" & $finale & "_for_Lte.exe"
   InetGet("http://uniprint2/Uniprint/" & $finale & "_for_Lte.exe","C:\temp\" & $item & ".exe", $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)
   ; MsgBox(0, "GUI Event", $downloader)
Next

$okbutton = GUICtrlCreateButton("Start", 70, 275, 60)
GUISetState(@SW_SHOW)
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $okbutton
            ; MsgBox(0, "GUI Event", "The Installs will now begin")

            For $Loop = 0 To UBound($arr) - 1 ; Loop through the checkboxes
                If GUICtrlRead($ControlIDs[$Loop]) = $GUI_CHECKED Then ; Find if it's checked
                    RunWait('C:\Temp\' & $Loop & '.exe', '', @SW_SHOWDEFAULT) ; run the program stored in the same position in the $arr array
                EndIf
             Next


            MsgBox(0, "GUI Event", "Your printers have been installed!")
            ExitLoop
            Case $msg = $GUI_EVENT_CLOSE

            MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")

            ExitLoop
    EndSelect
 WEnd


 Func _LineNumsOfSearchStr($sFileName, $searchString, $bDeleteLine = False)
    Local $location, $aCurrentLineNum, $iCurrentLineNum, $sFile, $iOccur = 1, $sRes = ""
    If FileExists($sFileName) = 0 Then Return 1
    Do
        $sFile = FileRead($sFileName)
        $location = StringInStr($sFile, $searchString, 0, $iOccur) ; Find the $iOccur occurrence of the "substring"

        If $location > 0 Then
            $aCurrentLineNum = StringRegExp(StringRegExpReplace($sFile, "(?s)(.{" & $location & "})(.*)$", "\1"), "(?s)(\v+)", 3) ;Find line number
            $iCurrentLineNum = UBound($aCurrentLineNum) + 1 ; Line number

            ;ConsoleWrite("CharPos: " &  $location & "   Ln: " & $iCurrentLineNum & @CRLF)
            $sRes &= $iCurrentLineNum & "|"
            If $bDeleteLine Then
                _FileWriteToLine($sFileName, $iCurrentLineNum, "", 1) ; Remove found line from file.
            Else
                $iOccur += 1
            EndIf
        Else
            ExitLoop
        EndIf
        Sleep(10)
    Until 0
    ;ShellExecute($sFileName)
    Return StringSplit(StringTrimRight($sRes, 1), "|")
EndFunc   ;==>_LineNumsOfSearchStr
;

Share this post


Link to post
Share on other sites



JohnWilson,

Looking at your _LineNumsOfSearchStr function it seems you are using a very inefficient way to search the file. Why not read the file into an array (_FileReadToArray) and then check each line just the once, rather then looking for the string one character at a time throughout the entire file? :huh:

Anyway, an example of a typical .csv file might be useful if you would like me to try and produce a new function. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

M23, 

Thanks for your reply. Any reasoning for using an inefficient method would be more based on my inexperience with AutoIT than anything. If there is a better way to do anything I would be more than happy to hear it and see it in action. As far as a sample csv, I do not have the actual CSV I am using because it is on my work machine, but here is the general scheme:

HP2300, 150.250.249.25
HP5200, 150.250.249.20
HP4300, 150.250.242.104
Xerox300, 150.250.247.25
HP2300, 150.250.247.21
HP5100, 150.250.247.25
Ricoh2000, 150.250.243.56
Dell5150, 150.250.241.50
HP2900, 150.250.245.48
HP2500, 150.250.240.56
 
It is a file with printer names and their matching IPs. The idea of the script is to scan a user's IP address and then depending on their subnet, match that to the csv file line to download the Pharos Uniprint packages and install them via a checklist. Everything is working as intended save for the fact that no matter what the subnet, line 8 always shows as a possible printer install. I even removed line 8 at one point, fearing it was something in the file, but then line 9 became line 8 and then that displayed. Thanks again for your help, and any explanation with the code would be awesome, I really want to not only figure out what the problem is, but learn what to do going forward. 
 
Thanks,
John

 

Share this post


Link to post
Share on other sites

JohnWilson,

Here is my take on how you might code your script. I have tried to keep the same basic format (although I could have streamlined it a lot more - e.g. only downloading the files you need when required) so that you can hopefully follow what I have done:

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>
#include <InetConstants.au3>
#include <WindowsConstants.au3>

;#RequireAdmin

Global $aLines ; We need this to be visible everywhere

Local $sFileName = "test.csv"
Local $searchStr = Octet() ; Why not just get the value directly?

; Get the array of valid line numbers (note they will be in reverse order - see below to understand why)
$aLineNumbers = _LineNumsOfSearchStr($sFileName, $searchStr, False)
Global $ControlIDs[UBound($aLineNumbers)]

GUICreate("Printers Near You", 600, 300)
GUICtrlCreateLabel("Printers Near You", 30, 30)

; As the numbers ar in reverse order, we loop through in reverse order too
For $item = UBound($aLineNumbers) - 1 To 1 Step -1

    ; A bit simpler than before
    $aSplit = StringSplit($aLines[$item], ",")
    $finale = StringStripWS($aSplit[1], 8)
    $ControlIDs[$item] = GUICtrlCreateCheckbox($finale, 10, $item * 20, 250, 20) ; create checkboxes and store their control IDs

    $downloader = "http://uniprint2/Uniprint/" & $finale & "_for_Lte.exe"
    ConsoleWrite("InetGet: " & $downloader & " ---> C:\temp\" & $item & ".exe" & @CRLF) ; Simulate downloading the file
    ;InetGet("http://uniprint2/Uniprint/" & $finale & "_for_Lte.exe","C:\temp\" & $item & ".exe", $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)

Next

$okbutton = GUICtrlCreateButton("Start", 70, 275, 60)

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg() ; Switch is cleaner than Select in my opinion
        Case $okbutton
            For $Loop = 0 To UBound($aLineNumbers) - 1 ; Loop through the checkboxes
                If GUICtrlRead($ControlIDs[$Loop]) = $GUI_CHECKED Then ; Find if it's checked
                    MsgBox($MB_SYSTEMMODAL, "Running", 'C:\Temp\' & $Loop & '.exe') ; Simulate running the software
                    ;RunWait('C:\Temp\' & $Loop & '.exe', '', @SW_SHOWDEFAULT) ; run the program stored in the same position in the $aLineNumbers array
                EndIf
            Next
            MsgBox(0, "GUI Event", "Your printers have been installed!")
            ExitLoop
        Case $GUI_EVENT_CLOSE
            MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
            ExitLoop
    EndSwitch
WEnd

Func Octet()

    ; Returns a variable which grabs only the first 3 octets of @IPAddress1 (we are simulating it)
    ;Return StringRegExpReplace(@IPAddress1, "(^.*\.).*", "$1")
    Return StringRegExpReplace("150.250.249.25", "(^.*\.).*", "$1")

EndFunc   ;==>Octet

Func _LineNumsOfSearchStr($sFileName, $searchString, $bDeleteLine = False)

    ; Does file exist?
    If FileExists($sFileName) = 0 Then
        Return 1
    Else
        Local $sRes = ""
        ; Read file into an array
        _FileReadToArray($sFileName, $aLines)
        ; Loop through the array from the bottom up in case we need to delete any lines
        For $i = $aLines[0] To 1 Step -1
            ; Does this line constin the search string?
            If StringInStr($aLines[$i], $searchString) Then
                ; Add the line number - much easier than that awful RegEx you were using!
                $sRes &= $i & "|"
                If $bDeleteLine Then
                    ; This is why we need to loop from the bottom up - otherwise the aray changes size as we descend and we get bounds errors
                    _ArrayDelete($aLines, $i)
                    ; Reduce the count
                    $aLines[0] -= 1
                EndIf
            EndIf
        Next
        ; if lines might heve been deleted, rewrite the file from the array - missing the count element
        If $bDeleteLine Then
            _FileWriteFromArray($sFileName, $aLines, 1)
        EndIf

        ; Return the line numbers - but remember they are in reverse order
        Return StringSplit(StringTrimRight($sRes, 1), "|")

    EndIf
EndFunc   ;==>_LineNumsOfSearchStr
Please ask if you have any questions about what I have done or why I have done it. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
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
Sign in to follow this  
Followers 0