Home > Net >  How to set a variable in CMD?
How to set a variable in CMD?

Time:01-09

I have the following command to set my own IP in a variable :

    for /F %I in ('curl http://ipecho.net/plain') do set ip=%I

When I open cmd and write (or paste) the command manually, it's working properly. The variable %ip% is set and the command

    echo %ip%

returns my IP adress.

However if I put this command in a cmd file and run the cmd file, I'm getting the error :

    //ipecho.net/plain') was unexpected.

enter image description here

Does anyone know what I'm doing wrong please ?

Thanks. Cheers,

CodePudding user response:

Like it says in the help (for /?)

To use the FOR command in a batch program, specify %%variable instead of %variable. Variable names are case sensitive, so %i is different from %I.

So, replace each %I by %%I.

Annoying, but there it is.

  • Related