Jump to content

[SOLVED] Two equal functions, but different result...


Recommended Posts

  • Moderators

Dana,

Hence my comment about the construct being "an abomination" that "only works by pure luck". ;)

Why rely on the AutoIt parser when you can make it unambiguous with parentheses (do not agree that they are necessarily redundant) or by splitting the two statements (much better practice)? Booleans are tricky things and any coding shortcut is a recipe for disaster. Just make the code as simple as possible and there is much more chance of getting it right! :)

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

The problem with the OPs statement is that it should always result in $success being equal to False, as long as the RunWait doesn't error and/or return 0. Because $Folders[$i] is a string that, as long as the folder name isn't a number, is going to result in it being 0 when compared to the exit code of the RunWait. At least that's how I interpreted it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Dana,

Hence my comment about the construct being "an abomination" that "only works by pure luck". ;)

Why rely on the AutoIt parser when you can make it unambiguous with parentheses (do not agree that they are necessarily redundant) or by splitting the two statements (much better practice)? Booleans are tricky things and any coding shortcut is a recipe for disaster. Just make the code as simple as possible and there is much more chance of getting it right! :)

M23

Ok, but if i'm doing this:

For $i = 1 to UBound($Folders) -1
    If $Folders[$i] = RunWait(@ComSpec & " /c " & $pre & " " & $command & " " & $Password & " " & '"' & $Path & "" & $Folders[$i] & '"', @TempDir, @SW_HIDE) Then
  If GUICtrlRead($FolderCheckbox) = $GUI_CHECKED Then
  All_File_Delete_Crypt()
  EndIf
FileDelete(@TempDir & "Test.txt")
MsgBox(0,"Information","OK")
Next
Else
FileDelete(@TempDir & "Test.txt")
MsgBox(0,16,"Error","NO")
EndIf
Return 1
EndFunc

I have N MsgBox for every file checked by CMD, if i'm insert a Return under MsgBox i have 1 messages but only i file processed.

Find for me please another solution to this, i'm here to learn, and not create construct being "an abomination" :D

Edited by johnmcloud
Link to comment
Share on other sites

  • Moderators

johnmcloud,

You post some code that flows logically and I will try and rework it for you. At present that code has so many errors within the For..Next and If..Then...Else structures that it is difficult to see what the logic flow is supposed to be: :)

For $i = 1 To UBound($Folders) - 1
    If $Folders[$i] = RunWait(@ComSpec & " /c " & $pre & " " & $command & " " & $Password & " " & '"' & $Path & "" & $Folders[$i] & '"', @TempDir, @SW_HIDE) Then
        If GUICtrlRead($FolderCheckbox) = $GUI_CHECKED Then
            All_File_Delete_Crypt()
        EndIf
        FileDelete(@TempDir & "Test.txt")
        MsgBox(0, "Information", "OK")
;### Tidy Error -> "next" is closing previous "if" on line 2
    Next
;### Tidy Error -> "else" is closing previous "for" on line 1
Else
    FileDelete(@TempDir & "Test.txt")
    MsgBox(0, 16, "Error", "NO")
;### Tidy Error -> "endif" is closing previous "for" on line 1
EndIf
Return 1
;### Tidy Error: next line creates a negative tablevel.
;### Tidy Error: next line creates a negative tablevel for the line after it.
EndFunc   ;==>

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

johnmcloud,

You post some code that flows logically and I will try and rework it for you. At present that code has so many errors within the For..Next and If..Then...Else structures that it is difficult to see what the logic flow is supposed to be: :)

The logis is:

Array process in the directory the complete path of file, so do this path to cmd like

echo off

test.exe -e -p pathofthefile.txt

test.exe -e -p pathofthefile.txt

test.exe -e -p pathofthefile.txt

test.exe -e -p pathofthefile.txt

If command is right, ( and checkbox is thicked ) good + make a func() + deletetefile a file + msgbox

If command is right, ( and checkbox is NOT thicked ) good + deletetefile a file + msgbox

if something goes wrong, bad and deletetefile a file + msgbox ;)

Your code don't work, give me error for "Next" statement

Edited by johnmcloud
Link to comment
Share on other sites

  • Moderators

johnmcloud,

Of course "my" code does not work - it is just "your" code after Tidy had a look at the structure and showed the errors! :)

I will try and devise something for you this morning - but do not hold your breath as I have a lot on. ;)

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

johnmcloud,

Of course "my" code does not work - it is just "your" code after Tidy had a look at the structure and showed the errors! ;)

I will try and devise something for you this morning - but do not hold your breath as I have a lot on. :D

M23

Ok, thanks,

P.S My strange code work and don't give me error :) except many MsgBox ( 4 file processed? 4 MsgBox )

But i want to make good coding, so i'll wait, i'll really appreciate you work

Link to comment
Share on other sites

  • Moderators

johnmcloud,

What return value are you expecting from your RunWait command to decide between "good" and "bad"? :)

We need to know that to construct the conditional statement which follows. ;)

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

johnmcloud,

What return value are you expecting from your RunWait command to decide between "good" and "bad"? :)

We need to know that to construct the conditional statement which follows. ;)

M23

0 = Good

I know it's strange, but i'll test it in different script, for autoit and this specific software 0 is not error, other result different from 0 is error. Take a look at this example for single file:

$Path = "C:Test.txt"
$Pre = "C:Test.exe"
$Command = "-e -p"
  
$Test = RunWait(@ComSpec & " /c " & $pre & " " & $command & " " & '"' & $Path & '"', @TempDir, @SW_HIDE)

If $Test = 1 then
  MsgBox(16,"Good","Good")
Else
  MsgBox(0,"Error","Error")
EndIf

The result is MsgBox(0,"Error","Error")

