6

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?

0

1 Answer 1

14

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= )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.