Solved

Exiting a loop without quitting Autoit

Posted on 2011-03-22
23
6,276 Views
Last Modified: 2013-11-13
Hello

I have a function in my code to allow the user to change passwords.  If the user changes passwords it works fine.  If however they press Cancel the program quits:

Func _ChangePW()
    If $new = 0 Then
        $bLoop = 1
        While $bLoop = 1
            $text = InputBox("Meerecat Folder Lock", "Please type your password and click OK.", "", "*")
            If @error = 1 Then Exit
            ; They clicked OK, but did they type the right thing?
            If $text <> IniRead($ini, "Password", "Password", "failed") Then
                MsgBox(4096, "Error", "Incorrect Password - try again!")
            Else
                $bLoop = 0 ; Exit the loop - ExitLoop would have been an alternative too :)
            EndIf
        WEnd
    EndIf
    $pw1 = InputBox("Meerecat Folder Lock", "Please create a new password and click OK.", "", "*")
    If @error = 1 Then Exit
    $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*")
    If @error = 1 Then Exit
    While $pw1 <> $pw2
        $pw1 = InputBox("Meerecat Folder Lock", "Passwords do not match or are blank. Please try again.", "", "*")
        If @error = 1 Then Exit
        $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*")
        If @error = 1 Then Exit
        If $pw2 = "" Then $pw2 = "ThisPreventsBlankPasswords"
    WEnd
    IniWrite($ini, "Password", "Password", $pw2)
    MsgBox(0 + 64, "Meerecat Folder Lock", "Your new password has been set.")
EndFunc

Open in new window


I have tried changing this line

        If @error = 1 Then Exit

Open in new window

to
If @error = 1 then _main()

Open in new window


This works and calls the main function, but it still brings up an invalid password message box.  I can't break out of this loop and have to terminate the program.

Can anybody suggest how to change this?

Many Thanks
0
Comment
Question by:JakeSpencer
  • 8
  • 7
  • 6
  • +1
23 Comments
 
LVL 10

Expert Comment

by:khan_webguru
ID: 35187147
Hello Bro,

In which language you are developing this logic???


Regards,

Asif Ahmed Khan
0
 

Author Comment

by:JakeSpencer
ID: 35187237
Hello

It is in AutoIT

Thanks
0
 
LVL 10

Expert Comment

by:khan_webguru
ID: 35187620
Hello Bro!

You can do this by Following statement.

 
ExitLoop

Open in new window


Or you can use another way that is written below:

 
Func Button1()
    $iLoop = True
    
    While $iLoop
        StealMedium()
        If Not $iLoop Then ExitLoop
        
        StealAuto()
        If Not $iLoop Then ExitLoop
        
        DrivingLicense()
        If Not $iLoop Then ExitLoop
        
        Shootingrange()
        If Not $iLoop Then ExitLoop
    WEnd
    
    $iLoop = False
EndFunc

Func _ExitLoop()
    $iLoop = False
EndFunc

Open in new window


Hope this will help you to solve your problem.

Regards,

Asif Ahmed Khan
0
 

Author Comment

by:JakeSpencer
ID: 35187706
Hello

How can I incorporate this into the code I already have?

Sorry if this is a stupid question, but I am new to AutoIT.

Many Thanks
0
 
LVL 10

Expert Comment

by:khan_webguru
ID: 35187743
Hello Bro!

Your code is modified just copy paste that and replace your code by this try both code one by one and see which give you desire result and go for that.


Func _ChangePW()
    If $new = 0 Then
        $bLoop = 1
        While $bLoop = 1
            $text = InputBox("Meerecat Folder Lock", "Please type your password and click OK.", "", "*")
            If @error = 1 Then ExitLoop
            ; They clicked OK, but did they type the right thing?
            If $text <> IniRead($ini, "Password", "Password", "failed") Then
                MsgBox(4096, "Error", "Incorrect Password - try again!")
            Else
                ExitLoop ; Exit the loop - ExitLoop would have been an alternative too :)
            EndIf
        WEnd
    EndIf
    $pw1 = InputBox("Meerecat Folder Lock", "Please create a new password and click OK.", "", "*")
    If @error = 1 Then ExitLoop
    $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*")
    If @error = 1 Then ExitLoop
    While $pw1 <> $pw2
        $pw1 = InputBox("Meerecat Folder Lock", "Passwords do not match or are blank. Please try again.", "", "*")
        If @error = 1 Then ExitLoop
        $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*")
        If @error = 1 Then ExitLoop
        If $pw2 = "" Then $pw2 = "ThisPreventsBlankPasswords"
    WEnd
    IniWrite($ini, "Password", "Password", $pw2)
    MsgBox(0 + 64, "Meerecat Folder Lock", "Your new password has been set.")
EndFunc

Open in new window



