0

Is there a way to apply :search=replace-syntax to %%var inside a for-loop in Batch?

I'm trying to replace all hyphens with colons inside the mac-addresses of a machine (and then use them). Unfortunately, my regular approach set var=%var:-=:% does not work inside a for loop.

    FOR /F "tokens=12 delims= " %%G IN ('ipconfig /all ^| find /I "Physical Address"') DO (
      SET var=%%G
      SET newmac=%var:-=:%
      wget https://api-server-uri/pxe/do-stuff?newmac=%newmac%
    )

The problem is that the value of %var:-=:% does not change along with %%G .

Thanks

aschipfl
  • 33,626
  • 12
  • 54
  • 99

1 Answers1

0

Thanks to Stephans comment:

    Setlocal enabledelayedexpansion
    FOR /F "tokens=12 delims= " %%G IN ('ipconfig /all ^| find /I "Physical Address"') DO (
      SET var=%%G
      wget https://api-server-uri/pxe/do-stuff?newmac=!var:-=:!
    )
  • if there is nothing else inside your `for` loop, you don't even need delayed expansion. Just `FOR /F "tokens=12 delims= " %%G IN ('ipconfig /all ^| find /I "Physical Address"') DO SET var=%%G` and `wget https://api-server-uri/pxe/do-stuff?newmac=%var:-=:%` – Stephan Mar 15 '17 at 16:07
  • @Stephan, you mean to put `wget` outside of the loop, right? obviously, this can only be done in case the loop iterates once only... – aschipfl Mar 15 '17 at 18:31
  • Try: `FOR /F "tokens=12-17 delims=- " %%G IN ('ipconfig /all ^| find /I "Physical Address"') DO wget https://api-server-uri/pxe/do-stuff?newmac=%%G:%%H:%%I:%%J:%%K:%%L` – Aacini Mar 15 '17 at 22:50