How to chain two or more commands after "commandline": "cmd.exe /k ..." #13928

Closed
opened 2026-01-31 03:56:01 +00:00 by claunia · 10 comments
Owner

Originally created by @DanyGee on GitHub (May 26, 2021).

Hi,
I can't figure out how to properly chain two commands in the following scenario 🤔
This is a line from the json file:
"commandline": "cmd.exe /k \"C:/Windows/System32/doskey.cmd\"&&\"D:/path/to/scriptsfolder/script.bat\"",
The first command is working, the second is not.
Current approach gives me an error ⚠️
The filename, directory name, or volume label syntax is incorrect. (script is 100% working when invoked manually in Terminal)

Windows Terminal
Version: 1.8.1444.0

Originally created by @DanyGee on GitHub (May 26, 2021). Hi, I can't figure out how to properly chain two commands in the following scenario 🤔 This is a line from the json file: `"commandline": "cmd.exe /k \"C:/Windows/System32/doskey.cmd\"&&\"D:/path/to/scriptsfolder/script.bat\"",` The first command is working, the second is not. Current approach gives me an error ⚠️ **The filename, directory name, or volume label syntax is incorrect.** (script is 100% working when invoked manually in Terminal) _Windows Terminal Version: 1.8.1444.0_
Author
Owner

@zadjii-msft commented on GitHub (May 26, 2021):

okay this is dumb, but what if you made a third file called do-the-thing.bat

@echo off
C:/Windows/System32/doskey.cmd
D:/path/to/scriptsfolder/script.bat\

then make the commandline cmd /k do-the-thing.bat?

Obviously this is a workaround, but it should work

@zadjii-msft commented on GitHub (May 26, 2021): okay this is dumb, but what if you made a _third_ file called `do-the-thing.bat` ``` @echo off C:/Windows/System32/doskey.cmd D:/path/to/scriptsfolder/script.bat\ ``` then make the commandline `cmd /k do-the-thing.bat`? Obviously this is a workaround, but it _should_ work
Author
Owner

@DanyGee commented on GitHub (May 26, 2021):

It - by all means - is not dumb, actually your solution works pretty well🥳
THANK YOU!

@DanyGee commented on GitHub (May 26, 2021): It - by all means - is not dumb, actually your solution works pretty well🥳 THANK YOU!
Author
Owner

@vefatica commented on GitHub (May 26, 2021):

What if someone really wanted/needed cmd.exe to start up executing the likes of this?

"a b c.bat" && "d e f.bat"

I tried a few things which didn't work.

@vefatica commented on GitHub (May 26, 2021): What if someone really wanted/needed cmd.exe to start up executing the likes of this? `"a b c.bat" && "d e f.bat"` I tried a few things which didn't work.
Author
Owner

@DHowett commented on GitHub (May 26, 2021):

I mean, I just did this:

wt -- cmd /k echo hi && echo bye

and it worked fine?

image

It also works with quoted things.

