How to escape 2>nul [solved]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

How to escape 2>nul [solved]

#1 Post by abc0502 » 21 Jan 2013 07:29

How can I Escape the "2>nul" in this Command

Code: Select all

TASKLIST /nh /fi "imagename eq cmd.exe" 2>nul


My Batch will Create another batch that will have the above command in a For command, I use This:

Code: Select all

@Echo OFF
(
Echo.@Echo OFF
Echo.SETLOCAL
Echo.FOR /F "tokens=2" %%%%a IN ^('TASKLIST /nh /fi "imagename eq cmd.exe" 2^>nul'^) DO SET Mpid=%%%%a
Echo.Echo %%Mpid%%
Echo.ENDLOCAL
Echo.Pause
)>"new.bat"

The 2^>nul is already escaped in the For command "i mean here the > sign", how can i re escabe it again so it will be created in the new batch like this:

Code: Select all

@Echo OFF
SETLOCAL
FOR /F "tokens=2" %%a IN ('TASKLIST /nh /fi "imagename eq cmd.exe" 2^>nul') DO SET Mpid=%%a
Echo %Mpid%
ENDLOCAL
Pause


I was able to get it to work , but there is 1 extra ^ sign and i can't make them go away.
The rule is % and ^ must be multiplied by 4, so it will be %%%% and ^^^^, but when escaping the > sign the last one from the ^^^^ escape the next ^ sign that should be escaping the > , it did it's work and escaped the > sign but also was written in the new file. :? :roll:

Code: Select all

(
Echo.@Echo OFF
Echo.SETLOCAL
Echo.FOR /F "tokens=2" %%%%a IN ^('TASKLIST /nh /fi "imagename eq cmd.exe" 2^^^^^>nul'^) DO SET pid=%%%%a
Echo.Echo %%pid%%
Echo.ENDLOCAL
)>2nd.bat
I know i can remove the whole "2^>nul" from the command "and that what i did" but was wondering how this thing can be escaped ... any idea :?:
Last edited by abc0502 on 21 Jan 2013 09:20, edited 2 times in total.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: How to escape 2>nul

#2 Post by jeb » 21 Jan 2013 08:42

Hi abc0502,

you should use three carets.

Code: Select all

Echo.FOR /F "tokens=2" %%%%a IN ^('TASKLIST /nh /fi "imagename eq cmd.exe" 2^^^>nul'^) DO SET Mpid=%%%%a


The first caret escapes the second, the third caret escapes the greater sign.

jeb

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: How to escape 2>nul

#3 Post by shirulkar » 21 Jan 2013 08:47

Hi,

@Echo OFF
(
Echo.@Echo OFF
Echo.SETLOCAL
Echo.FOR /F "tokens=2" %%%%a IN ^('TASKLIST /nh /fi "imagename eq cmd.exe" 2^^^>nul'^) DO SET Mpid=%%%%a
Echo.Echo %%Mpid%%
Echo.ENDLOCAL
Echo.Pause
)>"new.bat"

Above code will write be code in "new.bat" file as you want.

@Echo OFF
SETLOCAL
FOR /F "tokens=2" %%a IN ('TASKLIST /nh /fi "imagename eq cmd.exe" 2^>nul') DO SET Mpid=%%a
Echo %Mpid%
ENDLOCAL
Pause

Can you please tell me more clear the last point

"I was able to get it to work , but there is 1 extra ^ sign and i can't make them go away.
The rule is % and ^ must be multiplied by 4, so it will be %%%% and ^^^^, but when escaping the > sign the last one from the ^^^^ escape the next ^ sign that should be escaping the > , it did it's work and escaped the > sign but also was written in the new file. "

suresh_knv
Posts: 5
Joined: 21 Jan 2013 07:09

Re: How to escape 2>nul

#4 Post by suresh_knv » 21 Jan 2013 09:07

Hi abc0502,

Echo.FOR /F "tokens=2" %%%%a IN ^('TASKLIST /nh /fi "imagename eq cmd.exe" 2^^^>nul'^) DO SET Mpid=%%%%a

There is no rule of four ^ or four % for every special character to be printed we need 1 ^ before the special character here for > this to be printed we need ^> and for ^ we need 1 more so in total ^^^ these are enough to display the answer as you like

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: How to escape 2>nul

#5 Post by abc0502 » 21 Jan 2013 09:19

@jeb, thanks alot, i didn't know it was that easy :oops:
thanks again.

@suresh_knv, thanks, i now realized that :)

@shirulkar,
well, it's was just an explaination to what i thought is happening,
It's not that important, but the important thing here is how you can escape characters/signs like this " ^ % | & ( ) < > in your code when needed.

Post Reply