Command line: Don't automatically add "cmd /c" if the command being executed is cmd
Moderators: Hacker, petermad, Stefan2, white
- ghisler(Author)
- Site Admin

- Posts: 53125
- 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
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!
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
https://www.ghisler.com
Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd
No crashing encountered anymore.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.
Fixed typo, and change capitalization and spacing to improve readability.ghisler(Author) wrote: 2026-06-08, 07:04 UTC If you have any additional test cases, please let me know!
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
Code: Select all
# Cases where command is just modified, with full path added: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:
But I left it in.Run, %comspec% /k 2>test.123
"c:\windows\system32\cmd.exe" /S/K "Run, %comspec% /k 2>test.123"
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
cmdExamples 2:
Code: Select all
cmd >test.123Run 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.
- ghisler(Author)
- Site Admin

- Posts: 53125
- 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
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 changeI 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.
cmd
to
cmd /K
in this case.
Author of Total Commander
https://www.ghisler.com
https://www.ghisler.com
Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd
But you can leave the command unchanged, can't you?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.
- ghisler(Author)
- Site Admin

- Posts: 53125
- 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
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
https://www.ghisler.com
Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd
That seems obvious.
Current implementation differentiates 3 situations:
- no cmd or cmd without /c or /k - you add "c:\windows\system32\cmd.exe" /S/K "<command>"
- cmd with /c - you add path and quotes to cmd and change /c to /K
- cmd with /k - you add path and quotes to cmd and change /k to /K
- no cmd - you add "c:\windows\system32\cmd.exe" /S/K "<command>"
- cmd with /c - you add path and quotes to cmd and change /c to /K
- cmd without /c - you add path and quotes to cmd
- ghisler(Author)
- Site Admin

- Posts: 53125
- 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
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.
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
https://www.ghisler.com
Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd
There is an edge case where there is no space behind a quoted cmd command.
For example:
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:
Another example ("|clip" should do nothing):
When using Windows Run, it adds a space and runs:
For example:
Code: Select all
"cmd"/c pause
When using Windows Run, the above command runs fine. It actually adds a space and runs:
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.exe" c pause
"C:\WINDOWS\system32\cmd"/K pause
Another example ("|clip" should do nothing):
Code: Select all
"cmd"|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" clip
Resulting in the error:"C:\WINDOWS\system32\cmd.exe" /S/K "cmd"|clip"
'cmd"|clip' is not recognized as an internal or external command,
operable program or batch file.
- ghisler(Author)
- Site Admin

- Posts: 53125
- 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
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.
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
https://www.ghisler.com
Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd
ghisler(Author) wrote: 2026-06-08, 07:04 UTC If you have any additional test cases, please let me know!
Something seems different this time.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.
Still, replacing a character in the command with a space seems bad.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.
Command on TC's command line:
Code: Select all
"c:\Program Files\Notepad++\notepad++.exe"10.txt
"c:\Program Files\Notepad++\notepad++.exe" 0.txt
- ghisler(Author)
- Site Admin

- Posts: 53125
- 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
Well, what do you expect when entering an invalid command? When I enter
""c:\Program Files\Notepad++\notepad++.exe"10.txt"
is either written incorrectly, or couldn't be found.
Noclose.exe generates the following from it:
in cmd.exe and press Enter, I get an error that the command"c:\Program Files\Notepad++\notepad++.exe"10.txt
""c:\Program Files\Notepad++\notepad++.exe"10.txt"
is either written incorrectly, or couldn't be found.
Noclose.exe generates the following from it:
and leads to the same error message."C:\WINDOWS\system32\cmd.exe" /S/K ""c:\Program Files\Notepad++\notepad++.exe"10.txt"
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).Still, replacing a character in the command with a space seems bad.
Author of Total Commander
https://www.ghisler.com
https://www.ghisler.com
- ghisler(Author)
- Site Admin

- Posts: 53125
- 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
In RC3, I'm now handling the faulty commands
I will not change anything for
and"cmd"/c pause
internally, but did not change anything for Shift+Enter."c:\Program Files\Notepad++\notepad++.exe"10.txt
I will not change anything for
because it's pointless ("cmd" without parameters doesn't close and therefore doesn't send anything useful to "clip")."cmd"|clip
Author of Total Commander
https://www.ghisler.com
https://www.ghisler.com
Re: Command line: Don't automatically add "cmd /c" if the command being executed is cmd
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).
Executed by TC when pressing Shift+Enter:ghisler(Author) wrote: 2026-06-10, 12:30 UTC I will not change anything forbecause it's pointless ("cmd" without parameters doesn't close and therefore doesn't send anything useful to "clip")."cmd"|clip
The front quote is stripped, so the command is not passed unmodified."C:\WINDOWS\system32\cmd.exe" /S/K "cmd"|clip"
This command works OK:
Code: Select all
"attrib"123
"C:\WINDOWS\system32\cmd.exe" /S/K ""attrib"123"
But the front quote is stripped for:
Code: Select all
"cmd"123
It appears that cmd is not fully recognized as a program here, but the command is not passed on unchanged either."C:\WINDOWS\system32\cmd.exe" /S/K "cmd"123"
- ghisler(Author)
- Site Admin

- Posts: 53125
- 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
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
https://www.ghisler.com
