开发者

Batch file: suppress command error output when used in a for loop

开发者 https://www.devze.com 2023-03-30 04:53 出处:网络
I am iterating through the command output in a for loop. Consider the following code: for /F \"tokens=1 delims=?=\" %%A in (\'set __variable\') do ( set %%A= )

I am iterating through the command output in a for loop. Consider the following code:

for /F "tokens=1 delims=?=" %%A in ('set __variable') do ( set %%A= )

Basically I am trying to clear the value of every environment variable whose name starts with "__variable". However, if no such variable is set, I get an error that says "Environment variable __variable not defined", which is not something that I would like displayed on my console. So naturally, I would change my code like this:

for 开发者_如何转开发/F "tokens=1 delims=?=" %%A in ('set __variable 2> NUL') do ( set %%A= )

But now I get a new error that says "2> was unexpected at this time." or something of that effect. Now I am stuck; is there a way for me to complete my objective without having the standard error show up on the screen?


For Windows NT 4 and later, you will need to escape the pipe and redirection symbols, which is done by prefixing them with carets ( ˆ ):

for /F "tokens=1 delims=?=" %A in ('set __variable 2^>NUL') do ( set %A= )
0

精彩评论

暂无评论...
验证码 换一张
取 消