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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

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

#1 Post by Quisquose » 27 Oct 2018 17:48

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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

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

#2 Post by Squashman » 27 Oct 2018 21:48

https://ss64.com/nt/doskey.html wrote: You cannot run a Doskey macro from a batch file.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

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

#3 Post by ShadowThief » 27 Oct 2018 22:37

You can, however,

Code: Select all

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

Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

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

#4 Post by Quisquose » 28 Oct 2018 01:53

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.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#5 Post by Aacini » 28 Oct 2018 09:32

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

Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

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

#6 Post by Quisquose » 28 Oct 2018 10:11

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.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

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

#7 Post by ShadowThief » 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.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#8 Post by Aacini » 28 Oct 2018 14:57

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

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

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

#9 Post by ShadowThief » 28 Oct 2018 15:14

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.

Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

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

#10 Post by Quisquose » 28 Oct 2018 16:44

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

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

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

#11 Post by ShadowThief » 28 Oct 2018 16:48

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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#12 Post by Aacini » 28 Oct 2018 19:19

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

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

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

#13 Post by ShadowThief » 28 Oct 2018 19:39

That makes more sense, thanks.

Post Reply