@DHowett commented on GitHub (May 26, 2021): I mean, I just did this: ``` wt -- cmd /k echo hi && echo bye ``` and it worked fine? ![image](https://user-images.githubusercontent.com/189190/119695949-50411d00-be14-11eb-9928-ff385faa7c2f.png) It also works with quoted things.
Author
Owner

@DHowett commented on GitHub (May 26, 2021):

            {
                "commandline": "cmd.exe /k echo hi && echo bye && echo \"this has quotes\" && echo \"so does this\"",
                "name": "Profile 16"
            }

output

hi
bye
"this has quotes"
"so does this"

C:\Users\duhowett>

@DHowett commented on GitHub (May 26, 2021): ```json { "commandline": "cmd.exe /k echo hi && echo bye && echo \"this has quotes\" && echo \"so does this\"", "name": "Profile 16" } ``` output ``` hi bye "this has quotes" "so does this" C:\Users\duhowett> ```
Author
Owner

@DanyGee commented on GitHub (May 26, 2021):

Does it work two *.bat files?

@DanyGee commented on GitHub (May 26, 2021): Does it work two *.bat files?
Author
Owner

@DHowett commented on GitHub (May 26, 2021):

Honestly, I can't even get it to work outside of Terminal. Look:

C:\Users\duhowett>cmd.exe /k "C:\Users\duhowett\hello_world.bat" ^&^& "C:\Users\duhowett\eat_my_shorts.bat"
The filename, directory name, or volume label syntax is incorrect.

C:\Users\duhowett>exit

Note the ^& are required to -escape the original &s. Without those, second instance of cmd runs hello_world while the first runs eat_my_shorts. Of course that will appear to work -- it's two different instances of CMD handling it for you making it look like they're running back to back. You can tell it's not right, however, because you must exit the inner instance before the second batch file happens:

C:\Users\duhowett>cmd.exe /k "C:\Users\duhowett\hello_world.bat" && "C:\Users\duhowett\eat_my_shorts.bat"

[--- CONTROL MOVES TO CHILD CMD ---]

C:\Users\duhowett>echo sup
sup

C:\Users\duhowett>
C:\Users\duhowett>exit

[--- CONTROL RETURNS TO SPAWNING CMD ---]

C:\Users\duhowett>echo pithy simpsons reference
pithy simpsons reference

If I check the commandline of the process that failed to run:

cmd.exe  /k "C:\Users\duhowett\hello_world.bat" && "C:\Users\duhowett\eat_my_shorts.bat"

that's correct alright.

So ... uh, is this supposed to work in Windows?

@DHowett commented on GitHub (May 26, 2021): Honestly, I can't even get it to work _outside_ of Terminal. Look: ``` C:\Users\duhowett>cmd.exe /k "C:\Users\duhowett\hello_world.bat" ^&^& "C:\Users\duhowett\eat_my_shorts.bat" The filename, directory name, or volume label syntax is incorrect. C:\Users\duhowett>exit ``` Note the `^&` are required to -escape the original `&`s. Without those, _second_ instance of cmd runs `hello_world` while the _**first**_ runs `eat_my_shorts`. Of course that will appear to work -- it's two different instances of CMD handling it for you making it look like they're running back to back. You can tell it's not right, however, because you must `exit` the inner instance before the second batch file happens: ``` C:\Users\duhowett>cmd.exe /k "C:\Users\duhowett\hello_world.bat" && "C:\Users\duhowett\eat_my_shorts.bat" [--- CONTROL MOVES TO CHILD CMD ---] C:\Users\duhowett>echo sup sup C:\Users\duhowett> C:\Users\duhowett>exit [--- CONTROL RETURNS TO SPAWNING CMD ---] C:\Users\duhowett>echo pithy simpsons reference pithy simpsons reference ``` If I check the commandline of the process that failed to run: ``` cmd.exe /k "C:\Users\duhowett\hello_world.bat" && "C:\Users\duhowett\eat_my_shorts.bat" ``` that's correct alright. So ... uh, is this supposed to work in _Windows_?
Author
Owner

@DanyGee commented on GitHub (May 26, 2021):

I don't know if it should. That's why I've asked, cause I needed to run 2 different things at terminal start.
Ultimately a bat calling out other scripts did the trick (just like zadjii-msft already suggested) :

@echo off
call C:/Windows/System32/doskey.cmd
call D:/path/to/the/other/script.bat
@DanyGee commented on GitHub (May 26, 2021): I don't know if it should. That's why I've asked, cause I needed to run 2 different things at terminal start. Ultimately a bat calling out other scripts did the trick (just like zadjii-msft already suggested) : ``` @echo off call C:/Windows/System32/doskey.cmd call D:/path/to/the/other/script.bat ```
Author
Owner

@vefatica commented on GitHub (May 26, 2021):

It's kind of strange. From the WinKey+R dialog, cmd /k "v:\a b.bat" && "v:\c d.bat" gives

'v:\a' is not recognized as an internal or external command,
operable program or batch file.

The problem seems to be with the first of multiple commands being a quoted file name containing spaces. If I add a kludge as the first command, it works. cmd /k echo This is a kludge! && "v:\a b.bat" && "v:\c d.bat" produces

This is a kludge!
Hello!  This is 'a b.bat'.
My PID is ...
4556
Hello!  This is 'c d.bat'.
My PID is ...
4556
@vefatica commented on GitHub (May 26, 2021): It's kind of strange. From the WinKey+R dialog, `cmd /k "v:\a b.bat" && "v:\c d.bat"` gives ``` 'v:\a' is not recognized as an internal or external command, operable program or batch file. ``` The problem seems to be with the first of multiple commands being a quoted file name containing spaces. If I add a kludge as the first command, it works. `cmd /k echo This is a kludge! && "v:\a b.bat" && "v:\c d.bat"` produces ``` This is a kludge! Hello! This is 'a b.bat'. My PID is ... 4556 Hello! This is 'c d.bat'. My PID is ... 4556 ```
Author
Owner

@vefatica commented on GitHub (May 26, 2021):

Here's another kludge.

"commandline": "cmd.exe /k call "v:\a b.bat" && call "v:\c d.bat"",

@vefatica commented on GitHub (May 26, 2021): Here's another kludge. "commandline": "cmd.exe /k call \"v:\\a b.bat\" && call \"v:\\c d.bat\"",
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#13928