Jump to content

Loop getting stuck


aa2zz6
 Share

Recommended Posts

I'm trying to setup this script to read a text file that'll list a series True or False in order for certain scripts to run for ArcGIS Desktop. The problem I'm having is the main script that reads the log file is getting stuck on the first If statement StringintStr. The idea was to have a main script read a text file and whether it says true or false would determine which map function to execute  

 

#include <File.au3>
#include <Array.au3>

Local Const $Map_Settings = @ScriptDir & '\Map Settings.log'

;log file
Local Const $sFilePath = @ScriptDir & '\Directories\Log Sheet.log'

Global $True = "True"
Global $False = "False"
Global $equal = "="

Global $Monitor = "Monitor"
Global $ArcGIS = "ArcGIS"

$handle_read = FileOpen($Map_Settings)

While 1
    ; read each line from a file
    $line_read = FileReadLine($handle_read)
    ; exit the loop if end of file
    If @error Then ExitLoop

    If StringInStr($line_read, $Monitor & $equal & $True) Then
        ;MsgBox(0, "Result", $Monitor & $equal & $True, 1)
        RunWait(@AutoItExe & ' "' & @ScriptDir & '\Directories\Scripts\[0] Monitor Log\Monitor.exe"')
        _FileWriteToLine($sFilePath, 1, "" & $Monitor & $equal & $True, True)
        ExitLoop
    EndIf

    ;Open ArcGIS Explorer
    If StringInStr($line_read, $ArcGIS & $equal & $True) Then
        ;MsgBox(0, "Result", $ArcGIS & $equal & $True, 1)
        RunWait(@AutoItExe & ' "' & @ScriptDir & '\E3.exe"')
        _FileWriteToLine($sFilePath, 1, "" & $ArcGIS & $equal & $True, True)
        ExitLoop
    EndIf
WEnd

 

Link to comment
Share on other sites

I think it's just reading the first line of the log, you could place a ConsoleWrite($line_read & @crlf) to debug.  Personally I would use FileReadToArray or use a variable in your loop to step up for example:

$i = 1
While 1
    ; read each line from a file
    $line_read = FileReadLine($handle_read, $i)
    ; exit the loop if end of file
    If @error Then ExitLoop

    If StringInStr($line_read, $Monitor & $equal & $True) Then
        ;MsgBox(0, "Result", $Monitor & $equal & $True, 1)
        RunWait(@AutoItExe & ' "' & @ScriptDir & '\Directories\Scripts\[0] Monitor Log\Monitor.exe"')
        _FileWriteToLine($sFilePath, 1, "" & $Monitor & $equal & $True, True)
        ExitLoop
    EndIf

    ;Open ArcGIS Explorer
    If StringInStr($line_read, $ArcGIS & $equal & $True) Then
        ;MsgBox(0, "Result", $ArcGIS & $equal & $True, 1)
        RunWait(@AutoItExe & ' "' & @ScriptDir & '\E3.exe"')
        _FileWriteToLine($sFilePath, 1, "" & $ArcGIS & $equal & $True, True)
        ExitLoop
    EndIf
    $i += 1
WEnd

 

Link to comment
Share on other sites

I'm trying to use the FileReadToArray but I don't get any return results or errors. 

#include <file.au3>

Global $True = "True"
Global $False = "False"
Global $equal = "="

Global $Monitor = "Monitor"

Global $vReturn
Global $handle_read

_read()

Func _read()
    Local Const $sFilePath = @ScriptDir & '\Map Settings.log'

    _FileReadToArray($sFilePath, $vReturn)
    $handle_read = FileOpen($sFilePath)

    If @error Then
        MsgBox(4096, "Error", "Unable to read")
    Else
        For $i = 1 To $vReturn[1]
            If $i[$vReturn][1] == "test" Then
                MsgBox($MB_SYSTEMMODAL, "Map", "Preview ", 5)
                ExitLoop ; Exits For - Next loop (stops) when search value, "", is found?
            EndIf
        Next
    EndIf
EndFunc   ;==>_read

 

Edited by aa2zz6
Link to comment
Share on other sites

Try this:

#include <file.au3>

Global $True = "True"
Global $False = "False"
Global $equal = "="

Global $Monitor = "Monitor"

Global $vReturn
Global $handle_read

_read()

Func _read()
    Local $sFilePath = @ScriptDir & '\Map Settings.log'

    _FileReadToArray($sFilePath, $vReturn)
    If @error Then
        MsgBox(4096, "Error", "Unable to read")
    Else
        For $i = 1 To $vReturn[0]
            If $vReturn[$i] == 'test' Then
                MsgBox($MB_SYSTEMMODAL, "Map", "Preview ", 5)
                ExitLoop ; Exits For - Next loop (stops) when search value, "", is found?
            EndIf
        Next
    EndIf
EndFunc   ;==>_read

 

Link to comment
Share on other sites

Using your first post as an example, you can place a toggle $debug = True or False at the top of your script and then within your script add ConsoleWrite entries to see what the result of your function is.  Using this example I found that I was incorrect in suggesting that it was only reading the first line, however I could then determine that you may or may not have had spaces in your search, so for example "Monitor = True" is not the same as "Monitor=True" so would have returned 0 (String was not found).

