Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Here you can propose new features, make suggestions etc.

Moderators: petermad, Stefan2, Hacker

User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 53145
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Post by *ghisler(Author) »

Edit: I found the problem, it was a logical error when setting the ending 0 byte.

Please use beta 8 instead:
https://plugins.ghisler.com/addons/noclose_b8.zip

If you have any additional test cases, please let me know!
Author of Total Commander
https://www.ghisler.com
User avatar
white
Power Member
Power Member
Posts: 6941
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Post by *white »

ghisler(Author) wrote: 2026-06-08, 07:04 UTC Edit: I found the problem, it was a logical error when setting the ending 0 byte.
No crashing encountered anymore.
ghisler(Author) wrote: 2026-06-08, 07:04 UTC If you have any additional test cases, please let me know!
Fixed typo, and change capitalization and spacing to improve readability.
Changed "#" to "###" to improve readability.
Moved

Code: Select all

cmd /c echo ==Bare directory list== & dir /b & pause
"c:\windows\system32\cmd" /K echo ==Bare directory list== & dir /b & pause
to section

Code: Select all

#  Cases where command is just modified, with full path added:
Added some cases with redirection characters that are good to test on the command line without using Shift+Enter.
Added cases where there is no space behind /C or /K, and with /q/d instead of /s.

I don't know why you included an ahk line:
Run, %comspec% /k 2>test.123
"c:\windows\system32\cmd.exe" /S/K "Run, %comspec% /k 2>test.123"
But I left it in.

testcases.txt:

Code: Select all

###  Perform these tests with noclose.exe /Tc:\path\testcases.txt
###  Each test consists of two lines: 1 line input to noclose.exe, 1 line the expected output


###  Cases where full path to cmd.exe is put in front of command:

echo "test"
"c:\windows\system32\cmd.exe" /S/K "echo "test""

echo ==Bare directory list== & dir /b & pause
"c:\windows\system32\cmd.exe" /S/K "echo ==Bare directory list== & dir /b & pause"

echo ==Bare directory list== & dir /b & pause & rem |test
"c:\windows\system32\cmd.exe" /S/K "echo ==Bare directory list== & dir /b & pause & rem |test"

echo ==Bare directory list== & dir /b & pause & rem <test1 >test2
"c:\windows\system32\cmd.exe" /S/K "echo ==Bare directory list== & dir /b & pause & rem <test1 >test2"

Run, %comspec% /k 2>test.123
"c:\windows\system32\cmd.exe" /S/K "Run, %comspec% /k 2>test.123"


###  Cases where command is just modified, with full path added:

cmd /c echo ==Bare directory list== & dir /b & pause
"c:\windows\system32\cmd" /K echo ==Bare directory list== & dir /b & pause

cmd /c 2>test.123
"c:\windows\system32\cmd" /K 2>test.123

cmd /c "c:\Program Files\Notepad++\notepad++.exe"
"c:\windows\system32\cmd" /K "c:\Program Files\Notepad++\notepad++.exe"

cmd /s/c "dir"
"c:\windows\system32\cmd" /s/K "dir"
cmd.exe /s/c "dir"
"c:\windows\system32\cmd.exe" /s/K "dir"
cmd.exe /q/d/cdir
"c:\windows\system32\cmd.exe" /q/d/Kdir

cmd /s/k "dir"
"c:\windows\system32\cmd" /s/K "dir"
cmd.exe /s/k "dir"
"c:\windows\system32\cmd.exe" /s/K "dir"
cmd.exe /q/d/kdir
"c:\windows\system32\cmd.exe" /q/d/Kdir


###  Cases where command is already in double quotes:

"cmd" /c pause & rem >nul
"c:\windows\system32\cmd" /K pause & rem >nul
"cmd" /s/c "dir"
"c:\windows\system32\cmd" /s/K "dir"
"cmd.exe" /s/c "dir"
"c:\windows\system32\cmd.exe" /s/K "dir"

"cmd" /s/k "dir"
"c:\windows\system32\cmd" /s/K "dir"
"cmd.exe" /s/k "dir"
"c:\windows\system32\cmd.exe" /s/K "dir"


###  Cases where full path is already provided:

c:\windows\system32\cmd /s/c "dir"
c:\windows\system32\cmd /s/k "dir"
c:\windows\system32\cmd.exe /s/c "dir"
c:\windows\system32\cmd.exe /s/k "dir"

c:\windows\system32\cmd /s/k "dir"
c:\windows\system32\cmd /s/k "dir"
c:\windows\system32\cmd.exe /s/k "dir"
c:\windows\system32\cmd.exe /s/k "dir"


