Jump to content

[SOLVED] _StringBetween @error


Recommended Posts

Hi guys, i have a little problem to set the @error to _StringBetween

The script:

While 1
  $line = StdoutRead($CMDCommand)
  If @error Then ExitLoop
  $tmp = _StringBetween($line, "] ", '%')
  If Not @error Then
   $replace = StringReplace($tmp[0], "%", "")
   MsgBox(64, "Test", "string found")
  EndIf
WEnd

This script working, but i need to set something if the string don't match. I have try like this:

While 1
  $line = StdoutRead($CMDCommand)
  If @error Then ExitLoop
  $tmp = _StringBetween($line, "] ", '%')
  If Not @error Then
   $replace = StringReplace($tmp[0], "%", "")
   MsgBox(64, "Test", "string found")
  Else
   MsgBox(17, "Test", "no string found")
  EndIf
WEnd

But i have everytime the error MsgBox also if the string is found. Some advice?

Thanks for support

Edited by johnmcloud
Link to comment
Share on other sites

  • Moderators

johnmcloud,

Your logic works for me. In this example I have just reversed the order of the test - why use Not if you do not have to? :)

#include <String.au3>

Global $aLines[3] = ["works] 34%", "Fails", "works] 57%"]

For $i = 0 To 2
    $sLine = $aLines[$i]
    $aFound = _StringBetween($sLine, "] ", '%')
    If @error Then
        MsgBox(0, "Test " & $i, "no string found")
    Else
        $sReplace = StringReplace($aFound[0], "%", "")
        MsgBox(0, "Test " & $i, "string found" & @CRLF & $aFound[0] & @CRLF & $sReplace)
    EndIf
Next

And why do you use the StringReplace line? As you can see the & does not form part of the string returned by _StringBetween. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Melba, also with your example i have @error MsgBox and working at the same time...

Test 0no string foundTest 1no string foundTest 2no string foundTest 0no string foundTest 1no string foundTest 2no string foundTest 0no string foundTest

And this is the stdoutread at first check:

[download]   0.0% of 3.52M at    ---b/s ETA --:--
[download]   0.1% of 3.52M at    ---b/s ETA --:--
[download]   0.2% of 3.52M at  116.67k/s ETA 00:30
[download]   0.4% of 3.52M at  187.50k/s ETA 00:19
[download]   0.9% of 3.52M at  221.43k/s ETA 00:16
[download]   1.7% of 3.52M at  273.91k/s ETA 00:12
[download]   3.5% of 3.52M at  352.78k/s ETA 00:09
[download]   7.1% of 3.52M at  425.00k/s ETA 00:07
[download]  14.2% of 3.52M at  468.38k/s ETA 00:06

I have this only if file is valid, for this i want to set an @error if don't find string ] to %

Thanks for help

Edited by johnmcloud
Link to comment
Share on other sites

  • Moderators

johnmcloud,

So when you run that exact code I posted you it always says it found "no string"? :)

Have you recoded your _StringBetween function at all? ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Yes, your code:

#include <String.au3>

Global $aLines[3] = ["works] 34%", "Fails", "works] 57%"]
For $i = 0 To 2
    $sLine = $aLines[$i]
    $aFound = _StringBetween($sLine, "] ", '%')
    If @error Then
        ConsoleWrite("Test " & $i & "no string found")
    Else
        $sReplace = StringReplace($aFound[0], "%", "")
        ConsoleWrite("Test " & $i & "string found" & @CRLF & $aFound[0] & @CRLF & $sReplace)
    EndIf
Next

The result:

Test 0string found
34
34Test 1no string foundTest 2string found
57
57+>10:22:19 AutoIT3.exe ended.rc:0
>Exit code: 0   Time: 1.947
Edited by johnmcloud
Link to comment
Share on other sites

  • Moderators

johnmcloud,

Using your data also works fine for me:

#include <String.au3>

Global $aLines[11] = ["[download]   0.0% of 3.52M at    ---b/s ETA --:--", _
                    "[download]   0.1% of 3.52M at    ---b/s ETA --:--", _
                    "[download]   0.2% of 3.52M at  116.67k/s ETA 00:30", _
                    "[download]   0.4% of 3.52M at  187.50k/s ETA 00:19", _
                    "fred", _
                    "[download]   0.9% of 3.52M at  221.43k/s ETA 00:16", _
                    "[download]   1.7% of 3.52M at  273.91k/s ETA 00:12", _
                    "[download]   3.5% of 3.52M at  352.78k/s ETA 00:09", _
                    "fred", _
                    "[download]   7.1% of 3.52M at  425.00k/s ETA 00:07", _
                    "[download]  14.2% of 3.52M at  468.38k/s ETA 00:06"]

For $i = 0 To UBound($aLines) - 1
    $sLine = $aLines[$i]
    $aFound = _StringBetween($sLine, "] ", '%')
    If @error Then
        ConsoleWrite($i & " - " & "No string" & @CRLF)
        ;MsgBox(0, "Test " & $i, "no string found")
    Else
        ConsoleWrite($i & " - " & $aFound[0] & @CRLF)
        ;MsgBox(0, "Test " & $i, "string found" & @CRLF & $aFound[0] & @CRLF & $sReplace)
    EndIf
