Batch File Doesn't Run Properly in AHK. Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
lmstearn
Posts: 332
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn
Contact:

Batch File Doesn't Run Properly in AHK.

13 Mar 2017, 23:36

Hi there,
Made up this batch file for a game some time back. Runs well enough from the desktop. However launching it from AHK with the run command well- uh- makes the command window reposition itself repeatedly out of control. So much so it is impossible to cancel it.
Spoiler
Here is the line used to invoke it:

Code: Select all

Run, %comspec% /c C:\Users\%UserName%\Desktop\REMOVEFNISREMNANTTOOL.bat
No idea what is causing it on Windows 10. The use of echo or set p?
If anyone is game to try it, be WARNED! Save your work and prepare to sign out of or reboot Windows, as I have done on several occasions. The system becomes almost unresponsive even if the taskman is on the located on the screen RHS while attempting to end the task. :wtf:
Any suggestions appreciated.
Thanks
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
Guest

Re: Batch File Doesn't Run Properly in AHK.

14 Mar 2017, 01:18

try:

Code: Select all

Run, C:\Users\%UserName%\Desktop\REMOVEFNISREMNANTTOOL.bat
?
User avatar
lmstearn
Posts: 332
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn
Contact:

Re: Batch File Doesn't Run Properly in AHK.

14 Mar 2017, 03:10

Guest wrote:try:

Code: Select all

Run, C:\Users\%UserName%\Desktop\REMOVEFNISREMNANTTOOL.bat
?
Tried that, no dice. also with UseErrorLevel, OutputVarPID.

Here's a modified form of the batch file:

Code: Select all

:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights (thanks to TanisDLJ at Stackoverflow)
:::::::::::::::::::::::::::::::::::::::::
@echo off
color 1E
mode 100,50
VERIFY > nul
CLS
:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\icacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo args = "" >> "%temp%\getadmin.vbs"
    echo For Each strArg in WScript.Arguments >> "%temp%\getadmin.vbs"
    echo args = args ^& strArg ^& " "  >> "%temp%\getadmin.vbs"
    echo Next >> "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", args, "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs" %*
    exit /B

:gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )

Setlocal EnableDelayedExpansion & CD /D "%~dp0"
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::



REM Run shell as admin (example) - put here code as you like



set CREATKIT=
set TESVGAME=
set TES5EDIT=
set SKYRIMSE=


tasklist /fi "IMAGENAME eq CreationKit.exe" | find /i "CreationKit.exe" > nul

if %errorlevel% EQU 0 set CREATKIT=Creation Kit is running


tasklist /fi "IMAGENAME eq tesv.exe" | find /i "tesv.exe" > nul

if %errorlevel% EQU 0 set TESVGAME=Skyrim is running

tasklist /fi "IMAGENAME eq SkyrimSE.exe" | find /i "SkyrimSE.exe" > nul

if %errorlevel% EQU 0 set SKYRIMSE=Skyrim Special Edition is running


tasklist /fi "IMAGENAME eq TES5Edit.exe" | find /i "TES5Edit.exe" > nul

if %errorlevel% EQU 0 set TES5EDIT=TES5Edit is running

