Is the flow correct ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Is the flow correct ?

Post by yabab33299 » 20 Feb 2021, 18:42

Hi Everyone, Can you guys please check the flow of this script. It's not working so something must be wrong.

Code: Select all

#SingleInstance Force
If !A_IsAdmin
	Run *RunAs "%A_ScriptFullPath%"
Process, Priority, , High

s1 := A_ScriptDir
s2 := "C:\Users\Folder"

If FileExist("File.txt")
     
{ 
msgbox, File Exists.         ;  <------- The file.txt exists, so the script continues on. 
}

Else
{
FileAppend, AUTOHOTKEY`n, %A_WorkingDir%\File.txt

return            ;   <-------- Since, the file don't exist, it'll have to make one. Afterwards, I want the entire script to just end there and close. 
                     ;     The problem is, the script did NOT close here, it kept on going. What's wrong ?                             
}

checkname:
IF ( s1 = s2 )  

{


RunWait, cmd.exe /c "taskkill /f /IM explorer.exe",, hide
Run, cmd.exe /c "c:\Windows\explorer.exe",, hide

InputBox, passcode, Enter your Passcode,, HIDE,, 100

 if (passcode = "passcode") {


MsgBox, Yes
  }          else          {

MsgBox, Try Again

gosub checkname
      }
         
}
[Mod edit: [code][/code] tags added.]
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Is the flow correct ?

Post by mikeyww » 20 Feb 2021, 18:54

1. What happens when you run the script?
2. What is the desired effect?
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: Is the flow correct ?

Post by yabab33299 » 20 Feb 2021, 19:14

mikeyww wrote:
20 Feb 2021, 18:54
1. What happens when you run the script?
2. What is the desired effect?
When the mentioned file is absent, the script creates it. But it does not end, which I want it to. And it continues on to the checkname variable.
I would like the script to check if a file is present. If it is present, then it continues and one would need to enter a passcode and the file will automatically open, lets say. If the file is absent, then the script will create the file, and the whole script should close after that.
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Is the flow correct ?

Post by mikeyww » 20 Feb 2021, 19:29

You can run the following test to see what happens.

Code: Select all

s1 := A_ScriptDir
s2 := "C:\Users\Folder"
If FileExist("File.txt")
 MsgBox, File Exists.
Else
{
 FileAppend, AUTOHOTKEY`n, %A_WorkingDir%\File.txt
 MsgBox, Appended & done!
 Return
}
Checkname:
MsgBox, Checkname
Return
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: Is the flow correct ?

Post by yabab33299 » 20 Feb 2021, 19:34

mikeyww wrote:
20 Feb 2021, 19:29
You can run the following test to see what happens.

Code: Select all

s1 := A_ScriptDir
s2 := "C:\Users\Folder"
If FileExist("File.txt")
 MsgBox, File Exists.