OR



 
Func _ChangePW()
    If $new = 0 Then
        $bLoop = 1
        While $bLoop = 1
            $text = InputBox("Meerecat Folder Lock", "Please type your password and click OK.", "", "*")
            If @error = 1 Then ExitLoop
            ; They clicked OK, but did they type the right thing?
            If $text <> IniRead($ini, "Password", "Password", "failed") Then
                MsgBox(4096, "Error", "Incorrect Password - try again!")
            Else
                ExitLoop ; Exit the loop - ExitLoop would have been an alternative too :)
            EndIf
        WEnd
    EndIf
    $pw1 = InputBox("Meerecat Folder Lock", "Please create a new password and click OK.", "", "*")
    If @error = 1 Then ExitLoop
    $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*")
    If @error = 1 Then ExitLoop
    While $pw1 <> $pw2
        $pw1 = InputBox("Meerecat Folder Lock", "Passwords do not match or are blank. Please try again.", "", "*")
        If @error = 1 Then ExitLoop
        $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*")
        If @error = 1 Then ExitLoop
        If $pw2 = "" Then $pw2 = "ThisPreventsBlankPasswords"
    WEnd
    IniWrite($ini, "Password", "Password", $pw2)
    MsgBox(0 + 64, "Meerecat Folder Lock", "Your new password has been set.")
EndFunc

Func _ExitLoop()
    $iLoop = False
EndFunc

Open in new window




Hope this will help you.

Regards,

Asif Ahmed Khan
0
 

Author Comment

by:JakeSpencer
ID: 35187956
Many thanks for your help, but it doesn't work.  It gives the following error:

ExitLoop/ContinueLoop" statements only valid from inside a For/Do/While loop.:
0
 
LVL 10

Expert Comment

by:khan_webguru
ID: 35188010
Hello Bro!

I know the problem please use the below modified versions any of them that suits you.

 
Func _ChangePW()
    If $new = 0 Then
        $bLoop = 1
        While $bLoop = 1
            $text = InputBox("Meerecat Folder Lock", "Please type your password and click OK.", "", "*")
            If @error = 1 Then ExitLoop
            ; They clicked OK, but did they type the right thing?
            If $text <> IniRead($ini, "Password", "Password", "failed") Then
                MsgBox(4096, "Error", "Incorrect Password - try again!")
            Else
                ExitLoop ; Exit the loop - ExitLoop would have been an alternative too :)
            EndIf
        WEnd
    EndIf
    $pw1 = InputBox("Meerecat Folder Lock", "Please create a new password and click OK.", "", "*")
    If @error = 1 Then Exit
    $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*")
    If @error = 1 Then Exit
    While $pw1 <> $pw2
        $pw1 = InputBox("Meerecat Folder Lock", "Passwords do not match or are blank. Please try again.", "", "*")
        If @error = 1 Then ExitLoop
        $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*")
        If @error = 1 Then ExitLoop
        If $pw2 = "" Then $pw2 = "ThisPreventsBlankPasswords"
    WEnd
    IniWrite($ini, "Password", "Password", $pw2)
    MsgBox(0 + 64, "Meerecat Folder Lock", "Your new password has been set.")
EndFunc

Open in new window


OR

 
Func _ChangePW()
    If $new = 0 Then
        $bLoop = 1
        While $bLoop = 1
            $text = InputBox("Meerecat Folder Lock", "Please type your password and click OK.", "", "*")
            If @error = 1 Then ExitLoop
            ; They clicked OK, but did they type the right thing?
            If $text <> IniRead($ini, "Password", "Password", "failed") Then
                MsgBox(4096, "Error", "Incorrect Password - try again!")
            Else
                ExitLoop ; Exit the loop - ExitLoop would have been an alternative too :)
            EndIf
        WEnd
    EndIf
    $pw1 = InputBox("Meerecat Folder Lock", "Please create a new password and click OK.", "", "*")
    If @error = 1 Then Exit
    $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*")
    If @error = 1 Then Exit
    While $pw1 <> $pw2
        $pw1 = InputBox("Meerecat Folder Lock", "Passwords do not match or are blank. Please try again.", "", "*")
        If @error = 1 Then ExitLoop
        $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*")
        If @error = 1 Then ExitLoop
        If $pw2 = "" Then $pw2 = "ThisPreventsBlankPasswords"
    WEnd
    IniWrite($ini, "Password", "Password", $pw2)
    MsgBox(0 + 64, "Meerecat Folder Lock", "Your new password has been set.")
EndFunc


Func _ExitLoop()
    $iLoop = False
EndFunc

Open in new window



If you still find this error then perform following action

1- Restore you own means previous code that is written by you.
2- On the place where you want to break the loop just add the following line

 
ExitLoop

Open in new window


3- Keep one thing in your mind that (If @error = 1 Then Exit) this exit means EXIT FROM IF STATEMENT not from loop.

4- So, If you want to EXIT FROM LOOP after this statement TRUE the you should write like this

 
If @error = 1 Then
   ExitLoop
EndIf

Open in new window


That all from my side and thats what can I do for you. Now I am also busy in my shipment to client so if I will respond late saying SORRY in advance.

I hope your problem will solve now.

Best Regards,

Asif Ahmed Khan
0
 

Author Comment

by:JakeSpencer
ID: 35188571
Using either of the above examples, it still closes the program when pressing cancel on that dialogue.  

Thanks for your help anyway.
0
 
LVL 10

Expert Comment

by:khan_webguru
ID: 35188575
My pleasure

Regards,

Asif Ahmed Khan
0
 
LVL 11

Accepted Solution

by:
TheGorby earned 250 total points
ID: 35189908
Hi JakeSpencer! That code looks familiar :)

