-1

I have tried to search for an example of how to do this with no success including with autohotkey (I'm very much a noob with AHK). What I am trying to do is open a command window at the explorer location I am at, run a batch program that is in a location searched by the "path" environmental variable, then close the command window. What I have tried in AHK is:

LWin & Return::

if WinActive("ahk_class CabinetWClass")

or WinActive("ahk_class ExploreWClass")

{

  Send {Shift Down}{AppsKey}{Shift Up}

  Sleep 10

  Send w{enter}

  Sleep 10

  run fp.bat

  Sleep 10

Send,{LAlt Down}{Space Down}{Space Up}{LAlt Up}{c Down}

}

else

{

  run, cmd, C:\

}

return

The command window does open properly, but the batch file seems to run outside of that opened command window and the explorer window closes instead of the command window.

I am running Windows 10 Pro

I’m pretty close to what I am trying to accomplish with the AHK script I’ve included above. And what I am trying to do with it is to have the KB combo of the "Windows Key" and the "Return Key" open a command window in the current explorer location where I am working in an Explorer GUI window, run a specific batch file there (in my case fp.bat) and then close the previously opened command window returning me to the Explorer GUI window where I started.

What is happening with my AHK script is that the command window opens successfully, but when the batch file runs, it does not run in the opened command window, but rather runs as if I had just run in from the search/run box (I see a different window flash by quickly and close), and then the explorer window closes rather than the command window – leaving me with an open command window in the proper location, but which did not run the batch file. It is as if I am losing the focus from the command window and to the Explorer window when the 2nd and 3rd steps execute (run the batch file and then close the opened command window).

Maybe what I am trying to do just can’t be done…

  • cmd /c Run Command and then terminate? – DavidPostill Aug 12 '15 at 17:12
  • I'm trying to create a KB hotkey that will do it all with a combo keystroke using AHK. – MrGadget Aug 12 '15 at 17:14
0

With consideration to your update, why not send the keys to run your batch file directly to your command prompt?

if WinActive("ahk_class CabinetWClass") or WinActive("ahk_class ExploreWClass")
{
  Send {Shift Down}{AppsKey}{Shift Up}
  Sleep 10
  Send w
  Sleep 10
  WinWaitActive, ahk_exe cmd.exe
  Sleep 10
  Send fp.bat{Enter}
  ...
  • psouza4, I wish I understood AHK well enough to understand what the WinWait... line does, but I know that what you suggested solved my problem and so thank you very much! I'm learning AHK very slowly by finding examples that accomplish specific tasks/functions amd then reading up on the elements included, etc., then trying to apply them in modified form elsewhere. So again thank you for the assit and a new element in my KB. – MrGadget Aug 12 '15 at 19:15
  • No problem and thanks in advance for accepting my solution. FYI: WinWait literally just pauses the AHK script until the desired window appears. Instead of looking for the caption title text of the window, we're telling AHK to look for a process by the name of cmd.exe to appear. More info here: ahkscript.org/docs/commands/WinWait.htm – psouza4 Aug 12 '15 at 19:17
  • After two years of this working without issue, it stopped working after the Windows 10 Fall Creators update. Now the first time I run it a "New Folder" is created and then on subsequent attempts nothing (apparently) happens. Any thoughts on how to restore functionality? – MrGadget Oct 28 '17 at 17:35
0

Perhaps integrate your solution with the free, open-source CMDOW. I use it a lot in my command line automation:

http://www.commandline.co.uk/cmdow/

You can close windows with /CLS and /END, such as these batch file contents:

(fp.bat)

TITLE Automation ID 123456
...

(another batch file that closes fp.bat)

CMDOW.EXE "*Automation ID 123456*" /END

I'm assuming fp.bat is not ending gracefully, thus the forced termination.

  • Thanks for trying to assist, psouza4 and DavidPostill. I looked at cmdow, but am afraid that most of that goes over my head. I've edited my post to hopefully add more clarity to what is occuring. – MrGadget Aug 12 '15 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.