Else
{
 FileAppend, AUTOHOTKEY`n, %A_WorkingDir%\File.txt
 MsgBox, Appended & done!
 Return
}
Checkname:
MsgBox, Checkname
Return
It does not loop. If the wrong passcode is entered, it should keep looping until the correct one is inputted, then it stops.
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Is the flow correct ?

Post by mikeyww » 20 Feb 2021, 19:52

This is a test script without a loop. Which MsgBox appears?
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: Is the flow correct ?

Post by yabab33299 » 20 Feb 2021, 20:06

mikeyww wrote:
20 Feb 2021, 19:52
This is a test script without a loop. Which MsgBox appears?
After the file was created, I ran the script again. Both these MsgBoxs appeared, "File Exists", then "Checkname", then script ended
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Is the flow correct ?

Post by mikeyww » 20 Feb 2021, 20:34

So that is the answer. The file exists, so Checkname is executed. Am I missing a question that you have about this?

PS, if you kill the Explorer process, your script could run into problems.
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: Is the flow correct ?

Post by yabab33299 » 20 Feb 2021, 21:47

mikeyww wrote:
20 Feb 2021, 20:34
So that is the answer. The file exists, so Checkname is executed. Am I missing a question that you have about this?

PS, if you kill the Explorer process, your script could run into problems.
Sorry to digress, I have another question. When I use this heading, and I right-click, then Run This Script, the script SOMETIMES work, but when I right-click and choose RUN AS ADMINISTRATOR, it always work. What's wrong ?

Code: Select all

#SingleInstance Force
If !A_IsAdmin
	Run *RunAs "%A_ScriptFullPath%"
Process, Priority, , High
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Is the flow correct ?

Post by mikeyww » 20 Feb 2021, 22:40

For some scripts in some environments, a script needs to run as admin. I could imagine that this is the case if you are killing Explorer or writing to certain directories. In any case, it's easy: just run the script as admin.
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: Is the flow correct ?

Post by yabab33299 » 20 Feb 2021, 23:25

mikeyww wrote:
20 Feb 2021, 22:40
For some scripts in some environments, a script needs to run as admin. I could imagine that this is the case if you are killing Explorer or writing to certain directories. In any case, it's easy: just run the script as admin.
But by restarting Explorer, changes can take effect instantly without restarting computer. Do you know any way in place of restarting Explorer ?
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Is the flow correct ?

Post by mikeyww » 20 Feb 2021, 23:37

No. Perhaps everything works for you now if you run as admin.
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: Is the flow correct ?

Post by yabab33299 » 20 Feb 2021, 23:44

mikeyww wrote:
20 Feb 2021, 23:37
No. Perhaps everything works for you now if you run as admin.
But the autohotkey auto-elevate is not working. Does this elevate the entire script ? Or is something missing ?

#SingleInstance Force
If !A_IsAdmin
Run *RunAs "%A_ScriptFullPath%"
Process, Priority, , High
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Is the flow correct ?

Post by mikeyww » 20 Feb 2021, 23:53

Yes, but a description of "is not working" would be helpful.

It looks like you are not using the complete method to run as admin. See the following instructions.

https://www.autohotkey.com/docs/commands/Run.htm#RunAs

Alternatives are to use a Windows shortcut that is set with admin privileges, or to run from Windows Task Scheduler and set the admin privilege there.
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: Is the flow correct ?

Post by yabab33299 » 21 Feb 2021, 20:14

mikeyww wrote:
20 Feb 2021, 23:53
Yes, but a description of "is not working" would be helpful.

It looks like you are not using the complete method to run as admin. See the following instructions.

https://www.autohotkey.com/docs/commands/Run.htm#RunAs

Alternatives are to use a Windows shortcut that is set with admin privileges, or to run from Windows Task Scheduler and set the admin privilege there.
Hi, I have added what you just suggested into the script. It works now. However, when I run the compiled version in a task using task scheduler, the same problem appears again. The first "ELSE" function did not work.
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Is the flow correct ?

Post by mikeyww » 21 Feb 2021, 20:17

Feel free to post your revised script for feedback.
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: Is the flow correct ?

Post by yabab33299 » 21 Feb 2021, 21:24

Code: Select all


full_command_line := DllCall("GetCommandLine", "str")

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
    try
    {
        if A_IsCompiled
            Run *RunAs "%A_ScriptFullPath%" /restart
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
    }
    ExitApp
}

MsgBox A_IsAdmin: %A_IsAdmin%`nCommand line: %full_command_line%


s1 := A_ScriptDir
s2 := "C:\Users\Folder"

If FileExist("File.txt")
     
{ 
msgbox, File Exists.        
}

Else
{
FileAppend, AUTOHOTKEY`n, %A_WorkingDir%\File.txt

return                          
}

checkname:
IF ( s1 = s2 )  

{


RunWait, cmd.exe /c "taskkill /f /IM explorer.exe",, hide
Run, cmd.exe /c "c:\Windows\explorer.exe",, hide

InputBox, passcode, Enter your Passcode,, HIDE,, 100

 if (passcode = "passcode") {


MsgBox, Yes
  }          else          {

MsgBox, Try Again

gosub checkname
      }
         
}
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Is the flow correct ?

Post by mikeyww » 21 Feb 2021, 21:39

I added MsgBox lines so that you can report back with the exact sequence of boxes that appear.

Code: Select all

full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
    try
    {
        if A_IsCompiled
            Run *RunAs "%A_ScriptFullPath%" /restart
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
    }
    ExitApp
}
MsgBox, A_IsAdmin: %A_IsAdmin%`nCommand line: %full_command_line%
s1 := A_ScriptDir
s2 := "C:\Users\Folder"
MsgBox, s1 = %s1%`ns2 = %s2%
If FileExist("File.txt")
 MsgBox, File Exists.
Else
{
 MsgBox, File does not exist.
 FileAppend, AUTOHOTKEY`n, %A_WorkingDir%\File.txt
 Return
}

checkname:
MsgBox, Executing checkname.
If (s1 != s2) {
 MsgBox, s1 != s2
 Return
}
MsgBox, s1=s2
RunWait, cmd.exe /c "taskkill /f /IM explorer.exe",, hide
Run, cmd.exe /c "c:\Windows\explorer.exe",, hide
InputBox, passcode, Enter your Passcode,, HIDE,, 100
If (passcode = "passcode")
 MsgBox, Yes
Else
{
 MsgBox, Try Again
 Gosub checkname
}
yabab33299
Posts: 136
Joined: 06 May 2020, 17:16

Re: Is the flow correct ?

Post by yabab33299 » 21 Feb 2021, 22:26

mikeyww wrote:
21 Feb 2021, 21:39
I added MsgBox lines so that you can report back with the exact sequence of boxes that appear.

Code: Select all

full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
    try
    {
        if A_IsCompiled
            Run *RunAs "%A_ScriptFullPath%" /restart
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
    }
    ExitApp
}
MsgBox, A_IsAdmin: %A_IsAdmin%`nCommand line: %full_command_line%
s1 := A_ScriptDir
s2 := "C:\Users\Folder"
MsgBox, s1 = %s1%`ns2 = %s2%
If FileExist("File.txt")
 MsgBox, File Exists.
Else
{
 MsgBox, File does not exist.
 FileAppend, AUTOHOTKEY`n, %A_WorkingDir%\File.txt
 Return
}

checkname:
MsgBox, Executing checkname.
If (s1 != s2) {
 MsgBox, s1 != s2
 Return
}
MsgBox, s1=s2
RunWait, cmd.exe /c "taskkill /f /IM explorer.exe",, hide
Run, cmd.exe /c "c:\Windows\explorer.exe",, hide
InputBox, passcode, Enter your Passcode,, HIDE,, 100
If (passcode = "passcode")
 MsgBox, Yes
Else
{
 MsgBox, Try Again
 Gosub checkname
}
When I run the script by itself, everything works. However, if I put it in a task in the task scheduler, it goes to these msgboxes:
MsgBox, A_IsAdmin: %A_IsAdmin%`nCommand line: %full_command_line%
MsgBox, s1 = %s1%`ns2 = %s2%
MsgBox, File exist. <--------However, when the file does not exist, it still says it does.

then the loop continues, if passcode is wrong. But, it looks as if the script skips some parts
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Is the flow correct ?

Post by mikeyww » 22 Feb 2021, 06:40

In your FileExist function, provide the full path to that file. An alternative is to set the working directory.
Post Reply

Return to “Ask for Help (v1)”