Page 1 of 1

Getting >nul 2>&1 to work within a batch file

Posted: 27 Oct 2018 17:48
by Quisquose
How do I get the following command to work from within a batch file?

Code: Select all

doskey closeall=taskkill /IM "cmd.exe" /f >nul 2>&1
I want the output of taskkill to be suppressed. It works perfectly if typed at the command line, but when the above command is used in a batch file, you still see the output text from taskkill.

I am aware that commands behave differently when called from a batch file rather than when they're run directly, but I can't remember what needs to be done to modify the command for use in a batch file.

Re: Getting >nul 2>&1 to work within a batch file

Posted: 27 Oct 2018 21:48
by Squashman
https://ss64.com/nt/doskey.html wrote: You cannot run a Doskey macro from a batch file.

Re: Getting >nul 2>&1 to work within a batch file

Posted: 27 Oct 2018 22:37
by ShadowThief
You can, however,

Code: Select all

set "closeall=taskkill /IM "cmd.exe" /f >nul 2>&1"
%closeall%

Re: Getting >nul 2>&1 to work within a batch file

Posted: 28 Oct 2018 01:53
by Quisquose
ShadowThief wrote:
27 Oct 2018 22:37
You can, however,

Code: Select all

set "closeall=taskkill /IM "cmd.exe" /f >nul 2>&1"
%closeall%
Thank you, This now hides the output, but in order to get it to work I have to type %closeall% including the percentage marks. How can I get it to work with just the word closeall on its own?

I had a similar issue to this a long while I ago, and I did manage to successfully resolve it, but for the life of me I can't remember how I did it.

Re: Getting >nul 2>&1 to work within a batch file

Posted: 28 Oct 2018 09:32
by Aacini
This should work:

Code: Select all

doskey closeall=taskkill /IM "cmd.exe" /f ^>nul 2^>^&1
Anther way to "get it to work with just the word closeall on its own?" is creating a Batch file named closeall.bat with the desired command... :roll:

Antonio

Re: Getting >nul 2>&1 to work within a batch file

Posted: 28 Oct 2018 10:11
by Quisquose
Aacini wrote:
28 Oct 2018 09:32
This should work:

Code: Select all

doskey closeall=taskkill /IM "cmd.exe" /f ^>nul 2^>^&1
Thank you SO much, Antonio. This works perfectly, and it's exactly what I wanted. 8)

This time I will make a careful note of how it's done, so that I can use it as a template guide for achieving a similar effect for other commands in future.

Re: Getting >nul 2>&1 to work within a batch file

Posted: 28 Oct 2018 11:34
by ShadowThief
Aacini wrote:
28 Oct 2018 09:32
Anther way to "get it to work with just the word closeall on its own?" is creating a Batch file named closeall.bat with the desired command... :roll:
They'd still have to either call the secondary script or make peace with the fact that it would have to be the last command in the main script.

Re: Getting >nul 2>&1 to work within a batch file

Posted: 28 Oct 2018 14:57
by Aacini
ShadowThief wrote:
28 Oct 2018 11:34
Aacini wrote:
28 Oct 2018 09:32
Anther way to "get it to work with just the word closeall on its own?" is creating a Batch file named closeall.bat with the desired command... :roll:
They'd still have to either call the secondary script or make peace with the fact that it would have to be the last command in the main script.
As I understand it, they want to enter "closeall" from the keyboard only. There is not any Batch file involved in the use of "closeall" word. As it was said before: "You cannot run a Doskey macro from a batch file".

Antonio

Re: Getting >nul 2>&1 to work within a batch file

Posted: 28 Oct 2018 15:14
by ShadowThief
And yet the doskey solution you provided
Quisquose wrote:
28 Oct 2018 10:11
works perfectly, and it's exactly what I wanted.
which doesn't make sense because
Squashman wrote:
27 Oct 2018 21:48
https://ss64.com/nt/doskey.html wrote:You cannot run a Doskey macro from a batch file.
I'm not sure what Quisquose is looking for anymore, frankly.

Re: Getting >nul 2>&1 to work within a batch file

Posted: 28 Oct 2018 16:44
by Quisquose
Aacini wrote:
28 Oct 2018 14:57
As I understand it, they want to enter "closeall" from the keyboard only ...
Correct.

Re: Getting >nul 2>&1 to work within a batch file

Posted: 28 Oct 2018 16:48
by ShadowThief
Quisquose wrote:
27 Oct 2018 17:48
from within a batch file?
Did you change your mind when I wasn't looking?

Re: Getting >nul 2>&1 to work within a batch file

Posted: 28 Oct 2018 19:19
by Aacini
IMHO the OP have described the problem inaccurately and his question have several details...

I think he wanted to "Getting >nul 2>&1 to work within a doskey macro when the corresponding doskey command is executed in a batch file".

Although the question said that "the following command" (that is a doskey command) "works perfectly if typed at the command line", that is not true. To be clear, if you execute the following command at the command line:

Code: Select all

doskey closeall=taskkill /IM "cmd.exe" /f >nul 2>&1
... then, when you later execute the closeall macro you still see the output text from taskkill.

So in this case, there is no difference on executing the command from the command-line that from a Batch file.

The OP have not clearly separated the doskey command used to define a macro, from the closeall macro used to execute the taskkill command.

Anyway, this confusion disappear if you take into account that: "You cannot run a Doskey macro from a batch file". In other words:
  • Just the DOSKEY command can be used in a Batch file.
  • The CLOSEALL macro can only be used from the keybard, not from a Batch file.
Antonio

Re: Getting >nul 2>&1 to work within a batch file

Posted: 28 Oct 2018 19:39
by ShadowThief
That makes more sense, thanks.