See if this doesn't do the trick:
Func _ChangePW() 
    If $new = 0 Then 
        $bLoop = 1 
        While $bLoop = 1 
            $text = InputBox("Meerecat Folder Lock", "Please type your password and click OK.", "", "*") 
            If @error = 1 Then Exit 
            ; They clicked OK, but did they type the right thing? 
            If $text <> IniRead($ini, "Password", "Password", "failed") Then 
                MsgBox(4096, "Error", "Incorrect Password - try again!") 
            Else 
                $bLoop = 0 ; Exit the loop - ExitLoop would have been an alternative too :) 
            EndIf 
        WEnd 
    EndIf 
    $pw1 = InputBox("Meerecat Folder Lock", "Please create a new password and click OK.", "", "*") 
    If @error = 1 Then
        $canceled=1
        $pw1="canceled"
        $pw2=$pw1
    EndIf
    If $canceled<>1 Then
        $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*") 
        If @error = 1 Then
            $pw2=$pw1
            $canceled=1
        EndIf
    EndIf
    While $pw1 <> $pw2 
        $pw1 = InputBox("Meerecat Folder Lock", "Passwords do not match or are blank. Please try again.", "", "*") 
        If @error = 1 Then
            $canceled=1
	   $pw1="canceled"
	   $pw2=$pw1
        EndIf
        If $canceled<>1 Then
	   $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*") 
	   If @error = 1 Then
	       $pw2=$pw1
	       $canceled=1
	   Else
	       If $pw2 = "" Then $pw2 = "ThisPreventsBlankPasswords" 
	   EndIf
        EndIf
    WEnd 
    If $canceled<>1 Then
        IniWrite($ini, "Password", "Password", $pw2) 
        MsgBox(0 + 64, "Meerecat Folder Lock", "Your new password has been set.") 
    Else
        MsgBox(0 + 64, "Meerecat Folder Lock", "Password creation was canceled.")
    EndIf
EndFunc

Open in new window

0
 
LVL 11

Expert Comment

by:TheGorby
ID: 35190109
Some things I'd also like to clear up from other comments on this question, since you're still learning AutoIT;

"3- Keep one thing in your mind that (If @error = 1 Then Exit) this exit means EXIT FROM IF STATEMENT not from loop."

That may be true in other programming languages, but in AutoIT using the 'Exit' statement will exit from the entire script, no matter where it's used.

"4- So, If you want to EXIT FROM LOOP after this statement TRUE the you should write like this"

Since 'If...Then...EndIf' statements aren't loops, you cannot use ExitLoop within them (as you found out). ExitLoop can only be used in the following statements:

Do...Until
For...To...Step...Next
For...In...Next
While...WEnd

It stops running a loop even if the conditions for running the loop still exist. For example
While $pw1<>$pw2
    $pw1="test"
    $pw2="notequal"
    ExitLoop
WEnd

Open in new window


This loop would be never-ending, but since the ExitLoop statement was used it will only run/loop once and then continue with the rest of the script.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:TheGorby
ID: 35192621
Just noticed I missed a section! The only changes are on lines 6-9 and 18
Func _ChangePW()  
    If $new = 0 Then  
        $bLoop = 1  
        While $bLoop = 1  
            $text = InputBox("Meerecat Folder Lock", "Please type your password and click OK.", "", "*")  
            If @error = 1 Then
                $canceled=1
                ExitLoop
            EndIf  
            ; They clicked OK, but did they type the right thing?  
            If $text <> IniRead($ini, "Password", "Password", "failed") Then  
                MsgBox(4096, "Error", "Incorrect Password - try again!")  
            Else  
                $bLoop = 0 ; Exit the loop - ExitLoop would have been an alternative too :)  
            EndIf  
        WEnd  
    EndIf  
    If $canceled <> 1 Then $pw1 = InputBox("Meerecat Folder Lock", "Please create a new password and click OK.", "", "*")

Open in new window

0
 
LVL 11

Expert Comment

by:TheGorby
ID: 35192636
$%&@#!!! Please insert this in between lines 1 and 2 in addition to the changes on my previous comment.
$canceled = 0

Open in new window

0
 

Author Comment

by:JakeSpencer
ID: 35194188
Hello TheGorby

Yes that's your code :)

It kind of works, but not.

If I press Change Password, then Cancel straight away, it prompts me to create a NEW password.  If I Cancel this it then goes back to the _Advanced() function as expected.

If I cancel then type anything at all into the create NEW password dialogue then the program crashes complaining of a variable not being declared.

If I dont cancel the initial dialogue, but type in my password it prompts for a new password, then prompts for a new password again (the same dialogue), then asks me to confirm this password.

Does that all make sense?

I have posted the new function below in case I missed or messed up any of your changes.  I have also attached the full compiled program so you can test to see what I mean.  You will need to unzip it first.

Many Thanks

