How to auto decide close cmd window or not based on results after running? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pk23
Posts: 110
Joined: 24 Apr 2015, 00:49

How to auto decide close cmd window or not based on results after running?

12 Nov 2020, 23:57

Code: Select all

runwait, %comspec% /c %commands%
With /c, the cmd window closes after execution; with /k, the window does not close.

I wanna change it to: close the cmd window based on the results of the run.
If it succeeds (e.g. if a preset string such as "success" appears in the cmd window), the window will close automatically.
If there is an error while running (e.g., the preset string does not appear), keep the window open!

Is there any way to do this? cmd window doesn't seem to return a result code to AutoHotkey, I really don't know how to do it!
User avatar
mikeyww
Posts: 27241
Joined: 09 Sep 2014, 18:38

Re: How to auto decide close cmd window or not based on results after running?

13 Nov 2020, 07:36

RunWait: Sets ErrorLevel to the program's exit code (a signed 32-bit integer). If UseErrorLevel is in effect and the launch failed, the word ERROR is stored.
It works. Demo is below.

Code: Select all

RunWait, %A_ComSpec% /c "%TEMP%\success.cmd",, Hide
results = Success: %ErrorLevel%`n
RunWait, %A_ComSpec% /c "%TEMP%\failure.cmd",, Hide
results .= "Failure: " ErrorLevel
MsgBox, 64, Results, %results%
Here are the two programs.

success.cmd:

Code: Select all

Exit 0
failure.cmd:

Code: Select all

Exit 1
Output:

---------------------------
Results
---------------------------
Success: 0
Failure: 1
---------------------------
User avatar
boiler
Posts: 17279
Joined: 21 Dec 2014, 02:44

Re: How to auto decide close cmd window or not based on results after running?

13 Nov 2020, 08:12

pk23 wrote: Pls quote my reply or @ me when replying, otherwise I cannot be notified and notice your reply! (signature line)
You should change your notification settings to be notified when someone has posted in a thread in which you have posted (which becomes a subscribed thread) rather than expect people to quote or @mention you when they otherwise wouldn’t. That would take advantage of the forum’s capabilities rather than putting the burden on others.
pk23
Posts: 110
Joined: 24 Apr 2015, 00:49

Re: How to auto decide close cmd window or not based on results after running?

13 Nov 2020, 15:08

@mikeyww What need to be called here is an exe file, and the following code won't run as expected:

Code: Select all

runwait, %comspec% /c youtube-dl.exe "fake url"
results = Success: %ErrorLevel%`n
MsgBox, 64, Results, %results%
I didn't want to introduce batch file to pass error code at first, but if it's the only option, so be it. thank you for your warmheart mikey! :D
boiler wrote:
13 Nov 2020, 08:12
You should change your notification settings to be notified when someone has posted in a thread in which you have posted (which becomes a subscribed thread) rather than expect people to quote or @mention you when they otherwise wouldn’t. That would take advantage of the forum’s capabilities rather than putting the burden on others.
then once I replied in a megathread that tons of members participate in, eg this, I put burden on myself that either remember to unsubscribe this megathread after the sub-discussion I'm involved in is finished, or get ready to receive LOTS of notification about other little sub-discussions (under this same thread) that I'm not interested in. thanks for your reminder, but NO. tbh quote or @ in phpbb-type forum should not be a burden but netiquette which benifits us all, but if anyone feels it's a true burden and esp he's the one trying to help (instead of asking for my help), replying in any way he likes is of course all welcome. :)
User avatar
mikeyww
Posts: 27241
Joined: 09 Sep 2014, 18:38

Re: How to auto decide close cmd window or not based on results after running?

13 Nov 2020, 16:32

OK. We showed here that AHK can receive the ErrorLevel from a program. In your case, you are interested in creating an action depending on the results of the program's run. That means that you use ErrorLevel to do that or, if that program's ErrorLevel is not what you want, then you need some other way to determine what course of action to pursue. In your case, if your program doesn't actually use ErrorLevel at all, then you will have to look for a different "signal" or marker of whatever outcome you are seeking. For example, if the program downloads a file, then you look to see if that output file exists after the downloader finishes.

The AHK forum threads here in the help section tend to be focused on the initial problem at hand-- which is your own problem, of course, the one that interests you. It's certainly up to you whether you want to receive the replies about them! Every now and then, I unsubscribe from a thread, which is easy to do, too.
garry
Posts: 3788
Joined: 22 Dec 2013, 12:50

