Search code examples
batch-filecmd

How to set a variable in CMD?


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 address.

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

What am I doing wrong?


Solution

  • 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.