Computer Hope

Microsoft => Microsoft DOS => Topic started by: klaphat on February 05, 2017, 02:30:10 PM

Title: Help Im Stuck with Batch Variables in Loops
Post by: klaphat on February 05, 2017, 02:30:10 PM
First of all here is my script.

@echo off

FOR /F "tokens=2 delims==" %%a in ('WMIC NIC where "Manufacturer='Realtek' and PNPDeviceID like 'USB%%'" GET GUID /VALUE') DO ( SET GUID=%%a
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetDWORDValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\%GUID%" sValueName="EnableDHCP" uValue="1"
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetMultiStringValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\%GUID%" sValueName="IPAddress" sValue=""
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetMultiStringValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\%GUID%" sValueName="SubnetMask" sValue=""
)

all my commands work.. so to speak im struggeling with my variable. If i echo it, it returns the right value, if i change my variable with the actual value my command works and i can tell by the return value my commands are giving me that my loop works to. I tried different approaches but the clock is running late and im getting blinder by the hour so please help.
Title: Re: Help Im Stuck with Batch Variables in Loops
Post by: Squashman on February 05, 2017, 07:32:42 PM
You are inside a code block. You you need to enable delayed expansion and reference your variables with exclamation points instead of the percent symbol.  But there is no need for you to set the FOR variable to an environmental variable.  Just use the FOR variable in your commands.
Title: Re: Help Im Stuck with Batch Variables in Loops
Post by: klaphat on February 06, 2017, 06:13:32 AM
First of all thanks for replying... I tried that already I updated the script to show how i did it just in case i made an error.
it didnt work but thanks for trying though :D

@echo off
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=2 delims==" %%a in ('WMIC NIC where "Manufacturer='Realtek' and PNPDeviceID like 'USB%%'" GET GUID /VALUE') DO (   
   SET GUID=%%a
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetDWORDValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\!GUID!" sValueName="EnableDHCP" uValue="1"
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetMultiStringValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\!GUID!" sValueName="IPAddress" sValue=""
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetMultiStringValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\!GUID!" sValueName="SubnetMask" sValue=""
)
ENDLOCAL
Title: Re: Help Im Stuck with Batch Variables in Loops
Post by: Squashman on February 06, 2017, 06:57:50 AM
No clue.  When I use your code I get the correct output for my my Intel NIC.
Code: [Select]
@echo off
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=2 delims==" %%a in ('WMIC NIC where "Manufacturer='Intel'" GET GUID /VALUE') DO (
   SET GUID=%%a
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetDWORDValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\!GUID!" sValueName="EnableDHCP" uValue="1"
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetMultiStringValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\!GUID!" sValueName="IPAddress" sValue=""
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetMultiStringValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\!GUID!" sValueName="SubnetMask" sValue=""
)
ENDLOCAL
pause
Output
Code: [Select]
WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetDWORDValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\{63C9BB9F-7582-43F7-8950-C524E83018B7}\" sValueName="EnableDHCP" uValue="1"
WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetMultiStringValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\{63C9BB9F-7582-43F7-8950-C524E83018B7}" sValueName="IPAddress" sValue=""
WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetMultiStringValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\{63C9BB9F-7582-43F7-8950-C524E83018B7}" sValueName="SubnetMask" sValue=""

But like I said earlier.  Why not just use the FOR variable directly.
Code: [Select]
@echo off
FOR /F "tokens=2 delims==" %%a in ('WMIC NIC where "Manufacturer='Realtek' and PNPDeviceID like 'USB%%'" GET GUID /VALUE') DO (   
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetDWORDValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\%%a" sValueName="EnableDHCP" uValue="1"
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetMultiStringValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\%%a" sValueName="IPAddress" sValue=""
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetMultiStringValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\%%a" sValueName="SubnetMask" sValue=""
)
Title: Re: Help Im Stuck with Batch Variables in Loops
Post by: klaphat on February 06, 2017, 04:04:44 PM
Hey again thanks and sorry for my late reply.
I tried Both... If i setlocal enabledelayedexpansion i need to set a variable in order to change percentage to exclamation marks on my variables as far
as i know. I also tried to use the for variable directly, in that case i dont need to enabledelayedexpansion and to set a variable. I like this approach better since it keeps the script "cleaner" but either way the result stays the same :(. if i for an example echo %%a i can see that my variable contains the right input. if i change my variable to the actual text, my commands run as they should. it is as if this sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\{63C9BB9F-7582-43F7-8950-C524E83018B7}" and this sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\%%a" is not the same... i know if you run the script it will say that method execution successful
with a returnvalue of 2 the returnvalue should be 0 for it to be successful though.
again thanks for your effort Squashman it is appreciated :)
Title: Re: Help Im Stuck with Batch Variables in Loops
Post by: klaphat on February 07, 2017, 04:17:35 PM
I did manage to resolve my issue the script below works. It turns out that my variable didnt like curly parentheses.
Thanks for your help Squashman for pointing in the right direction :)

@echo off
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=2 delims==" %%a in ('WMIC NIC where "Manufacturer='Intel Corporation'" GET GUID /VALUE') DO ( set GUID=%%a
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetDWORDValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\{!GUID:~1,36!}" sValueName="EnableDHCP" uValue="1"
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetMultiStringValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\{!GUID:~1,36!}" sValueName="IPAddress" sValue=""
   WMIC /NAMESPACE:\\root\default Class StdRegProv Call SetMultiStringValue hDefKey="&H80000002" sSubKeyName="SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\{!GUID:~1,36!}" sValueName="SubnetMask" sValue=""
)
ENDLOCAL