By the way in your Map Settings.log do you have any [Section Names] in square brackets, if so you could just use IniRead to get those values.

#include <File.au3>
#include <Array.au3>

Local $debug = True
Local Const $Map_Settings = @ScriptDir & '\Map Settings.log'

;log file
Local Const $sFilePath = @ScriptDir & '\Directories\Log Sheet.log'

Global $True = "True"
Global $False = "False"
Global $equal = "="

Global $Monitor = "Monitor"
Global $ArcGIS = "ArcGIS"

$handle_read = FileOpen($Map_Settings)

While 1
    ; read each line from a file
    $line_read = FileReadLine($handle_read)
    ; exit the loop if end of file
    If @error Then ExitLoop
    If $debug Then ConsoleWrite('Read Line: ' & $line_read & @CRLF)

    If StringInStr($line_read, $Monitor & $equal & $True) Then
        If $debug Then ConsoleWrite('StringinStr $Monitor & $equal: ' & StringInStr($line_read, $Monitor & $equal & $True) & @CRLF)
       ;MsgBox(0, "Result", $Monitor & $equal & $True, 1)
        RunWait(@AutoItExe & ' "' & @ScriptDir & '\Directories\Scripts\[0] Monitor Log\Monitor.exe"')
        _FileWriteToLine($sFilePath, 1, "" & $Monitor & $equal & $True, True)
        ExitLoop
    EndIf

    ;Open ArcGIS Explorer
    If StringInStr($line_read, $ArcGIS & $equal & $True) Then
        ;MsgBox(0, "Result", $ArcGIS & $equal & $True, 1)
        RunWait(@AutoItExe & ' "' & @ScriptDir & '\E3.exe"')
        _FileWriteToLine($sFilePath, 1, "" & $ArcGIS & $equal & $True, True)
        ExitLoop
    EndIf
WEnd

PS: This only works from within Scite when using Tools » Go

Edited by Subz
Added PS
Link to comment
Share on other sites

On 1/29/2017 at 0:32 AM, aa2zz6 said:

I'm trying to setup this script to read a text file that'll list a series True or False in order for certain scripts to run for ArcGIS Desktop....The idea was to have a main script read a text file and whether it says true or false would determine which map function to execute

So it has to read each line and execute on that basis, right?

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

On 1/28/2017 at 10:15 PM, Subz said:

Using your first post as an example, you can place a toggle $debug = True or False at the top of your script and then within your script add ConsoleWrite entries to see what the result of your function is.  Using this example I found that I was incorrect in suggesting that it was only reading the first line, however I could then determine that you may or may not have had spaces in your search, so for example "Monitor = True" is not the same as "Monitor=True" so would have returned 0 (String was not found).

By the way in your Map Settings.log do you have any [Section Names] in square brackets, if so you could just use IniRead to get those values.

I haven't used debugging before but I feel it's something that I'll need to learn especially while troubleshooting scripts.

17 hours ago, careca said:

So it has to read each line and execute on that basis, right?

Yeah that's what I've been researching and testing for the past few days. 

Edited by aa2zz6
Link to comment
Share on other sites

If i got it wrong, please correct me, i understood that if true, so this, if false, do that, so:

#include <File.au3>
#include <Array.au3>

Local $debug = True
Local Const $Map_Settings = @ScriptDir & '\123Settings.log'

Global $True = "True"
Global $False = "False"
Global $equal = "="

Global $Monitor = "Monitor"
Global $ArcGIS = "ArcGIS"

$handle_read = FileOpen($Map_Settings)

While 1
    ; read each line from a file
    $line_read = FileReadLine($handle_read)
    ; exit the loop if end of file
    If @error Then ExitLoop
    If $debug Then ConsoleWrite('Read Line: ' & $line_read & @CRLF)

    If StringInStr($line_read, $True, 2, 1) Then
       MsgBox(0, "Result", $Monitor & $equal & $True, 1)
        ;RunWait(@AutoItExe & ' "' & @ScriptDir & '\Directories\Scripts\[0] Monitor Log\Monitor.exe"')
        ;_FileWriteToLine($sFilePath, 1, "" & $Monitor & $equal & $True, True)
        ExitLoop
    EndIf

    ;Open ArcGIS Explorer
    If StringInStr($line_read, $False, 2, 1) Then
        MsgBox(0, "Result", $ArcGIS & $equal & $False, 1)
        ;RunWait(@AutoItExe & ' "' & @ScriptDir & '\E3.exe"')
        ;_FileWriteToLine($sFilePath, 1, "" & $ArcGIS & $equal & $True, True)
        ExitLoop
    EndIf
WEnd

I don't get why the filewriteline, it's a log, right?

This way it searches No-Case words true and false and act accordingly. Mbox to show, change as appropriate.

Why not have the code of E3 and monitor exes included in this script? are they yours?

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

16 hours ago, careca said:

If i got it wrong, please correct me, i understood that if true, so this, if false, do that, so:

I don't get why the filewriteline, it's a log, right?

