Cannot launch external program (wrong escape characters?)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jmorr
Posts: 23
Joined: 23 Mar 2022, 15:26

Cannot launch external program (wrong escape characters?)

Post by jmorr » 04 Oct 2022, 02:49

At one stage of a script I want to process some video files using MKVToolNix. Without any escape characters, the command might look like this:

C:\Program Files\MKVToolNix\mkvmerge.exe --ui-language en --output C:\MyDir\MyVideo.mkv --no-video --no-track-tags --no-global-tags --language 1:en --track-name 1:simple_aac --default-track 1:yes ( C:\MyDir\MyVideo.mka )

If I put that in quotes (one set of quotes around the whole thing) and run it, I get "The system cannot find the file specified".

If I get rid of all the switches and just enclose the .exe in quotes, a CMD window opens, something flashes past and then the window closes. The same happens if I put the switches in a separate string and concatenate them on the Run line.

If I try to run MKVToolnix via %ComSpec%, a CMD window opens but nothing else happens.

If I run %ComSpec% and send the program name and switches on a separate line, the \s in the paths are converted to #s so the exe is not found (maybe this the reason I get a file not found error when I try to run the whole thing directly from the script). I have tried various combinations of escape characters but no dice.

Any advice as to what I should do next?

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Cannot launch external program (wrong escape characters?)

Post by mikeyww » 04 Oct 2022, 06:31

Test your command line in a separate Windows batch file first. Ensure that it works.

When you Run a command-line interface in AHK, the command window is displayed. You can hide it with the Run command's Hide parameter.

For most AHK scripts using a command-line interface and then subsequent commands, using RunWait is preferred to Run, because the user usually wants the command to finish before continuing the script.

Feel free to post your scripts in your replies. You might get a faster resolution to your problem.

garry
Posts: 3720
Joined: 22 Dec 2013, 12:50

Re: Cannot launch external program (wrong escape characters?)

Post by garry » 04 Oct 2022, 06:40

an example with ffmpeg , convert mp4 to mkv
with > Runwait,%comspec% /k ... see what happens ( /k means keep DOS window open , must close manually )

Code: Select all

;-- mp4 to mkv -------------
ffmpeg64    := a_scriptdir . "\FFMPEG_3264\ffmpeg_64.exe"  ;- EDIT : I see have a problem if path contains a space like FFMPEG 3264
srcpath     := a_scriptdir . "\Video_Source"                 ;- xx.mp4 files are here
destfolder  := a_scriptdir . "\Video_DEST                    ;- xx.mkv files get here
starttime   := A_TickCount
i:=0
Loop, Files,%srcPath%\*.mp4
 {
 src:=a_loopfilefullpath
 if src=
    break
 i++	
 SplitPath,src, name, dir, ext, name_no_ext, drive   
 dst:=destfolder . "\" . name_no_ext . ".mkv" 
 Runwait,%comspec% /c %ffmpeg64% -i "%src%" -c:a copy -s hd720 "%dst%",,hide   ;- with hide don't see the DOS window
 }
aat:=(A_TickCount - starttime)//1000 
msgbox, 262208,CONVERT ,Total Files = %i%`nCONVERT MP4 to MKV ENDED in %aat% seconds
run,%destfolder%
exitapp
2nd example , create xy.lnk

Code: Select all

;-- ffmpeg convert MP4 to MKV ----------------
;-
setworkingdir,%a_scriptdir%
srcpath     := a_scriptdir . "\Video_Source"
destfolder  := a_scriptdir . "\Video_DEST"
starttime   := A_TickCount

;---  create a shortcut .lnk ( not in same folder as ffmpeg_64.exe )   ;- create xy.lnk
from     :=a_scriptdir . "\FFMPEG 3264\ffmpeg_64.exe"                         ;- problem is the SPACE in path
SplitPath,from, name, dir, ext, name_no_ext, drive
ffmpegx  :=a_scriptdir . "\" . name_no_ext . ".lnk"
ifnotexist,%ffmpegx%
  FileCreateShortcut,%from%,%ffmpegx%
;--
i:=0
Loop, Files,%srcPath%\*.mp4
 {
 src:=a_loopfilefullpath
 if src=
    break
 SplitPath,src, name, dir, ext, name_no_ext, drive   
 dst:=destfolder . "\" . name_no_ext . ".mkv" 
 ifexist, %dst%
    continue
 i++	
 Runwait,%comspec% /k %ffmpegx% -i "%src%" -c:a copy -s hd720 "%dst%"
 }
aat:=(A_TickCount - starttime)//1000 
msgbox, 262208,CONVERT ,Total Files = %i%`nCONVERT MP4 to MKV ENDED in %aat% seconds
run,%destfolder%
exitapp

jmorr
Posts: 23
Joined: 23 Mar 2022, 15:26

Re: Cannot launch external program (wrong escape characters?)

Post by jmorr » 06 Oct 2022, 03:58

garry wrote:
04 Oct 2022, 06:40
...
with > Runwait,%comspec% /k ... see what happens ( /k means keep DOS window open , must close manually )
That was the key, thanks a lot. Once I could see what was going on I was able to fix it.

Post Reply

Return to “Ask for Help (v1)”