Func _ChangePW() 
	$canceled = 0 
    If $new = 0 Then 
        $bLoop = 1 
        While $bLoop = 1 
            $text = InputBox("Meerecat Folder Lock", "Please type your password and click OK.", "", "*") 
            If @error = 1 Then
                $canceled=1
                ExitLoop
            EndIf  
 
            ; They clicked OK, but did they type the right thing? 
            If $text <> IniRead($ini, "Password", "Password", "failed") Then 
                MsgBox(4096, "Error", "Incorrect Password - try again!") 
            Else 
                $bLoop = 0 ; Exit the loop - ExitLoop would have been an alternative too :) 
            EndIf 
        WEnd 
    EndIf 
	    If $canceled <> 1 Then $pw1 = InputBox("Meerecat Folder Lock", "Please create a new password and click OK.", "", "*")
    $pw1 = InputBox("Meerecat Folder Lock", "Please create a new password and click OK.", "", "*") 
    If @error = 1 Then
        $canceled=1
        $pw1="canceled"
        $pw2=$pw1
    EndIf
    If $canceled<>1 Then
        $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*") 
        If @error = 1 Then
            $pw2=$pw1
            $canceled=1
        EndIf
    EndIf
    While $pw1 <> $pw2 
        $pw1 = InputBox("Meerecat Folder Lock", "Passwords do not match or are blank. Please try again.", "", "*") 
        If @error = 1 Then
            $canceled=1
	   $pw1="canceled"
	   $pw2=$pw1
        EndIf
        If $canceled<>1 Then
	   $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*") 
	   If @error = 1 Then
	       $pw2=$pw1
	       $canceled=1
	   Else
	       If $pw2 = "" Then $pw2 = "ThisPreventsBlankPasswords" 
	   EndIf
        EndIf
    WEnd 
    If $canceled<>1 Then
        IniWrite($ini, "Password", "Password", $pw2) 
        MsgBox(0 + 64, "Meerecat Folder Lock", "Your new password has been set.") 
    Else
        MsgBox(0 + 64, "Meerecat Folder Lock", "Password creation was cancelled.")
    EndIf
EndFunc

Open in new window



Folder-Lock-v2.zip
0
 
LVL 11

Expert Comment

by:TheGorby
ID: 35194244
To make it easier, could you post the code for the entire program? Especially since I don't remember any function named _Advanced() so this way I can be sure any changes I make won't clash with your changes.
0
 

Author Comment

by:JakeSpencer
ID: 35194401
Here we go, many Thanks
#NoTrayIcon
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

Global $ini = @AppDataDir & "\Meerecat\FolderLock\FldLock.ini"
Global $pic = @AppDataDir & "\Meerecat\FolderLock"
Global $dir = @MyDocumentsDir & "\My Secure Folder"
If Not FileExists($pic) Then
    DirCreate($pic)
EndIf
FileInstall ("C:\Users\Sienna X\Desktop\AutoIT Scripts\meerekat.jpg", $pic & "\meerekat.jpg")

Opt("GUIOnEventMode", 1); =========================================================option only needs to be here
If Not FileExists($ini) Then
    $hnd = FileOpen($ini, 9)
    FileWrite($hnd, "[Password]" & @CRLF)
    FileWriteLine($hnd, "Password=")
    FileClose($hnd)
    MsgBox(0 + 64, "Meerecat Folder Lock", "This is your first time running Folder Lock on this PC" & @LF & @LF & "You will now be prompted to create a password.")
    $new = 1
    _ChangePW()
EndIf
If Not FileExists($dir) Then
    DirCreate($dir)
    MsgBox(0 + 64, "Meerecat Folder Lock", "Your confidential folder has been created:" & @LF & @LF & "You will now find a folder in your Documents area called My Secure Folder.")
EndIf
$bLoop = 1
While $bLoop = 1
    $text = InputBox("Meerecat Folder Lock", "Please type your password and click OK.", "", "*")
    If @error = 1 Then Exit
    ; They clicked OK, but did they type the right thing?
    If $text <> IniRead($ini, "Password", "Password", "failed") Then
        MsgBox(4096, "Error", "Incorrect Password - try again!")
    Else
        $bLoop = 0 ; Exit the loop - ExitLoop would have been an alternative too :)
    EndIf
WEnd
$new = 0

_main(); Moved this, autoit3 checks all functions before anything else, (they are declared first noi matter where they are)

While 1
    Sleep(1000) ; =======================================================================while loop here.
WEnd

Func _main()
    ; ==================================================================================== removed option
    GUIDelete()
	$prompt = GUICreate("Meerecat Folder Lock", 581, 222, 192, 124)
    GUICtrlCreateLabel("Meerecat Simple Software © 2011", 408, 200, 167, 17)
    GUICtrlCreateLabel("Welcome to Meerecat Folder Lock", 215, 24, 294, 28)
    GUICtrlSetFont(-1, 16, 400, 0, "MS Sans Serif")
    GUICtrlCreateLabel("Please choose an option below", 247, 57, 223, 24)
	GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlCreatePic($pic & "\meerekat.jpg", 0, 0, 137, 217)
    GUISetBkColor(0xFBFBFB)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    $lock = GUICtrlCreateButton("Lock Folder", 152, 112, 120, 33)
    $unlock = GUICtrlCreateButton("Unlock Folder", 296, 112, 120, 33)
    $advanced = GUICtrlCreateButton("Advanced Options", 440, 112, 120, 33)
	$open = GUICtrlCreateButton("Open Secure Folder", 296, 152, 120, 33)
    $exit = GUICtrlCreateButton("Exit Program", 440, 152, 120, 33)
    GUICtrlSetOnEvent($lock, "_Lock")
    GUICtrlSetOnEvent($unlock, "_Unlock")
    GUICtrlSetOnEvent($advanced, "_Advanced")
	GUICtrlSetOnEvent($open, "_Open")
    GUICtrlSetOnEvent($exit, "_Exit")
    GUISetState(@SW_SHOW)
	    ;==================================================================================== moved while loop
EndFunc   ;==>_main