echo(
echo(
If DEFINED CREATKIT echo %CREATKIT%
If DEFINED TESVGAME echo %TESVGAME%
If DEFINED TES5EDIT echo %TES5EDIT%
If DEFINED SKYRIMSE echo %SKYRIMSE%
echo(
echo(



echo                     Welcome to the FNIS Remnant Removal Tool!
echo(
echo        Before continuing this script, check the install status of the FNIS Creature Pack:
echo(
echo    If installed, open the FNIS Generator and click "De-Install Creatures", and quit the Generator. 
echo(
echo   Then deactivate and uninstall FNIS, FNIS Creature Pack, and FNIS Spells through your mod manager.
echo(
:PROMPTBEGIN
SET "validateText="
SET /P validateText="________________________________Are you ready? type Y or N_________________________________Y/N]"
echo(
echo(
@echo off

if /I !validateText!==y goto BEGIN
if /I !validateText!==n goto ENDSCRIPT
GOTO PROMPTBEGIN

:BEGIN
:PROMPTBEGIN
:ENDSCRIPT
Even something like this made no difference:

Code: Select all

Run, C:\Users\New\Desktop\Test.bat,, Hide UseErrorLevel, OutputVarPID
However, just remembered the combination to stop the darn thing flashing: <CTRL-Break> or <CTRL-C>.
Last edited by lmstearn on 14 Mar 2017, 05:02, edited 1 time in total.
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
4GForce
Posts: 553
Joined: 25 Jan 2017, 03:18
GitHub: 4GForce

Re: Batch File Doesn't Run Properly in AHK.

14 Mar 2017, 03:55

I hate that post title, feels like your blaming Ahk because you are unable to achieve what you want. :crazy:
Anyway I haven't tested your code because of the warning and mostly because I'm lazy but ...
have you tried running the script as admin ? You are trying to access UAC and some privileges so it seems obvious that would help ...
User avatar
lmstearn
Posts: 332
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn
Contact:

Re: Batch File Doesn't Run Properly in AHK.

14 Mar 2017, 04:53

4GForce wrote:Anyway I haven't tested your code because of the warning and mostly because I'm lazy but ...
have you tried running the script as admin ? You are trying to access UAC and some privileges so it seems obvious that would help ...
Tried running as admin, no difference but thanks.
4GForce wrote:I hate that post title, feels like your blaming Ahk because you are unable to achieve what you want. :crazy:
Yes I agree. It's very sad. How shall we reword it? Perhaps:
Naughty script runs amok with nice AHK?
Well, in actual fact that just about says it alll. :)

Consider this pared down (again) script:

Code: Select all

@echo off
color 1E
mode 100,50

Setlocal EnableDelayedExpansion & CD /D "%~dp0"

set CREATKIT=
set TESVGAME=
set TES5EDIT=
set SKYRIMSE=


tasklist /fi "IMAGENAME eq CreationKit.exe" | find /i "CreationKit.exe" > nul

if %errorlevel% EQU 0 set CREATKIT=Creation Kit is running


tasklist /fi "IMAGENAME eq tesv.exe" | find /i "tesv.exe" > nul

if %errorlevel% EQU 0 set TESVGAME=Skyrim is running

tasklist /fi "IMAGENAME eq SkyrimSE.exe" | find /i "SkyrimSE.exe" > nul

if %errorlevel% EQU 0 set SKYRIMSE=Skyrim Special Edition is running


tasklist /fi "IMAGENAME eq TES5Edit.exe" | find /i "TES5Edit.exe" > nul

if %errorlevel% EQU 0 set TES5EDIT=TES5Edit is running

echo(
echo(
If DEFINED CREATKIT echo %CREATKIT%
If DEFINED TESVGAME echo %TESVGAME%
If DEFINED TES5EDIT echo %TES5EDIT%
If DEFINED SKYRIMSE echo %SKYRIMSE%
echo(
echo(



echo Blah
echo(
:PROMPTBEGIN
SET "validateText="
SET /P validateText="________________________________Are you ready? type Y or N_________________________________Y/N]"
echo(
echo(
@echo off

if /I !validateText!==y goto BEGIN
if /I !validateText!==n goto ENDSCRIPT
GOTO PROMPTBEGIN

:BEGIN
:PROMPTBEGIN
:ENDSCRIPT
Now executes perfectly! So it was the embedded VBscript causing the issue:

Code: Select all

REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\icacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo args = "" >> "%temp%\getadmin.vbs"
    echo For Each strArg in WScript.Arguments >> "%temp%\getadmin.vbs"
    echo args = args ^& strArg ^& " "  >> "%temp%\getadmin.vbs"
    echo Next >> "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", args, "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs" %*
    exit /B

:gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
In fact running with the embedded script actually causes a lot of new command windows to appear almost spontaneously. Hence the flashing. But still no idea why this should occur when launched from AHK unless something funky is going on with stdout, stderr and DACLs. :?
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
User avatar
Xeo786
Posts: 238
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Batch File Doesn't Run Properly in AHK.

14 Mar 2017, 08:31

I tried following code and its working

Code: Select all

UserName := "MyUserName"   ; set your actual user name 
Runwait, %comspec% /k ""C:\Users\""%UserName%""\Desktop\REMOVEFNISREMNANTTOOL.bat""  ; runwait so ahk wait for comspec


but I changed the bat file :P just to test ahk code ;)

Code: Select all

START CMD /C "ECHO My Popup Message && PAUSE"
:D

but why don't you save bat code into variable and run it through ahk comspec

Code: Select all

code =
(
START CMD /C "ECHO My Popup Message && PAUSE"
)
Runwait, %comspec% /c "%code%"
return
"When there is no gravity, there is absolute vacuum" -Game changer theory
User avatar
lmstearn
Posts: 332
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn
Contact:

Re: Batch File Doesn't Run Properly in AHK.

14 Mar 2017, 09:03

Xeo786 wrote: but why don't you save bat code into variable and run it through ahk comspec

Code: Select all

code =
(
START CMD /C "ECHO My Popup Message && PAUSE"
)
Runwait, %comspec% /c "%code%"
return
Ah yes, thanks that's a good idea. A bit like the system command where the arcane nuances of syntax turned out to be ugh ... quite interesting. :)
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
BoBo
Posts: 3636
Joined: 13 May 2014, 17:15

Re: Batch File Doesn't Run Properly in AHK.

14 Mar 2017, 17:56

Has anyone of you worked with jpsoft's TCC already?
User avatar
lmstearn
Posts: 332
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn
Contact:

Re: Batch File Doesn't Run Properly in AHK.

31 Jan 2018, 08:47

Bobo: Looks interesting- also Powershell overcomes a few cmd limitations.
Just a little bit more info on the vbs script:

Code: Select all

 echo CreateObject^("Shell.Application"^).ShellExecute "%~s0", "",  "", "runas", 1  >> "%temp%\getadmin.vbs"
Fine if run directly from the shell, but if there is an intermediary e.g. N++ or AHK etc. the "runas" fails, and the script is run as invoker instead, which causes the looping as described in the OP.
There are other options as well as the file append, however- e.g. RunAs or the following method which may or may not be any better:

Code: Select all

DllCall(ShellExecute, uint, 0, str, "RunAs", str, "Script.bat", str, "", int, 1)
.
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
User avatar
lmstearn
Posts: 332
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn
Contact:

Re: Batch File Doesn't Run Properly in AHK.

01 Feb 2018, 09:24

Out of interest, here's an "MCVE", this time running a JS version of the Shell Object. Run it as a bat file, all good, but launch it as a command from a second process and the shell call disregards the "Runas" verb, regardless of the level of invocation- (as if that mattered anyway):

Code: Select all

@echo off
color 1E
mode 100,50
VERIFY > nul
CLS

Setlocal EnableDelayedExpansion & CD /D "%~dp0"
>nul 2>&1 "%SYSTEMROOT%\system32\icacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' == '0' (
goto gotAdmin
) else (
echo Requesting administrative privileges..
Pause
goto UACPrompt
)

:UACPrompt
CD /d %~dp0
MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('%~nx0', '', '', 'runas', 1);close();"
exit /b

:gotAdmin
echo Eureka Got Admin!
pause

EXIT /b 0
Definitely looks like a limitation on the Shell object itself. Now to lift the hood and see what's inside! *Shrinks Away*.
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
User avatar
lmstearn
Posts: 332
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn
Contact:

Re: Batch File Doesn't Run Properly in AHK.  Topic is solved

05 Dec 2019, 01:25

Further investigation reveals the looping "culprit" in AHK Run not working on W10 1809 is some kind of issue with calcs.exe got from this code. The file launched from getadmin.vbs in the OP code never gets admin privileges, even after sampling the most recent SO version of it.
Having said that, who now would of ever have thought FSUTIL dirty query of ever being a solution to the problem?

Code: Select all

@Echo Off
Setlocal
:: First check if we are running As Admin/Elevated
FSUTIL dirty query %SystemDrive% >nul
if %errorlevel% EQU 0 goto START

::Create and run a temporary VBScript to elevate this batch file
   Set _batchFile=%~f0
   Set _Args=%*
   :: double up any quotes
   Set _batchFile=""%_batchFile:"=%""
   Set _Args=%_Args:"=""%

   Echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\~ElevateMe.vbs"
   Echo UAC.ShellExecute "cmd", "/c ""%_batchFile% %_Args%""", "", "runas", 1 >> "%temp%\~ElevateMe.vbs"

   cscript "%temp%\~ElevateMe.vbs" 
   Exit /B

:START
:: set the current directory to the batch file location
cd /d %~dp0
:: Place the code which requires Admin/elevation below
Echo We are now running as admin [%1] [%2]
pause
Marking this as solved (at least for this rig)- all improvements welcome. :)
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH

Return to “Ask For Help”

Who is online

Users browsing this forum: malcev, mikeyww, Rohwedder, SteveB123 and 43 guests