Running CMD command and waiting for it to finish. Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Vandalieu
Posts: 3
Joined: 06 Aug 2020, 10:09

Running CMD command and waiting for it to finish.

06 Aug 2020, 10:22

So I'm trying to create a hotkey that will run a ping or tracert in command prompt and output the results to a file. That's easy and all but I'm stumped as I want to use autohotkey variables and the like, so that stops me from using the run and runwait commands.

This is where I am at so far:

Code: Select all

^!numpad4::
InputBox, var, What address would you like to ping? 
if ErrorLevel
{
	return
}
run, cmd.exe
WinWait, C:\WINDOWS\SYSTEM32\cmd.exe
Sendinput, prompt Time:$T $B Path: $P$G{enter}cd "\path"{enter}cls{enter}ping %var%>ping-%A_Hour%%A_Min%-%A_DD%%A_MM%%A_YYYY%-%var%.txt{enter}
return
Now that works fine. That does exactly as I would like it to. Now what I want is that after It is finished with it's ping test is to exit the command prompt window.

Is there any dynamic wait command that will specifically target the ping command? I could split the code into multiple Sendinput lines if there were a specific command to run right before it.

Any ideas? Scoured the web to no avail.
User avatar
flyingDman
Posts: 860
Joined: 29 Sep 2013, 19:01

Re: Running CMD command and waiting for it to finish.

06 Aug 2020, 11:18

Use this to have the ping response go straight to the clipboard:

Code: Select all

s1 := "google.com"
Runwait %comspec% /c ping %s1% | clip,,hide
Use this for it to go to a txt file:

Code: Select all

s1 := "google.com"
Runwait %comspec% /c ping %s1% > %A_Hour%%A_Min%-%A_DD%%A_MM%%A_YYYY%-%s1%.txt,, hide
You'll never see the command prompt window.
Vandalieu
Posts: 3
Joined: 06 Aug 2020, 10:09

Re: Running CMD command and waiting for it to finish.

06 Aug 2020, 21:57

flyingDman wrote:
06 Aug 2020, 11:18
Use this to have the ping response go straight to the clipboard:

Code: Select all

s1 := "google.com"
Runwait %comspec% /c ping %s1% | clip,,hide
Use this for it to go to a txt file:

Code: Select all

s1 := "google.com"
Runwait %comspec% /c ping %s1% > %A_Hour%%A_Min%-%A_DD%%A_MM%%A_YYYY%-%s1%.txt,, hide
You'll never see the command prompt window.
I attempted both of those (copy and paste) and only got the first one to work, placing the results straight to the clipboard, and that worked even with all my variables, etc.

The second one, however, I was unable to get working no matter how I put it. For the sake of simplicity - this is what I've tried to get going:

Code: Select all

^!numpad4::
Runwait %comspec% /c cd "C:\Users\User\Desktop\cmdoutput" | ping google.com > TESTING.txt ;,, hide
return
That does not work. No file is created in that directory. I tested it by copy and pasting into cmd for 100% accuracy and it worked :headwall:
If I edit it a little and have it as so:

Code: Select all

^!numpad4::
Runwait %comspec% /c cd "C:\Users\User\Desktop\cmdoutput" | ping google.com | clip ;,,hide
return
Then it works! It just seems to not want to create the file and paste in there for some reason. I got rid of all the date and time variables just in case they were to interfere with it at all and nothing.
I don't believe that it should require any administrative permissions as it should run as the current user (I assume).

Is there a way to create a .txt file with the clipboard as the contents? Without seeing any GUI come up on the screen. If that's easier than having to have administrator permissions (which my user does) then I don't see any downside.

PS. The ',, hide' being a comment in both of my examples is to say I have tested it with and without it.
garry
Posts: 2430
Joined: 22 Dec 2013, 12:50

Re: Running CMD command and waiting for it to finish.  Topic is solved

07 Aug 2020, 08:13

Is there a way to create a .txt file with the clipboard as the contents?
script from flyingDman

Code: Select all

s1 := "google.com"
Runwait %comspec% /c ping %s1% | clip,,hide
fileappend,%clipboard%`r`n,test55.txt
exitapp
User avatar
flyingDman
Posts: 860
Joined: 29 Sep 2013, 19:01

Re: Running CMD command and waiting for it to finish.

07 Aug 2020, 10:05

Code: Select all

^!numpad4::
Runwait %comspec% /c cd "C:\Users\User\Desktop\cmdoutput" | ping google.com > TESTING.txt ;,, hide
return
works here
Vandalieu
Posts: 3
Joined: 06 Aug 2020, 10:09

Re: Running CMD command and waiting for it to finish.

08 Aug 2020, 00:29

Thanks heaps for this! I'll put what I learned here to good use in my future hotkeys.
garry
Posts: 2430
Joined: 22 Dec 2013, 12:50

Re: Running CMD command and waiting for it to finish.

08 Aug 2020, 09:54

@flyingDman thank you for the examples , works fine , but I've just problem with change directory
the second command : ping > testing.txt is in 'a_scriptdir' ( not in changed directory )
seems correct :
Runwait %comspec% /k cd /D "D:\M_GARRY"&ping google.com >TESTING1.txt,,

Code: Select all

;- Example OK :
/*
fd1:="D:\M_GARRY"
fl1:=fd1 . "\testing.txt"
setworkingdir,%fd1%
Runwait %comspec% /c ping google.com >TESTING.txt,,
try
  run,%fl1%
return
*/

;- Example OK :
;Runwait %comspec% /k cd /D "D:\M_GARRY"   ;- OK


;- here problem with change directory / testing.txt is written to  'a_scriptdir'  :
/*
Runwait %comspec% /k cd /D "D:\M_GARRY" | ping google.com >TESTING1.txt,,
Runwait %comspec% /k cd "C:\TEST" | ping google.com >TESTING2.txt,,
*/

;- Example OK :
Runwait %comspec% /k cd /D "D:\M_GARRY"&ping google.com >TESTING1.txt,,

User avatar
flyingDman
Posts: 860
Joined: 29 Sep 2013, 19:01

Re: Running CMD command and waiting for it to finish.

08 Aug 2020, 11:39

It should have been:

Code: Select all

Runwait %comspec% /c cd "C:\Users\User\Desktop\cmdoutput" && ping google.com > testing.txt ;,, hide
return
but, obviously, one could use:

Code: Select all

Runwait %comspec% /c ping google.com > C:\Users\User\Desktop\cmdoutput\testing.txt ;,, hide
return

Return to “Ask For Help”

Who is online

Users browsing this forum: DanRim, Drako, mikeyww, thebbandit, wineguy, yaoz889 and 48 guests