Re: How to auto decide close cmd window or not based on results after running?  Topic is solved

14 Nov 2020, 04:07

below example with DOS , shows the LINE with searched STRING = 'Bytes'

Question : is it possible to copy the DOS ( maybe hidden ) window ? Maybe with postmessage ...
Example : when youtube-dl.exe is running and the video-URL is wrong , you see in DOS window the word = 'ERROR:' , if this found so no success

Code: Select all

;- https://www.autohotkey.com/boards/viewtopic.php?f=27&t=83018     20201108
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=83172     20201112

;- Example : DOS command = netstat -e -s |find /I "Bytes" ,  find the word 'bytes'
;- ( maybe clip not work for streaming )
#Warn
#Noenv
setworkingdir,%a_scriptdir%
mycmd:="netstat -e -s"
srch :="Bytes"
transform,s,chr,34
Gui,1: -dpiscale
Gui,1:Font,cBlack S12,Lucida Console
Gui,1:Add,Edit,   x10   y5    h600  w1000 vED1,
Gui,1:Add,button, x10   y650  h35   w110  gSTART,START
Gui,1:Add,Text  , x150  y650                    , Found the string "%srch%" (Y/N) ? >>
Gui,1:Add,Edit  , x850  y650  h35   w110  vED2 right,
Gui,1:Show      , x0    y0    h720  w1020 , Searched string=  %srch%   for=  %mycmd%
return
;-------------------------------------------------------
Guiclose:
Exitapp
;-------------------------------------------------------
START:
clipboard=
Gui,1:submit,nohide
GuiControl,1:,ED1,
;- case-1
runwait,%comspec% /c %mycmd% |find /I "%srch%" |clip,,hide      ;- shows only the LINE with found STRING
msg1:= mycmd . " |find /I " . s . srch . s . " |clip`r`n====== Searched for : " . srch . " =====================`r`n`r`n"
;- case-2
;runwait,%comspec% /c %mycmd% |clip,,hide                        ;- shows all
;msg1:= mycmd . " |clip`r`n====== SHOW ALL =====================`r`n`r`n"
;---------
clipwait,1,1
GuiControl,1:,ED1,%msg1%%clipboard%
if clipboard contains %srch%
  GuiControl,1:,ED2,YES
else
  GuiControl,1:,ED2,NO
clipboard=
return
;========================================================
pk23
Posts: 110
Joined: 24 Apr 2015, 00:49

Re: How to auto decide close cmd window or not based on results after running?

14 Nov 2020, 05:50

garry wrote:
14 Nov 2020, 04:07

Code: Select all

runwait,%comspec% /c %mycmd% |find /I "%srch%" |clip,,hide
Wow! didnt know (forget) pipeline of cmd. it works here amazingly! thank you garry for so detailed and incisive help! :clap: :thumbup:
garry
Posts: 3788
Joined: 22 Dec 2013, 12:50

Re: How to auto decide close cmd window or not based on results after running?

14 Nov 2020, 15:06

@pk23 good it works for your example ,

but I don't understand how to use in this case , when runwait finished then copy the DOS window ,
example : runwait, %comspec% /c youtube-dl.exe "fake url"
example get 'ERROR:' because wrong URL or other failure

Question : How to copy the DOS window to clipboard after runwait finished ?

In this example I had no success with CLIP

Code: Select all

;url=https://www.youtube.com/watch?v=G6_DldYj0uQ   ;- Li Xiang Lan
 url=https://www.youtube.com/watch?v=G6_DldYj0u

;runwait,%comspec% /c youtube-dl %url% |clip,,hide 
runwait,%comspec% /k youtube-dl %url% |clip,, 
 
clipwait,1,1
msgbox,%clipboard%
exitapp

/*
ERROR: Incomplete YouTube ID G6_DldYj0u. URL https://www.youtube.com/watch?v=G6_DldYj0u looks truncated.
C:\Users\GARRY\Desktop\PROJECT\YT_DL_TEST>
*/
User avatar
mikeyww
Posts: 27241
Joined: 09 Sep 2014, 18:38

Re: How to auto decide close cmd window or not based on results after running?

14 Nov 2020, 15:30

Just FYI-- not the answer here-- ClipWait requires starting with an empty clipboard. After the Clipboard is then populated, the ClipWait is completed. If you use ClipWait, you can also add a condition that checks for the ErrorLevel. If you don't, then you won't know if the ClipWait succeeded or failed. Since you are waiting only for text, the final ClipWait parameter serves no purpose and can be omitted.