Func _Advanced()
    ; ==================================================================================== removed option
    GUIDelete()
	$prompt = GUICreate("Meerecat Folder Lock", 581, 222, 192, 124)
    GUICtrlCreateLabel("Meerecat Simple Software © 2011", 408, 200, 167, 17)
    GUICtrlCreateLabel("Meerecat Folder Lock", 263, 24, 188, 28)
    GUICtrlSetFont(-1, 16, 400, 0, "MS Sans Serif")
    GUICtrlCreateLabel("Advanced Options", 287, 57, 134, 24)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlCreatePic($pic & "\meerekat.jpg", 0, 0, 137, 217)
    GUISetBkColor(0xFBFBFB)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    $chgpass = GUICtrlCreateButton("Change Password", 152, 112, 120, 33)
    $delete = GUICtrlCreateButton("Delete Folder", 296, 112, 120, 33)
    $back = GUICtrlCreateButton("Go Back", 440, 112, 120, 33)
    GUICtrlSetOnEvent($chgpass, "_ChangePW")
    GUICtrlSetOnEvent($delete, "_Delete")
    GUICtrlSetOnEvent($back, "_Main")
    GUISetState(@SW_SHOW)
    ;============================================== Removed while loop
EndFunc   ;==>_Advanced

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _ChangePW() 
	$canceled = 0 
    If $new = 0 Then 
        $bLoop = 1 
        While $bLoop = 1 
            $text = InputBox("Meerecat Folder Lock", "Please type your password and click OK.", "", "*") 
            If @error = 1 Then
                $canceled=1
                ExitLoop
            EndIf  
 
            ; They clicked OK, but did they type the right thing? 
            If $text <> IniRead($ini, "Password", "Password", "failed") Then 
                MsgBox(4096, "Error", "Incorrect Password - try again!") 
            Else 
                $bLoop = 0 ; Exit the loop - ExitLoop would have been an alternative too :) 
            EndIf 
        WEnd 
    EndIf 
	    If $canceled <> 1 Then $pw1 = InputBox("Meerecat Folder Lock", "Please create a new password and click OK.", "", "*")
    $pw1 = InputBox("Meerecat Folder Lock", "Please create a new password and click OK.", "", "*") 
    If @error = 1 Then
        $canceled=1
        $pw1="canceled"
        $pw2=$pw1
    EndIf
    If $canceled<>1 Then
        $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*") 
        If @error = 1 Then
            $pw2=$pw1
            $canceled=1
        EndIf
    EndIf
    While $pw1 <> $pw2 
        $pw1 = InputBox("Meerecat Folder Lock", "Passwords do not match or are blank. Please try again.", "", "*") 
        If @error = 1 Then
            $canceled=1
	   $pw1="canceled"
	   $pw2=$pw1
        EndIf
        If $canceled<>1 Then
	   $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*") 
	   If @error = 1 Then
	       $pw2=$pw1
	       $canceled=1
	   Else
	       If $pw2 = "" Then $pw2 = "ThisPreventsBlankPasswords" 
	   EndIf
        EndIf
    WEnd 
    If $canceled<>1 Then
        IniWrite($ini, "Password", "Password", $pw2) 
        MsgBox(0 + 64, "Meerecat Folder Lock", "Your new password has been set.") 
    Else
        MsgBox(0 + 64, "Meerecat Folder Lock", "Password creation was cancelled.")
    EndIf
EndFunc

Func _Delete()
$answer=MsgBox(1 + 48,"Meerecat Folder Lock","This will delete your secure folder.  This action CANNOT be undone." & @CRLF & "To continue press OK otherwise press Cancel")
	; Check the user's answer to the prompt (see the help file for MsgBox return values)
	Switch $answer
	Case 1
	RunWait(@ComSpec & ' /c cacls "' & $dir & '" /e /c /g ' & @UserName & ":f",@UserProfileDir,@SW_HIDE)
    RunWait(@ComSpec & ' /c attrib -s -h -r "' & $dir & '"',@UserProfileDir,@SW_HIDE)
	DirRemove ($dir)
			MsgBox(0 + 64,"Meerecat Folder Lock","Your secure folder has been removed. You will need to restart the program to recreate it.")
		Case 2
			_main()
	EndSwitch
	
EndFunc
Func _Lock()
	RunWait(@ComSpec & ' /c attrib +s +h +r "' & $dir & '"',@UserProfileDir,@SW_HIDE)
RunWait(@ComSpec & ' /c cacls "' & $dir & '" /e /c /d ' & @UserName,@UserProfileDir,@SW_HIDE)
			MsgBox(0 + 64,"Meerecat Folder Lock","Your folder has been LOCKED")
EndFunc
		
Func _Unlock()
	RunWait(@ComSpec & ' /c cacls "' & $dir & '" /e /c /g ' & @UserName & ":f",@UserProfileDir,@SW_HIDE)
    RunWait(@ComSpec & ' /c attrib -s -h -r "' & $dir & '"',@UserProfileDir,@SW_HIDE)
			MsgBox(0 + 64,"Meerecat Folder Lock","Your folder has been UNLOCKED")
		EndFunc			
		
Func _Open()
		RunWait(@ComSpec & ' /c cacls "' & $dir & '" /e /c /g ' & @UserName & ":f",@UserProfileDir,@SW_HIDE)
    RunWait(@ComSpec & ' /c attrib -s -h -r "' & $dir & '"',@UserProfileDir,@SW_HIDE)
