Page 1 of 1

How to escape 2>nul [solved]

Posted: 21 Jan 2013 07:29
by abc0502
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 :?:

Re: How to escape 2>nul

Posted: 21 Jan 2013 08:42
by jeb
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

Re: How to escape 2>nul

Posted: 21 Jan 2013 08:47
by shirulkar
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. "

Re: How to escape 2>nul

Posted: 21 Jan 2013 09:07
by suresh_knv
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

Re: How to escape 2>nul

Posted: 21 Jan 2013 09:19
by abc0502
@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.