Try this. It worked for me. The key was enabling the error output from the command line.

Code: Select all

url = https://www.youtube.com/watch?v=G6_DldYj0u
app = e:\data\utils\youTubeDownloader\youtube-dl.exe ; Your own path here
Clipboard =
RunWait, %ComSpec% /c %app% %url% 2>&1 |clip
ClipWait, 2
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else MsgBox, 64, Clipboard, %Clipboard%
ExitApp
garry
Posts: 3788
Joined: 22 Dec 2013, 12:50

Re: How to auto decide close cmd window or not based on results after running?

14 Nov 2020, 17:32

@pk23 here script from user 'mikeyww'
@mikeyww ,thank you , your examples work fine

Code: Select all

;- user mikeyww
;-------- saved at 星期六 十一月 2020-11-14  22:15 UTC --------------
;- How to auto decide close cmd window or not based on results after running? 
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=83172

;url=https://www.youtube.com/watch?v=G6_DldYj0u    ;- URL wrong
url=https://www.youtube.com/watch?v=G6_DldYj0uQ    ;- URL Li Xiang Lan

app = youtube-dl.exe                               ; Your own path here
Clipboard =
SplashTextOn, 700, 100, Downloading %url%, `nPlease wait....
RunWait, %ComSpec% /c %app% %url% 2>&1 |clip,,hide
SplashTextOff
ClipWait, 2
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else MsgBox, 64, Clipboard, %Clipboard%
;fileappend,%clipboard%,test21.txt
ExitApp
;------------------------
/*
ERROR: Incomplete YouTube ID G6_DldYj0u. URL https://www.youtube.com/watch?v=G6_DldYj0u looks truncated.
;- or if OK :

[youtube] G6_DldYj0uQ: Downloading webpage
[download] Destination: Ye Lai Xiang-G6_DldYj0uQ.mp4

[download]   0.0% of 5.74MiB at Unknown speed ETA Unknown ETA
[download]   0.1% of 5.74MiB at Unknown speed ETA Unknown ETA
[download]   0.1% of 5.74MiB at 448.03KiB/s ETA 00:13        
[download]   0.3% of 5.74MiB at 960.06KiB/s ETA 00:06        
[download]   0.5% of 5.74MiB at  1.94MiB/s ETA 00:02         
[download]   1.1% of 5.74MiB at  3.94MiB/s ETA 00:01         
[download]   2.2% of 5.74MiB at  3.96MiB/s ETA 00:01         
[download]   4.3% of 5.74MiB at  7.95MiB/s ETA 00:00         
[download]   8.7% of 5.74MiB at  2.28MiB/s ETA 00:02         
[download]  17.4% of 5.74MiB at  2.37MiB/s ETA 00:02         
[download]  34.8% of 5.74MiB at  2.72MiB/s ETA 00:01         
[download]  69.7% of 5.74MiB at  2.61MiB/s ETA 00:00         
[download] 100.0% of 5.74MiB at  2.59MiB/s ETA 00:00         
[download] 100% of 5.74MiB in 00:02                          
*/
;=================================================================
meanwhile I tried a complicated script ...

Code: Select all

modified = 20201114
created  = 20201114
;-- example youtube-dl.exe / copy  to EDIT
;------------------------------------------------
#Warn
#Noenv
setworkingdir,%a_scriptdir%
settitlematchmode,2
Gui,2: +HwndGui
Gui,2:default
Gui,2: -DPIScale
Gui,2:color,black,black
Gui,2:Font,s12 cYellow,Lucida Console
DetectHiddenWindows On
wa:=A_screenwidth,ha:=A_screenHeight,xx:=100
;------------
filename1=YOUTUBE-DL.exe TEST
youtubedl:=a_scriptdir . "\youtube-dl.exe"         ;<<< youtube-dl.exe in a_scriptdir
fdmp4:=a_desktop . "\M_VIDEO"                      ;<<< save videos here
ifnotexist,%fdmp4%
  filecreatedir,%fdmp4%
ifexist,%youtubedl%
 {
 loop,%youtubedl%        
   SP1:=A_loopFileShortPath
 ;RunWait, %comspec% /k %sp1% -U                    ;- Update youtube-dl.exe
 }
else
 {
 run,https://youtube-dl.org/latest/youtube-dl.exe
 return
 }
var=https://www.youtube.com/watch?v=G6_DldYj0uQ    ;- URL Li Xiang Lan

x:=(wa*1)/xx,y:=(ha*1)/xx,h:=(ha*3)/xx,w:=(wa*70)/xx
Gui,2:Add, Edit  , x%x%  y%y%   h%h%   w%w% vVAR ,%VAR%
x:=(wa*72)/xx,y:=(ha*1)/xx,h:=(ha*2.5)/xx,w:=(wa*5)/xx
Gui,2:Add, Button, x%x%  y%y%  w%w%  h%h%  gSTART1,Start

x:=(wa*1)/xx,y:=(ha*10)/xx,h:=(ha*50)/xx,w:=(wa*70)/xx
Gui,2:Add,Edit, x%x%   y%y%  h%h%  w%w% vED1 cYellow,
Gui,2:add,Text,x0 y0 w0 h0 vTT
x:=(wa*.1)/xx,y:=(ha*.1)/xx,h:=(ha*70)/xx,w:=(wa*80)/xx
Gui,2:Show, x%x% y%y% w%w% h%h% ,%filename1%
GuiControl,2:Focus,TT
return
;----------------------------------------------

2Guiescape:
2Guiclose:
xx=youtube-dl.exe
process,exist,%xx%
pps:=errorlevel
if pps<>0
 Process, Close, %pid2%
exitapp
;----------------------------------------------
start1:
gui,2:submit,nohide
GuiControl,2:,ED1,
settimer,aa,900
;-------------
cmd=%sp1% %var% --verbose --youtube-skip-dash-manifest --write-auto-sub --sub-lang de -o "%fdmp4%\`%(title)s$`%(uploader)s$`%(id)s.`%(ext)s"
RunWait,%cmd%,,hide,pid2
;RunWait,%comspec% /k %cmd%,,,pid2    ;-- see original window
;-------------
sleep,2000
settimer,aa,off
try
run,%fdmp4%
return
;----------------------------------------------
aa:
gui,2:submit,nohide
WinActivate ahk_pid %PID2%
send,^a{Enter}                      ;- <<< copy hidden DOS Windows-10
ClipWait,1,1
GuiControl,2:,ED1,%Clipboard%
ControlSend, Edit2,^{end}, ahk_id %Gui%
return
;=========== END youtube-dl.exe  ========================================
pk23
Posts: 110
Joined: 24 Apr 2015, 00:49