ShellExecute($dir)
EndFunc

Open in new window

0
 
LVL 10

Expert Comment

by:khan_webguru
ID: 35196223
Hello Bro!

See my previous communication/Post with you I did not find another way just found ExitLoop and suggested that. Hope your problem has been solved.

Regards,

Asif Ahmed Khan
0
 
LVL 29

Assisted Solution

by:matrixnz
matrixnz earned 250 total points
ID: 35198022
Try this

I've basically used standard gui, I've also removed unnecessary code to help keep it clean, I've used FileSetAttrib rather than using command prompt.

Anyway I hope that helps.

Cheers
#NoTrayIcon
#include <Array.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>

Global $DIR_PASSWORD = @AppDataDir & '\Meerecat\FolderLock'
Global $INI_PASSWORD = $DIR_PASSWORD & '\FldLock.ini'
Global $DIR_SECURITY = @MyDocumentsDir & '\My Secure Folder'
;Create the FolderLock folder if it doesn't exist
If Not FileExists($DIR_PASSWORD) Then DirCreate($DIR_PASSWORD)

;Install meerekat.jpg if it doesn't exist
If FileExists($DIR_PASSWORD & '\meerekat.jpg') = 0 Then FileInstall ('meerekat.jpg', $DIR_PASSWORD & '\meerekat.jpg')

;Check if Meerekat Password has been set
If IniRead($INI_PASSWORD, 'Password', 'Password', 'Error') = 'Error' Then
    $MSG_MEEREKAT = MsgBox(65, "Meerecat Folder Lock", "This is your first time running Folder Lock on this PC" & @LF & @LF & "You will now be prompted to create a password.")
	If $MSG_MEEREKAT = 2 Then Exit
	_SET_PASSWORD('Password')
EndIf

If FileExists($DIR_SECURITY) = 0 Then
    DirCreate($DIR_SECURITY)
    MsgBox(0 + 64, "Meerecat Folder Lock", "Your confidential folder has been created:" & @LF & @LF & "You will now find a folder in your Documents area called My Secure Folder.")
EndIf

_GET_PASSWORD('Password')


Func _main()
    ; ==================================================================================== removed option
    GUIDelete()
	$prompt = GUICreate("Meerecat Folder Lock", 581, 222, 192, 124)
    GUICtrlCreateLabel("Meerecat Simple Software © 2011", 408, 200, 167, 17)
    GUICtrlCreateLabel("Welcome to Meerecat Folder Lock", 215, 24, 294, 28)
    GUICtrlSetFont(-1, 16, 400, 0, "MS Sans Serif")
    GUICtrlCreateLabel("Please choose an option below", 247, 57, 223, 24)
	GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlCreatePic($DIR_PASSWORD & "\meerekat.jpg", 0, 0, 137, 217)
    GUISetBkColor(0xFBFBFB)
    $lock = GUICtrlCreateButton("Lock Folder", 152, 112, 120, 33)
    $unlock = GUICtrlCreateButton("Unlock Folder", 296, 112, 120, 33)
    $advanced = GUICtrlCreateButton("Advanced Options", 440, 112, 120, 33)
	$open = GUICtrlCreateButton("Open Secure Folder", 296, 152, 120, 33)
    $exit = GUICtrlCreateButton("Exit Program", 440, 152, 120, 33)
    GUISetState(@SW_SHOW)
	While 1
		$msg = GUIGetMsg()
		Switch $msg
			Case $GUI_EVENT_CLOSE
				Exit
			Case $exit
				Exit
			Case $lock
				FileSetAttrib($DIR_SECURITY, '+RSH', 1)
				RunWait(@ComSpec & ' /c cacls "' & $DIR_SECURITY & '" /e /c /d ' & @UserName, @UserProfileDir, @SW_HIDE)
				MsgBox(64,"Meerecat Folder Lock","Your folder has been LOCKED")
			Case $unlock
				RunWait(@ComSpec & ' /c cacls "' & $DIR_SECURITY & '" /e /c /g ' & @UserName & ":f",@UserProfileDir,@SW_HIDE)
				FileSetAttrib($DIR_SECURITY, '-RSH', 1)
				MsgBox(0 + 64,"Meerecat Folder Lock","Your folder has been UNLOCKED")
			Case $advanced
				_Advanced()
			Case $open
				RunWait(@ComSpec & ' /c cacls "' & $DIR_SECURITY & '" /e /c /g ' & @UserName & ":f",@UserProfileDir,@SW_HIDE)
				FileSetAttrib($DIR_SECURITY, '-RSH', 1)
				ShellExecute($DIR_SECURITY)
		EndSwitch
	WEnd
EndFunc   ;==>_main