$Path = "C:Test.txt"
$Pre = "C:CDModule.exe"
$Command = "-e -p"
$Password = 123

$Test = RunWait(@ComSpec & " /c " & $pre & " " & $command & " " & $Password & " " & '"' & $Path & '"', @TempDir, @SW_HIDE)

If $Test = 0 then
  MsgBox(16,"Good","Good")
Else
  MsgBox(0,"Error","Error")
EndIf

The result is MsgBox(16,"Good","Good"), so i have to do this script for single file

If RunWait(@ComSpec & " /c " & $pre & " " & $command & " " & $Password & " " & '"' & $Path & '"', @TempDir, @SW_HIDE) Then
   MsgBox(16,"Error","Error")
Else
   MsgBox(16,"Good","Good")

So Work=0 NotWork= 1.

I'm totally sure the command work becouse the software create a .log file with "success", but for autoit is error so i have I reversed the MsgBox. It's my first case, i never see something like that.

Edited by johnmcloud
Link to comment
Share on other sites

  • Moderators

johnmcloud,

I think this will work for you - it shows just the one MsgBox when all is done and even tells you which elements failed the test: ;)

; Create a flag
$fFlag = ""

For $i = 1 To UBound($Folders) - 1

    ; Do your cmd bit here and check if it works - you said a return of 0 = good
    If RunWait(@ComSpec & " /c " & $pre & " " & $command & " " & $Password & " " & '"' & $Path & "" & $Folders[$i] & '"', @TempDir, @SW_HIDE) = 0 Then
        ; If it worked then check the checkbox
        If GUICtrlRead($FolderCheckbox) = $GUI_CHECKED Then
            ; Run the function if it is checked
            All_File_Delete_Crypt()
        EndIf
    Else
        ; Add the error index to the flag if there was a failure
        $fFlag &= $i & "|"
    EndIf
    ; And here we delete the file
    FileDelete(@TempDir & "Test.txt")
    
Next

; Now we check to see if the flag is still clear
If $fFlag = "" Then
    ; The flag was not amended so all RunWaits ran successfully
    MsgBox(0, "Information", "OK")
Else
    ; The flag was set so at least one RunWait did not run successfully
    ; So which ones failed - split the flag into an array
    $aFailed = StringSplit($fFlag, "|")
    ; And loop through the array to get the names
    $sMsg = "The following elements failed:" & @CRLF & @CRLF
    For $i = 1 To $aFailed[0] - 1
        $sMsg &= $Folders[$aFailed[$i]] & @CRLF
    Next
    ; And now display them
    MsgBox(0, "Error", $sMsg)
EndIf

If it not quite what you want then let me know and we can tweak it a bit. :D

As to the return value "Work=0 NotWork=1" not matching the AutoIt standard - you can return anything you like from a function just as long as it is clear what each value means. AutoIt usually returns 1 for success, but there is no reason why this should be the case for everything else. :)

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

johnmcloud,

I think this will work for you - it shows just the one MsgBox when all is done and even tells you which elements failed the test: ;)

If it not quite what you want then let me know and we can tweak it a bit. :D

As to the return value "Work=0 NotWork=1" not matching the AutoIt standard - you can return anything you like from a function just as long as it is clear what each value means. AutoIt usually returns 1 for success, but there is no reason why this should be the case for everything else. :)

M23

Thanks Melba, i'll change a bit because i want the verify for checkbox only if result is =0

$fFlag = ""

For $i = 1 To UBound($Folders) - 1
    If RunWait(@ComSpec & " /c " & $pre & " " & $command & " " & $Password & " " & '"' & $Path & "" & $Folders[$i] & '"', @TempDir, @SW_HIDE) = 0 Then
    FileDelete(@TempDir & "Test.txt")
    Else
        $fFlag &= $i & "|"
    EndIf
    FileDelete(@TempDir & "Test.txt")
  
Next
If $fFlag = "" Then
If GUICtrlRead($FolderCheckbox) = $GUI_CHECKED Then
  Function()
EndIf
MsgBox(0, "Information","OK")
Else
    $aFailed = StringSplit($fFlag, "|")
    $sMsg = "Error:" & @CRLF & @CRLF
    For $i = 1 To $aFailed[0] - 1
        $sMsg &= $Folders[$aFailed[$i]] & @CRLF
    Next
    MsgBox(16, "Error", $sMsg)
EndIf

Only one think is not clear, the use of:

$fFlag = ""

$fFlag &= $i & "|"

If $fFlag = ""

Can you do a simple example for this? Thanks

Edited by johnmcloud
Link to comment
Share on other sites

  • Moderators

johnmcloud,

We start with the variable as an empty string:

$fFlag = ""

Each time the test fails, we add the index of the failed item to the string followed by a "|":

$fFlag &= $i & "|"

When the loop has finished, if nothing failed the string is still empty so we can announce success. If the string has any content, then we will have a list of the index number of the failures - like this:

"3|6|8|13|"

We can then use StringSplit to turn this into an array and loop through it to announce which elements failed.

Clearer now? :)

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

johnmcloud,

We start with the variable as an empty string:

$fFlag = ""

Each time the test fails, we add the index of the failed item to the string followed by a "|":

$fFlag &= $i & "|"

When the loop has finished, if nothing failed the string is still empty so we can announce success. If the string has any content, then we will have a list of the index number of the failures - like this:

"3|6|8|13|"

We can then use StringSplit to turn this into an array and loop through it to announce which elements failed.

Clearer now? :)

M23

Yes, all clear thanks ;)

Link to comment
Share on other sites

  • Moderators

johnmcloud,

One last thing - please use the "Reply to this topic" button at the top of the page or the "Reply to this topic" editbox at the bottom. If you use the "Quote" button as you are currently doing the thread becomes double the size and difficult to follow. :)

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

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