Re: How to auto decide close cmd window or not based on results after running?

22 Nov 2020, 11:32

Sorry for this so late replying! I'm touched by you guys' warmheart help! many many thank you to @mikeyww and @garry (thought you were chinese at first but then see this post haha) :dance:
garry
Posts: 3788
Joined: 22 Dec 2013, 12:50

Re: How to auto decide close cmd window or not based on results after running?

22 Nov 2020, 16:41

@pk23
are you from Formosa /Taiwan , Macao, Hong Kong ...
most tools / electronic products / electronic cars / big things ... etc ... are from china
I use them everyday , except my swiss knife ... and stable pencil 'Caran d'ache' / ru= карандаш tr=kara taş ( black stone ) https://www.carandache.com/ch/en/

20201115 RCEP
https://en.wikipedia.org/wiki/Regional_Comprehensive_Economic_Partnership

https://en.wikipedia.org/wiki/Belt_and_Road_Initiative

---------------------
http://www.xinhuanet.com/english/2020-11/14/c_139516497.htm
Xinhua Headlines: China province with largest remaining poor population eliminates absolute poverty - Xinhua | English.news.cn
-- Southwest China's Yunnan Province, which had the country's largest remaining poor population at the end of last year, has eradicated absolute poverty.
-- All the 88 poverty-stricken counties in Yunnan have shaken off poverty.
-- Since the start of this year, 18.5 billion yuan (about 2.8 billion U.S. dollars) has been injected into industries supporting poverty reduction in the 88 counties.
---------------------
---------------------
20201015
https://www.arte.tv/en/videos/099397-000-A/china-the-wuhan-paradox/
China: The Wuhan Paradox - ARTE Reportage - Watch the full documentary | ARTE
Wuhan was little known in the West before the pandemic.
The central Chinese city the size of New York, was was a vibrant city before the pandemic.
After a strict lockdown, social life in Wuhan seems to have returned to normal with busy streets, lively restaurants and packed nightclubs.
---------------------

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], slowwd and 137 guests