Func _Advanced()
    ; ==================================================================================== removed option
    GUIDelete()
	$prompt = GUICreate("Meerecat Folder Lock", 581, 222, 192, 124)
    GUICtrlCreateLabel("Meerecat Simple Software © 2011", 408, 200, 167, 17)
    GUICtrlCreateLabel("Meerecat Folder Lock", 263, 24, 188, 28)
    GUICtrlSetFont(-1, 16, 400, 0, "MS Sans Serif")
    GUICtrlCreateLabel("Advanced Options", 287, 57, 134, 24)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlCreatePic($DIR_PASSWORD & "\meerekat.jpg", 0, 0, 137, 217)
    GUISetBkColor(0xFBFBFB)
    $chgpass = GUICtrlCreateButton("Change Password", 152, 112, 120, 33)
    $delete = GUICtrlCreateButton("Delete Folder", 296, 112, 120, 33)
    $back = GUICtrlCreateButton("Go Back", 440, 112, 120, 33)
    GUISetState(@SW_SHOW)
	While 1
		$msg = GUIGetMsg()
		Switch $msg
			Case $GUI_EVENT_CLOSE
				Exit
			Case $back
				_main()
			Case $chgpass
				_SET_PASSWORD('Password', $prompt)
			Case $delete
				$MSG_DELETE = MsgBox(49,"Meerecat Folder Lock","This will delete your secure folder.  This action CANNOT be undone." & @CRLF & "To continue press OK otherwise press Cancel")
				If $MSG_DELETE = 1 Then
					RunWait(@ComSpec & ' /c cacls "' & $DIR_SECURITY & '" /e /c /g ' & @UserName & ":f",@UserProfileDir,@SW_HIDE)
					FileSetAttrib($DIR_SECURITY, '-RSH', 1)
					DirRemove ($DIR_SECURITY)
					MsgBox(64,"Meerecat Folder Lock","Your secure folder has been removed. You will need to restart the program to recreate it.")
				Else
					_main()
				EndIf
		EndSwitch
	WEnd
EndFunc   ;==>_Advanced

Func _GET_PASSWORD($INI_SECTION)
	$GUI_PASSWORD = GUICreate('Meerecat Folder Lock', 320, 100)
	GUICtrlCreateLabel('Please enter your Password:', 10, 10, 300, 20)
	$GUI_PASSWORD1 = GUICtrlCreateInput('', 10, 35, 200, 20, $ES_PASSWORD)
	$CHK_PASSWORD1 = GUICtrlCreateLabel('', 210, 35, 100, 20, $SS_CENTERIMAGE)
	$BTN_PASSWORD = GUICtrlCreateButton('OK', 10, 60, 100, 20)
	$BTN_CANCELED = GUICtrlCreateButton('Cancel', 110, 60, 100, 20)
	GUISetState()
	While 1
		$msg = GUIGetMsg()
		Switch $msg
			Case $GUI_EVENT_CLOSE
				Exit
			Case $BTN_CANCELED
				Exit
			Case $BTN_PASSWORD
				$GET_PASSWORD = IniRead($INI_PASSWORD, $INI_SECTION, 'Password', 'Error')
				If $GET_PASSWORD = 'Error' Then
					$MSG_MEEREKAT = MsgBox(65, "Meerecat Folder Lock", "Meerecat Folder Lock Password has not been set, you will now be prompted to create a password.")
					If $MSG_MEEREKAT = 2 Then Exit
					_SET_PASSWORD('Password')
				Else
					If GUICtrlRead($GUI_PASSWORD1) <> $GET_PASSWORD Then
						GUICtrlSetBkColor($GUI_PASSWORD1, 0xFFE1E1)
						GUICtrlSetData($CHK_PASSWORD1, 'Password Incorrect')
						GUICtrlSetColor($CHK_PASSWORD1, 0xCC0000)
						ContinueLoop
					ElseIf GUICtrlRead($GUI_PASSWORD1) = '' Then
						GUICtrlSetBkColor($GUI_PASSWORD1, 0xFFE1E1)
						GUICtrlSetData($CHK_PASSWORD1, 'Blank Password')
						GUICtrlSetColor($CHK_PASSWORD1, 0xCC0000)
						ContinueLoop
					Else
						GUICtrlSetBkColor($GUI_PASSWORD1, 0xFFFFFF)
						GUICtrlSetData($CHK_PASSWORD1, '')
						_main()
						ExitLoop
					EndIf
				EndIf
		EndSwitch
	WEnd
EndFunc

