-1

I'm looking to create a batch file that will perform a pretty specific function:

Search the registry key "\HKLM\SYSTEM\CurrentControlSet\Control\Class{4D36E972-E325-11CE-BFC1-08002bE10318}"

Find all subkeys that contain a specific text string, in this case "Intel" in "DriverDesc" value

Change the "PnPCapabilities" value data to "38" (hex) only in the keys that contain "Intel" in "DriverDesc" value

I know how to do each of these things separately, but I'm not quite able to get them all to work together in a single batch. My knowledge of the batch language is still too limited to know how to do it properly on my own, or to decipher and modify an existing batch.

To make things less confusing, I'm trying to disable the power management options for a pair of Intel NIC cards. I'd like to make a batch that will automatically do this on any machine, regardless of what other NICs are present, or in which order they were installed.

Any help would be greatly appreciated.

The following is what I've found, tried and failed:
EDIT: New batch
EDIT2: New batch, working

:start
setlocal ENABLEDELAYEDEXPANSION
set qry=reg query "HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}" /s /v DriverDesc
for /f "Tokens=*" %%p in ('%qry%') do (
 set var=%%p
 set var=!var:^&=!
 set var=!var:^)=!
 set var=!var:^(=!
 call :parse
)
endlocal
goto :EOF
:parse
if /i "%var:~0,5%" NEQ "HKEY_" goto parse1
set key=%var%
set key=%key:HKEY_LOCAL_MACHINE=HKLM%
goto :EOF
:parse1
setlocal ENABLEEXTENSIONS
for /f "Tokens=*" %%f in ('@echo %var%^|findstr /i /c:"Intel(R)"') do (
  if defined key reg add %key% /v PnPCapabilities /t REG_DWORD /d 56 /f&set key=
)
endlocal >nul 2>&1

Fixed so that it does what is intended.

Matt M
  • 1
  • 1
  • 2
  • Well, you aren't really doing anything with the output of the `for` statement that calls the `if` statement. You go through a lot of trouble to get a particular output in `%%f`, but then fail to use it. – James K Sep 22 '12 at 11:59

2 Answers2

0

Well, for one thing, you don't put a \ in front of HKEY, and for another thing \Class{4D36E972-E325-11CE-BFC1-08002bE10318} should be \Class\{4D36E972-E325-11CE-BFC1-08002bE10318}. Also, you don't really need the double quotes.

So make your third line:

set qry=reg query HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318} /s

This line seems problematic:

call :parse2>nul 2>&1

Likely to call parse and pipe stderr to nul and then redirect it to stdout. At the very least put a space between :parse2 and >nul 2>&1.

You could eliminate the problem by eliminating parse2 by pulling it into parse1 and appending your >nul 2>&1 to the end of your for or if statement, like so:

:parse1
for /f "Tokens=*" %%f in ('@echo %line%^|findstr /i /c:"Intel(R)" ^|findstr /i /c:"DriverDesc"') do (
  if defined key reg add "\%key%" /v PnPCapabilities /t REG_DWORD /d 56 /f&set key=
) >nul 2>&1

That's everything that jumps out at me. But before you pump that stuff back into your registry, I reccomend that you pipe it into a file first. That way you can check to make sure that everything is formatted properly, and you can even cut a few individual lines from the file and paste it into the command prompt to see if reg reacts in the way you anticipate. That way you don't risk screwing up your registry on an industrial scale if you made a mistake.

Oh yes, and make a backup of at-least the part of the registry you're going to modify so you can restore it to it's exact previous condition. You know, just in case. :D


EDIT: In answer to your updated question:

Your if statement never runs because somehow %line% is being set to line: Look at your "output".

For expands the variable !line! and then runs @echo line: |findstr /i /c:"vmxnet3" |findstr /i /c:"DriverDesc" which results in a blank line because the literal string "line:" will not match one of, let alone both of your search strings.

Since for ignores empty lines, your if statement never gets run.

For's evaluation must result in data for the commands after DO to be run.

James K
  • 4,005
  • 4
  • 20
  • 28
  • Thanks for your help. I've updated the initial question with more info and results after modifying the batch as suggested. – Matt M Sep 21 '12 at 14:21
  • Thanks again for your help. As I mentioned above, I'm not very experience with batch files. Though with your help I'm starting to understand it a bit better. Have you any ideas on why my `%line%` is being set to `line:`? I'm not really sure how the `if /i "%line:~0,5%"` portion works. Specifically the `"%line:~0,5%"` – Matt M Sep 25 '12 at 16:11
  • @MattM - Well, I'm assuming that somehow you par it down to `line: `. I suggest that you put `ECHO !line!` after every statement cutting things out of `line` . That's the easiest way to find unexpected errors. The code `"%line:~0,5%"` puts the first 5 characters if %line% inside a pair of quotes. So if `set line=1234567890` then `"%line:~0,5%"` == `"12345"`. Also `%line:~0,1%` == `1`, `%line:~1,1%` == `2` and `%line~2,1%` == `3`. Type `set /?` at the command line for a more in-depth explanation. – James K Sep 25 '12 at 20:48
  • Figured it out. Thanks again for your help. – Matt M Sep 26 '12 at 16:01
0

The following is the working batch. Thanks to James K for helping me get to the right direction.

:start
setlocal ENABLEDELAYEDEXPANSION
    set qry=reg query "HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}" /s /v DriverDesc
    for /f "Tokens=*" %%p in ('%qry%') do (
        set var=%%p
        set var=!var:^&=!
        set var=!var:^)=!
        set var=!var:^(=!
        call :parse
    )
endlocal
goto :EOF
:parse
if /i "%var:~0,5%" NEQ "HKEY_" goto parse1

set key=%var%
set key=%key:HKEY_LOCAL_MACHINE=HKLM%
goto :EOF

:parse1
setlocal ENABLEEXTENSIONS
    for /f "Tokens=*" %%f in ('@echo %var%^|findstr /i /c:"Intel(R)"') do (
        if defined key reg add %key% /v PnPCapabilities /t REG_DWORD /d 56 /f&set key=
    )
endlocal >nul 2>&1
Jake1164
  • 12,291
  • 6
  • 47
  • 64
Matt M
  • 1
  • 1
  • 2