Next

Result:

0 -   0.0
1 -   0.1
2 -   0.2
3 -   0.4
4 - No string
5 -   0.9
6 -   1.7
7 -   3.5
8 - No string
9 -   7.1
10 -  14.2

How about when you run it? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

$aLines = StdoutRead($CMDCommand)
  For $i = 0 To UBound($aLines) - 1
   $sLine = $aLines[$i]
   Global $aFound = _StringBetween($sLine, "] ", '%')
   If @error Then
    ConsoleWrite($i & " - " & "No string" & @CRLF)
    ;MsgBox(0, "Test " & $i, "no string found")
   Else
    ConsoleWrite($i & " - " & $aFound[0] & @CRLF)
    ;MsgBox(0, "Test " & $i, "string found" & @CRLF & $aFound[0] & @CRLF & $sReplace)
   EndIf
  Next
  If $fInterrupt <> 0 Then ; Stop Loop
   Stop_Download()
   MsgBox(16, "Information", "Interrupt")
   Return
  EndIf
EndFunc

Seems not working on my script, you script working. I'm sure something miss, what i'm doing wrong?

Edited by johnmcloud
Link to comment
Share on other sites

  • Moderators

johnmcloud,

That will not work as you do not get an array returned from StdoutRead. I only used the For...Next loop as an example. ;)

Perhaps this:

Global $aFound

While 1
    $line = StdoutRead($CMDCommand)
    If @error Then ExitLoop
    $aFound = _StringBetween($sLine, "] ", '%')
    If @error Then
        ConsoleWrite($i & " - " & "No string" & @CRLF)
        ;MsgBox(0, "Test " & $i, "no string found")
    Else
        ConsoleWrite($i & " - " & $aFound[0] & @CRLF)
        ;MsgBox(0, "Test " & $i, "string found" & @CRLF & $aFound[0] & @CRLF & $sReplace)
    EndIf

    If $fInterrupt <> 0 Then ; Stop Loop
        Stop_Download()
        MsgBox(16, "Information", "Interrupt")
        Return
    EndIf
    
WEnd

Any luck? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

johnmcloud,

Run this so we can see exactly what you actually get from the stream:

Global $aFound

While 1
    $line = StdoutRead($CMDCommand)
    If @error Then ExitLoop
    $aFound = _StringBetween($sLine, "] ", '%')
    If @error Then
        ConsoleWrite($i & " - " & $sLine & " = " & "No string" & @CRLF)
        ;MsgBox(0, "Test " & $i, "no string found")
    Else
        ConsoleWrite($i & " - " & $sLine & " = " & $aFound[0] & @CRLF)
        ;MsgBox(0, "Test " & $i, "string found" & @CRLF & $aFound[0] & @CRLF & $sReplace)
    EndIf

    If $fInterrupt <> 0 Then ; Stop Loop
        Stop_Download()
        MsgBox(16, "Information", "Interrupt")
        Return
    EndIf

WEnd

Now we will check exactly what you are getting and whether the function should return something. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

An infinite number of - NoString

P.S I have error for $i, so i have removed them

Normal working output:

[software] Setting language
[software] ktHwS3yUr4U: Downloading video webpage
[software] ktHwS3yUr4U: Downloading video info webpage
[software] ktHwS3yUr4U: Extracting video information
[download] Destination: ktHwS3yUr4U.mp4
[download] 0.0% of 3.52M at   90.91k/s ETA 00:39
[download] 0.1% of 3.52M at  272.72k/s ETA 00:13
[download] 0.2% of 3.52M at   98.59k/s ETA 00:36
[download] 0.9% of 3.52M at  205.30k/s ETA 00:17

When don't work

[software] Setting language
[software] asdsdadsa: Downloading video webpage
ERROR: unable to download video webpage: HTTP Error 404: Not Found
Edited by johnmcloud
Link to comment
Share on other sites

  • Moderators

johnmcloud,

Sorry about the $i - but as I cannot test for the same input as you.... ;)

What did you actually get in the console? The output line should include the actual line we are using as well as "No string". If there was nothing in the line then you cannot expect to find anything within it. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

I'll post the entire code, so you can test it, if you want:

"Script is broken, i'll post only if full working"

Mine is only a test, i have many other command-line software, the principle is always the same

Edited by johnmcloud
Link to comment
Share on other sites

Guys, i think i can figure out how resolve the problem, but i need another little information. I can't write on a txt the log of a stderrRead, what is the problem?

Local $txt = @WorkingDir & "log.txt"
Local $txtread = FileRead($ini)

$Error = StderrRead($CMDCommand)
$logCreate = FileOpen($txt, 2)
FileWrite($logCreate, $Error)
FileClose($txt)

The file is empty, but on consolewrite i see a result, so what is my error? Thanks for support

EDIT: Solved adding a Dim on all $var

Edited by johnmcloud
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

×
×
  • Create New...