Func _SET_PASSWORD($INI_SECTION, $PARENT_WINDOW = '')
	$GUI_PASSWORD = GUICreate('Meerecat Folder Lock', 320, 100, -1, -1, -1, -1, $PARENT_WINDOW)
	GUICtrlCreateLabel('Password:', 10, 10, 100, 20)
	$GUI_PASSWORD1 = GUICtrlCreateInput('', 110, 10, 100, 20, $ES_PASSWORD)
	$CHK_PASSWORD1 = GUICtrlCreateLabel('', 210, 10, 100, 20, $SS_CENTERIMAGE)
	GUICtrlCreateLabel('Confirm Password', 10, 35, 100, 20)
	$GUI_PASSWORD2 = GUICtrlCreateInput('', 110, 35, 100, 20, $ES_PASSWORD)
	$CHK_PASSWORD2 = GUICtrlCreateLabel('', 210, 35, 100, 20, $SS_CENTERIMAGE)
	$BTN_PASSWORD = GUICtrlCreateButton('OK', 10, 60, 100, 20)
	$BTN_CANCELED = GUICtrlCreateButton('Cancel', 110, 60, 100, 20)
	GUISetState()
	While 1
		$msg = GUIGetMsg()
		Switch $msg
			Case $GUI_EVENT_CLOSE
				GUIDelete($GUI_PASSWORD)
				ExitLoop
			Case $BTN_CANCELED
				GUIDelete($GUI_PASSWORD)
				ExitLoop
			Case $BTN_PASSWORD
				If GUICtrlRead($GUI_PASSWORD1) <> GUICtrlRead($GUI_PASSWORD2) Then
					GUICtrlSetBkColor($GUI_PASSWORD1, 0xFFE1E1)
					GUICtrlSetBkColor($GUI_PASSWORD2, 0xFFE1E1)
					GUICtrlSetData($CHK_PASSWORD1, 'Password Mismatch')
					GUICtrlSetData($CHK_PASSWORD2, 'Password Mismatch')
					GUICtrlSetColor($CHK_PASSWORD1, 0xCC0000)
					GUICtrlSetColor($CHK_PASSWORD2, 0xCC0000)
					ContinueLoop
				ElseIf GUICtrlRead($GUI_PASSWORD1) = '' Then
					GUICtrlSetBkColor($GUI_PASSWORD1, 0xFFE1E1)
					GUICtrlSetData($CHK_PASSWORD1, 'Blank Password')
					GUICtrlSetColor($CHK_PASSWORD1, 0xCC0000)
					ContinueLoop
				Else
					GUICtrlSetBkColor($GUI_PASSWORD1, 0xFFFFFF)
					GUICtrlSetBkColor($GUI_PASSWORD2, 0xFFFFFF)
					GUICtrlSetData($CHK_PASSWORD1, '')
					GUICtrlSetData($CHK_PASSWORD2, '')
					IniWrite($INI_PASSWORD, $INI_SECTION, 'Password', GUICtrlRead($GUI_PASSWORD1))
					GUIDelete($GUI_PASSWORD)
					ExitLoop
				EndIf
		EndSwitch
	WEnd
EndFunc

Open in new window

0
 
LVL 11

Expert Comment

by:TheGorby
ID: 35198282
Ok, my fault for not getting my post right the first time and having to add those extras changes, making things a bit confusing. Going by the line numbering in your most recent comment, just remove line 120 completely and it should work correctly... I think I clicked cancel at every opportunity possible and it worked for me that way.
0
 

Author Comment

by:JakeSpencer
ID: 35199559
Many thanks, that's great.

TheGorby, if you get chance would you send me a quick email, I want to send you something.  It's not SPAM I promise:

lee at meerecat dot net
0
 
LVL 11

Expert Comment

by:TheGorby
ID: 35200161
Will do, JakeSpencer.

matrixnz, I like your style but do you know if there's a way to make it so that after typing in the password(s) on the password prompt, pressing the enter key will 'click' ok? As it is now you have to actually click ok or hit Tab again and press Enter... but either way I'll certainly be saving your script for future reference.
0
 
LVL 29

Expert Comment

by:matrixnz
ID: 35201657
Hi TheGorby

Yes sure, basically something like this.

Cheers

#NoTrayIcon
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <Misc.au3>
#include <StaticConstants.au3>

$GUI_TEST = GUICreate('Test', 320, 100)
GUICtrlCreateLabel('Please enter your Password:', 10, 10, 300, 20)
$GUI_PASSWORD1 = GUICtrlCreateInput('', 10, 35, 200, 20, $ES_PASSWORD)
$CHK_PASSWORD1 = GUICtrlCreateLabel('', 210, 35, 100, 20, $SS_CENTERIMAGE)
$BTN_PASSWORD = GUICtrlCreateButton('OK', 10, 60, 100, 20)
$BTN_CANCELED = GUICtrlCreateButton('Cancel', 110, 60, 100, 20)
GUISetState()
While 1
  $msg = GUIGetMsg()
  Switch $msg
    Case $GUI_EVENT_CLOSE
      Exit
    Case $BTN_CANCELED
      Exit
    Case $GUI_PASSWORD1
      If _IsPressed('0D') Then
        CHK_PASSWORD('Enter Pressed')
      ElseIf _IsPressed('1B') Then
        Exit
      EndIf
      ContinueLoop
    Case $BTN_PASSWORD
      CHK_PASSWORD('Button Clicked')
        ContinueLoop
  EndSwitch
WEnd

Func CHK_PASSWORD($PAR_MESSAGE)
  $GET_PASSWORD = 'Password'
  If GUICtrlRead($GUI_PASSWORD1) <> $GET_PASSWORD Then
    GUICtrlSetBkColor($GUI_PASSWORD1, 0xFFE1E1)
    GUICtrlSetData($CHK_PASSWORD1, 'Password Incorrect')
    GUICtrlSetColor($CHK_PASSWORD1, 0xCC0000)
    MsgBox(48, 'Password', $PAR_MESSAGE)
  ElseIf GUICtrlRead($GUI_PASSWORD1) = '' Then
    GUICtrlSetBkColor($GUI_PASSWORD1, 0xFFE1E1)
    GUICtrlSetData($CHK_PASSWORD1, 'Blank Password')
    GUICtrlSetColor($CHK_PASSWORD1, 0xCC0000)
    MsgBox(48, 'Password', $PAR_MESSAGE)
  Else
    GUICtrlSetBkColor($GUI_PASSWORD1, 0xFFFFFF)
    GUICtrlSetData($CHK_PASSWORD1, '')
    MsgBox(48, 'Password', $PAR_MESSAGE)
  EndIf
EndFunc

Open in new window

0
 
LVL 11

Expert Comment

by:TheGorby
ID: 35201913
Very cool, thanks.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now