###  Cases where full path in double quotes is already provided:

"c:\windows\system32\cmd" /s/c "dir"
"c:\windows\system32\cmd" /s/k "dir"
"c:\windows\system32\cmd.exe" /s/c "dir"
"c:\windows\system32\cmd.exe" /s/k "dir"

"c:\windows\system32\cmd" /s/k "dir"
"c:\windows\system32\cmd" /s/k "dir"
"c:\windows\system32\cmd.exe" /s/k "dir"
"c:\windows\system32\cmd.exe" /s/k "dir"



I wonder about when the user uses "cmd" without /c or /k. It seems to me that an extra cmd never needs to be added when the command being executed is cmd.

Examples 1:

Code: Select all

cmd
Pressing Shift+Enter causes an extra cmd to be added, and when the user uses "Exit" to close the window, he needs to issue the Exit command twice.

Examples 2:

Code: Select all

cmd >test.123
Run this command on TC's command line using Enter: cmd is started, the file test.123 is not created.
Run this command on TC's command line using Shift+Enter (and type Exit): cmd is started twice, one time with redirected output to the file test.123.
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 53145
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Post by *ghisler(Author) »

I wonder about when the user uses "cmd" without /c or /k. It seems to me that an extra cmd never needs to be added when the command being executed is cmd.
Hmm, the whole point of noclose.exe is to add the /K parameter to cmd, but you can't do that when there are no parameters like the two cases you mentioned. That's why I'm putting cmd /S/K in front in these cases. I can't just change
cmd
to
cmd /K
in this case.
Author of Total Commander
https://www.ghisler.com
User avatar
white
Power Member
Power Member
Posts: 6941
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Post by *white »

ghisler(Author) wrote: 2026-06-08, 15:34 UTC Hmm, the whole point of noclose.exe is to add the /K parameter to cmd, but you can't do that when there are no parameters like the two cases you mentioned. That's why I'm putting cmd /S/K in front in these cases. I can't just change
cmd
to
cmd /K
in this case.
But you can leave the command unchanged, can't you?
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 53145
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Post by *ghisler(Author) »

But how to detect when to do that and when not? Currently I check whether the command starts with cmd/cmd.exe with or without full path, and whether it has either the parameter /C or /K. If yes, I modify the command, if not, I put cmd /S/K in front. In which cases should cmd be left alone?
Author of Total Commander
https://www.ghisler.com
User avatar
white
Power Member
Power Member
Posts: 6941
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Post by *white »

ghisler(Author) wrote: 2026-06-09, 08:26 UTC But how to detect when to do that and when not?
That seems obvious.

Current implementation differentiates 3 situations:
  1. no cmd or cmd without /c or /k - you add "c:\windows\system32\cmd.exe" /S/K "<command>"
  2. cmd with /c - you add path and quotes to cmd and change /c to /K
  3. cmd with /k - you add path and quotes to cmd and change /k to /K
Wished implementation:
  1. no cmd - you add "c:\windows\system32\cmd.exe" /S/K "<command>"
  2. cmd with /c - you add path and quotes to cmd and change /c to /K
  3. cmd without /c - you add path and quotes to cmd
(Note: In the code, you still have to check for both /c and /k to correctly determine the end of the parameter list.)
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 53145
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Post by *ghisler(Author) »

OK, please try beta 9:
https://plugins.ghisler.com/addons/noclose_b9.zip

I have included your test cases plus a few more to handle cmd without /C or /K switches. Please let me know if you find any problem cases.
Author of Total Commander
https://www.ghisler.com
User avatar
white
Power Member
Power Member
Posts: 6941
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Post by *white »

There is an edge case where there is no space behind a quoted cmd command.
For example:

Code: Select all

"cmd"/c pause
The quote character acts also as a separator here.
When using Windows Run, the above command runs fine. It actually adds a space and runs:
"C:\WINDOWS\system32\cmd.exe" /c pause
On TC's command line (using Enter), it fails and executes:
"C:\Windows\System32\cmd.exe" c pause
On TC's command line using Shift+Enter, it works and executes:
"C:\WINDOWS\system32\cmd"/K pause

Another example ("|clip" should do nothing):

Code: Select all