This way it searches No-Case words true and false and act accordingly. Mbox to show, change as appropriate.

Why not have the code of E3 and monitor exes included in this script? are they yours?

I tried to create a series of functions inside my script but I couldn't figure out how to assign a variable to each functions so when the script scans for Explorer=true or false inside the log file and depending on the choice it would do something. I couldn't figure out how to attach a variable to a function so I move them into there own scripts and call them. The E3.exe is a 3rd party mapping software provided by ESRI.

123Settings.log ;~ wrote inside log

Explorer=True

#include <File.au3>
#include <Array.au3>

Local $debug = True
Local Const $Map_Settings = @ScriptDir & '\123Settings.log'

Global $True = "True"
Global $False = "False"
Global $equal = "="

Global $ArcGIS = "Explorer"

$handle_read = FileOpen($Map_Settings)

While 1
    ; read each line from a file
    $line_read = FileReadLine($handle_read)
    ; exit the loop if end of file
    If @error Then ExitLoop
    If $debug Then ConsoleWrite('Read Line: ' & $line_read & @CRLF)

    If StringInStr($line_read, $True, 2, 1) Then
       MsgBox(0, "Result", $Monitor & $equal & $True, 1)
        ;RunWait(@AutoItExe & ' "' & @ScriptDir & '\Directories\Scripts\[0] Monitor Log\Monitor.exe"')
        ;_FileWriteToLine($sFilePath, 1, "" & $Monitor & $equal & $True, True)
        ExitLoop
    EndIf

    ;Open ArcGIS Explorer
    If StringInStr($line_read, $False, 2, 1) Then
        MsgBox(0, "Result", $ArcGIS & $equal & $False, 1)
        ;RunWait(@AutoItExe & ' "' & @ScriptDir & '\E3.exe"')
        ;_FileWriteToLine($sFilePath, 1, "" & $ArcGIS & $equal & $True, True)
        ExitLoop
    EndIf
WEnd

Func ArcGIS_Explorer();How to integrate unique variable to call function if true or false
    ;Load Map and check settings
EndFunc

 

 

Edited by aa2zz6
Link to comment
Share on other sites

So then you can use something like this:

Local $debug = True

Global $ArcGIS = _ReadLogFile()

If $ArcGIS = True Then ArcGIS_Explorer('Do something here')

Func _ReadLogFile($sSearch = 'Explorer=true')
    Local $line_read
    Local $Map_Settings = @ScriptDir & '\Map Settings.log'
    Local $handle_read = FileOpen($Map_Settings, 0)
    While 1
        ; read each line from a file
        $line_read = FileReadLine($handle_read)
        ; exit the loop if end of file
        If @error Then ExitLoop
        If $debug Then ConsoleWrite('Read Line: ' & $line_read & @CRLF)

        If StringInStr($line_read, $sSearch) Then Return True
    WEnd
    ;~ $sSearch was not found
    Return False
EndFunc

Func ArcGIS_Explorer($sMsgBox);How to integrate unique variable to call function if true or false
    MsgBox(0,'Explorer found in log', $sMsgBox)
    ;Load Map and check settings
EndFunc

 

Link to comment
Share on other sites

Local $debug = True

Global $ArcGIS = _ReadLogFile()
Global $Settings = _ReadLogFile()

If $ArcGIS = True Then ArcGIS_Explorer('Do something here')
If $Settings = True Then ArcGIS_Settings('Do something here')

Func _ReadLogFile($sSearch = 'Explorer=true')
    Local $line_read
    Local $Map_Settings = @ScriptDir & '\Map Settings.log'
    Local $handle_read = FileOpen($Map_Settings, 0)
    While 1
        ; read each line from a file
        $line_read = FileReadLine($handle_read)
        ; exit the loop if end of file
        If @error Then ExitLoop
        If $debug Then ConsoleWrite('Read Line: ' & $line_read & @CRLF)

        If StringInStr($line_read, $sSearch) Then Return True
    WEnd
;~ $sSearch was not found
    Return False
EndFunc   ;==>_ReadLogFile

Func ArcGIS_Explorer($sMsgBox)
    MsgBox(0, 'Explorer found in log', $sMsgBox, 1)
    ;Load Map and check settings
EndFunc   ;==>ArcGIS_Explorer

Func ArcGis_Settings($sMsgBox)
    MsgBox(0, 'Explorer found in log', $sMsgBox, 1)
    ;Load Map and check settings
EndFunc   ;==>ArcGIS_Explorer

Would it be more practical to repeat the _ReadLogFile function for each one or create something that is dynamic for all function. I'm wondering whether an array to hold the different searches but somehow would reference the functions this way I won't have to list _ReadLogFile each time.

Func _ReadLogFile($sSearch = 'Explorer=true')

 

Edited by aa2zz6
Link to comment
Share on other sites

Sorry I meant what searches are you wanting to perform i.e. 

Explorer=True
Settings???=...

You can just use the function I created by changing the following, however if you have multiple search criteria we could put those into an array, if you let me know what the search criteria is I can try and put something together for you.

$Settings = _ReadLogFile('Settings=True')
Edited by Subz
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...