"cmd"|clip
When using Windows Run, it adds a space and runs:
"C:\WINDOWS\system32\cmd.exe" |clip
On TC's command line (using Enter), the pipe character is replaced by a space:
"C:\Windows\System32\cmd.exe" clip
On TC's command line using Shift+Enter, it fails and executes:
"C:\WINDOWS\system32\cmd.exe" /S/K "cmd"|clip"
Resulting in the error:
'cmd"|clip' is not recognized as an internal or external command,
operable program or batch file.
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 53145
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Post by *ghisler(Author) »

Sorry, I don't know what exactly is allowed as separator and what not, so I can't support them all.
You can come up with the separator of the week and keep this going forever.
No one will ever willingly enter
"cmd"/c pause
And if they do, they will get random results.
Author of Total Commander
https://www.ghisler.com
User avatar
white
Power Member
Power Member
Posts: 6941
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Post by *white »

ghisler(Author) wrote: 2026-06-07, 09:39 UTC Please check whether I missed any test cases!
ghisler(Author) wrote: 2026-06-08, 07:04 UTC If you have any additional test cases, please let me know!
ghisler(Author) wrote: 2026-06-09, 13:19 UTC Please let me know if you find any problem cases.
ghisler(Author) wrote: 2026-06-09, 19:54 UTC Sorry, I don't know what exactly is allowed as separator and what not, so I can't support them all.
You can come up with the separator of the week and keep this going forever.
Something seems different this time.
ghisler(Author) wrote: 2026-06-09, 19:54 UTC No one will ever willingly enter
"cmd"/c pause
And if they do, they will get random results.
Still, replacing a character in the command with a space seems bad.
Command on TC's command line:

Code: Select all

"c:\Program Files\Notepad++\notepad++.exe"10.txt
TC executes (when pressing Enter):
"c:\Program Files\Notepad++\notepad++.exe" 0.txt
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 53145
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Post by *ghisler(Author) »

Well, what do you expect when entering an invalid command? When I enter
"c:\Program Files\Notepad++\notepad++.exe"10.txt
in cmd.exe and press Enter, I get an error that the command
""c:\Program Files\Notepad++\notepad++.exe"10.txt"
is either written incorrectly, or couldn't be found.

Noclose.exe generates the following from it:
"C:\WINDOWS\system32\cmd.exe" /S/K ""c:\Program Files\Notepad++\notepad++.exe"10.txt"
and leads to the same error message.
Still, replacing a character in the command with a space seems bad.
That's not what happens: The command line is split into program and parameters. When there is a double quote at the start, it means that the program is in double quotes, and the parameters are behind it, separated by a space. That's why the first character after the double quote is ignored. I will check whether it's a space or not, but then it will work with Enter but not with Shift+Enter (noclose.exe does not scan the command line for errors, it passes it to cmd /S/K unmodified as it should).
Author of Total Commander
https://www.ghisler.com
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 53145
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Post by *ghisler(Author) »

In RC3, I'm now handling the faulty commands
"cmd"/c pause
and
"c:\Program Files\Notepad++\notepad++.exe"10.txt
internally, but did not change anything for Shift+Enter.

I will not change anything for
"cmd"|clip
because it's pointless ("cmd" without parameters doesn't close and therefore doesn't send anything useful to "clip").
Author of Total Commander
https://www.ghisler.com
User avatar
white
Power Member
Power Member
Posts: 6941
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Post by *white »

ghisler(Author) wrote: 2026-06-10, 07:56 UTC I will check whether it's a space or not, but then it will work with Enter but not with Shift+Enter (noclose.exe does not scan the command line for errors, it passes it to cmd /S/K unmodified as it should).
ghisler(Author) wrote: 2026-06-10, 12:30 UTC I will not change anything for
"cmd"|clip
because it's pointless ("cmd" without parameters doesn't close and therefore doesn't send anything useful to "clip").
Executed by TC when pressing Shift+Enter:
"C:\WINDOWS\system32\cmd.exe" /S/K "cmd"|clip"
The front quote is stripped, so the command is not passed unmodified.


This command works OK:

Code: Select all

"attrib"123
Executed by TC when pressing Shift+Enter:
"C:\WINDOWS\system32\cmd.exe" /S/K ""attrib"123"

But the front quote is stripped for:

Code: Select all

"cmd"123
Executed by TC when pressing Shift+Enter:
"C:\WINDOWS\system32\cmd.exe" /S/K "cmd"123"
It appears that cmd is not fully recognized as a program here, but the command is not passed on unchanged either.
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 53145
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd

Post by *ghisler(Author) »

None of these commands make any sense (they would not return any valid results), so it doesn't matter whether I fix them or not. Are there actually any commands left which do make sense and don't work correctly?
Author of Total Commander
https://www.ghisler.com
Post Reply