How can I do it better instead of adding a lot of duplicate same command for the purpose?

Discussion in 'Scripting' started by RemixPL1994, May 9, 2023.

  1. RemixPL1994

    RemixPL1994 MDL Junior Member

    Jul 16, 2017
    54
    6
    0
    #1 RemixPL1994, May 9, 2023
    Last edited: May 9, 2023
    Hello, how can I do it better instead of adding a lot of duplicate same command for the purpose?

    I have a command:

    REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0000" /v PnPCapabilities /t REG_DWORD /d 00000024 /f

    Where 0000 I am currently using a number up to 0020 as you can see here:

    upload_2023-5-9_23-42-3.png

    My goal is to have the PnPCapabilities value added in those places and set to a DWORD value of 24.

    This sets the network adapter in device manager under power management to uncheck "allow the computer to turn off this device to save power":

    upload_2023-5-9_23-45-45.png


    My problem is that I currently have the same REG ADD command duplicated 20 times with numbers from 0000 to 0020 so that some setting hits the right key value in the registry and correctly sets this value for the network card I'm running on.

    Also, the problem is that it has to work for my current Intel(R) Ethernet Connection (2) I218-V NIC name but always for any system and PC/Laptop where I will apply my script with settings for this purpose.

    Why am I using 20 duplicate REG ADD values from 0000 to 0024? Because during the installation of drivers for the network card, virtualbox and others, the network card is not always fixed under a fixed number, e.g. 0000 and after each installation of the system and various drivers, and depending on whether it is a LAN or WLAN card and how many of them are system, this causes the PnPCapabilities value to be in a random location.

    How can I make PnPCapabilities be added and set to DWORD 24 only in those places where it is really needed, so that I don't need to add numbers in the range 0000 -> 0020 or more where they are not needed?

    As you can see in the screenshot, there are 24 items in the registry and I didn't think that setting the bar of duplicate values so high would turn out to be smaller than they really can exist in the registry.

    This shows that my way works poorly and is wrong.

    If someone suggest me a better working way, it will be a relief for me from the pain that is currently going on with the REG ADD duplication:

    upload_2023-5-10_0-6-34.png

    Perhaps something that will first check if the REG_SZ value named DriverDesc is in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318} in numbers from 0000 to 0050, e.g. to be sure.

    And then PnPCapabilities set to a DWORD value of 24 only in those places where REG_SZ named DriverDesc was found and detected ?
     

    Attached Files:

  2. Carlos Detweiller

    Carlos Detweiller Emperor of Ice-Cream
    Staff Member

    Dec 21, 2012
    6,911
    7,987
    210
    I guess someone better than me in batch (almost anyone actually) will explain it in detail, but, for starters, take a look at the FOR /L batch command.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. RemixPL1994

    RemixPL1994 MDL Junior Member

    Jul 16, 2017
    54
    6
    0
    I secretly believe he's saving me @abbodi1406 because very often I get help from him for me, but of course if someone else is able to help me, I will be grateful to everyone and respect will belong to him :)

    If I manage to find a way for the network card, I will also be able to apply it to the graphics card for the video super resolution parameter which is in the nvidia control panel and improves the quality of displayed materials on a computer with a compatible video player.

    This is specifically about HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0000\_User_Global_VAL_SuperResolution

    where 0000 is again the place of the device / driver order and I can't add it rigidly to 0000 because there can be two cards in latpop and it can already be, for example, in 0001.

    And in a desktop computer you can have, for example, Intel + two cards in SLI or something similar and then there will be 3 numbers, i.e. 0000 / 0001 / 0002 and then again I would have to duplicate REG ADD to always hit the right hardware and driver order in the system.

    upload_2023-5-10_0-32-41.png
     
  4. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    17,370
    91,852
    340
    Code:
    for /L %%i in (0,1,9) do REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\000%%i" /v PnPCapabilities /t REG_DWORD /d 24 /f
    for /L %%i in (10,1,20) do REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\00%%i" /v PnPCapabilities /t REG_DWORD /d 24 /f
     
  5. RemixPL1994

    RemixPL1994 MDL Junior Member

    Jul 16, 2017
    54
    6
    0
    #5 RemixPL1994, May 9, 2023
    Last edited: May 9, 2023
    (OP)
    Thanks! I'm not sure.

    I need to use two lines for /L %%i in (0,1,9 and for /L %%i in (10,1,20) one after the other?

    What does it look like exactly?

    This will search from 0 to 20?

    If it is 24 will it also find that value and add it or will the limit be 20?

    Thanks in advance for any additional explanations.
     

    Attached Files:

  6. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    17,370
    91,852
    340
    It just loop "count" from 0-9 then 10-20 and add the registry

    the value is always 24
     
  7. RemixPL1994

    RemixPL1994 MDL Junior Member

    Jul 16, 2017
    54
    6
    0
    #7 RemixPL1994, May 9, 2023
    Last edited: May 10, 2023
    (OP)
    So the value should always be 24. I misinterpreted it. It was not about the value of 24, but about the number of positions, i.e. 0000 up to 0024, etc

    As you can see, it inserted 21 items:

    upload_2023-5-10_0-58-7.png

    I have 12 items of network adapters from device manager:

    upload_2023-5-10_0-58-32.png

    Is there a chance to improve REG ADD Z FOR so that it adds only as many items as it detects in the device manager physically and not one more?

    So for me it should only search and add REG ADD 12 times.

    Btw. And can we make FOR ignore everything that has "WAN Miniport" and "VirtualBox" in the name This would only focus on the real used LAN/WLAN cards in the system instead of the virtual ones, etc.

    This would add an entry once or twice when there are wlan and lan cards together except for the exceptions when someone has multiple lan/wlan but then it would add just a bit more.

    But not as many as 21 items.
     

    Attached Files:

  8. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    17,370
    91,852
    340
    Code:
    @echo off
    set "key=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}"
    for /L %%i in (0,1,9) do call :Add 0%%i
    for /L %%i in (10,1,20) do call :Add %%i
    pause
    exit /b
    
    :Add
    REG QUERY "%key%\00%1" 2>nul && REG ADD "%key%\00%1" /v PnPCapabilities /t REG_DWORD /d 24 /f
    exit /b
    
     
  9. RemixPL1994

    RemixPL1994 MDL Junior Member

    Jul 16, 2017
    54
    6
    0
    I checked this just for a test and unfortunately it still adds a minimum of 20 items.

    I entered 15 to see the changes and as you can see 15 also appeared for item 0020 where there really is no network card and its settings:

    upload_2023-5-10_1-38-8.png

    upload_2023-5-10_1-37-27.png

    Is there anything else I can do to make it only consider current network cards?

    @Dark Dinosaur

    Can you suggest something? hmm
     
  10. Carlos Detweiller

    Carlos Detweiller Emperor of Ice-Cream
    Staff Member

    Dec 21, 2012
    6,911
    7,987
    210
    The two lines are necessary because of handling of leading zeros. 0 to 9 needs 3 and 10 to 20 needs only two.

    100 to 999 would then need one and 1000 to 9999 none, but more than 20 is probably overkill.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. RemixPL1994

    RemixPL1994 MDL Junior Member

    Jul 16, 2017
    54
    6
    0
    Now I know why it's working badly for me now.

    My old script added 20 duplicate REG ADDs from 0000 to 0020

    That's why I have so many subkeys in the system, even empty ones

    the new commands even search for empty subkeys where a PnPCapabilities value is already present.

    Can we do something to make it ignore empty subkeys where there is no REG_SZ value named DriverDesc ?

    Or make it ignore names like "WAN Miniport" and "VirtualBox" to avoid adding values for PnPCapabilities there.
     
  12. ohoo i see.
    try pwsh tut to disable power management of Network interface cards.

    :D
     
  13. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    4,233
    6,084
    150
    #13 Dark Dinosaur, May 10, 2023
    Last edited: May 10, 2023
    we can use regex ... and add 0-unlimited ... regex match ... 0-X .. do ...
    i did use regex for stuff etc. its kinda fun.

    Code:
    Regex time .....
    
    echo $$I|findstr /r "^[0-9]$"
    echo $$I|findstr /r "^[1-9][0-9]$"
    same code with regex ... :D
    Code:
    @cls
    @echo off
    
    set "key=HKLM\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}"
    for /L %%i in (0,1,20) do (
      echo %%i|>nul findstr /r "^[0-9]$"      && (echo REG QUERY "%key%\000%%i") && REG ADD "%key%\000%%i" /v PnPCapabilities /t REG_DWORD /d 24 /f
      echo %%i|>nul findstr /r "^[1-9][0-9]$" && (echo REG QUERY "%key%\00%%i")  && REG ADD "%key%\00%%i"  /v PnPCapabilities /t REG_DWORD /d 24 /f
    )
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  14. RemixPL1994

    RemixPL1994 MDL Junior Member

    Jul 16, 2017
    54
    6
    0
    #14 RemixPL1994, May 10, 2023
    Last edited: May 11, 2023
    (OP)

    I tried this and it also creates new values in the registry that are empty and unnecessary:
    upload_2023-5-10_23-46-29.png

    So far my favorite way is this because it is the shortest, i.e. it has only 2 straight lines one under the other:

    for /L %%i in (0,1,9) do REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\000%%i" /v PnPCapabilities /t REG_DWORD /d 24 /f
    for /L %%i in (10,1,20) do REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\00%%i" /v PnPCapabilities /t REG_DWORD /d 24 /f

    But it also creates unnecessary new empty registry entries where it is not needed.

    Can we modify these two lines for \L from REG ADD so that it also searches first if there is a DriverDesc and based on this, if it does not exist, it will not add any empty entry of type 0016 as in the screenshot above where I showed it.

    And if DriverDesc exists it will check its name and if it is Intel or Realtek or something similar it will add PnPCapabilities with value 24 to the registry. But if it doesn't find DriverDesc then PnPCapabilities won't show up as an empty registry entry in 0016 etc where no network card is physically assigned.

    Also, could it ignore names like "WAN Miniport" and "VirtualBox" and "Hyper-V" in DriverDesc so that if it finds them it will also ignore that and not add PnPCapabilities?

    Step-by-step description of how the script works:

    1. It looks in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318} what numbers ( 0000, 0001 etc ) are currently in it.

    2. Searches for "DriverDesc" in all found numbers ( 0000, 0001 etc )
    a total of 21 pieces, i.e. from 0000 and 0001 to 0020:

    a) didn't find "DriverDesc" = does nothing / doesn't add PnPCapabilities

    upload_2023-5-11_0-13-49.png


    b) found "DriverDesc" = goes to point 3. below:

    3. Checks what is the name in "DriverDesc" and:

    a) if name in "DriverDesc" does not contain words/names etc "WAN Miniport" and "VirtualBox" and "Hyper-V" = add PnPCapabilities with value 24

    upload_2023-5-11_0-12-4.png


    b) if the name in "DriverDesc" contains anything from the words / names "WAN Miniport" and "VirtualBox" and "Hyper-V" = does nothing / does not add PnPCapabilities

    upload_2023-5-11_0-11-34.png


    Can someone make something like this and show me what it would look like? That would be the best solution.
     
  15. RemixPL1994

    RemixPL1994 MDL Junior Member

    Jul 16, 2017
    54
    6
    0
    My previous post is long so I decided to make a new one but I am asking the moderators to merge my two posts if they feel it necessary. I tried to do something with CHAT GPT and here are the results:

    Code No. 1 - Adds PnPCapabilities to the registry correctly and does it only where there are current key values such as 0001 / 0015, so it does not add new empty values unnecessarily, e.g. 0016 which in my registry does not have any network card and device assigned:

    setlocal enabledelayedexpansion

    set start=0
    set end=20

    for /L %%i in (%start%,1,%end%) do (
    set key=000%%i
    set key=!key:~-4!

    reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\!key!" /v DriverDesc 2>nul | find "DriverDesc" >nul
    if not errorlevel 1 (
    reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\!key!" /v PnPCapabilities /t REG_DWORD /d 24 /f
    )
    )


    Code No. 2 - I added an argument here using CHAT GPT so that if it finds in DriverDesc names like "WAN Miniport" /c:"VirtualBox" /c:"Hyper-V" /c:"Microsoft Kernel" then it will not add PnPCapabilities to the registry, but if are different then it will add PnPCapabilities correctly:

    setlocal enabledelayedexpansion

    set start=0
    set end=20

    for /L %%i in (%start%,1,%end%) do (
    set key=000%%i
    set key=!key:~-4!

    reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\!key!" /v DriverDesc 2>nul | findstr /i /c:"WAN Miniport" /c:"VirtualBox" /c:"Hyper-V" /c:"Microsoft Kernel" >nul && (
    echo DriverDesc zawiera niedozwoloną nazwę w kluczu !key!
    ) || (
    reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\!key!" /v DriverDesc 2>nul | findstr /i /c:"WAN Miniport" /c:"VirtualBox" /c:"Hyper-V" /c:"Microsoft Kernel" >nul || (
    reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\!key!" /v PnPCapabilities /t REG_DWORD /d 24 /f
    echo Dodano wartość do klucza !key!
    )
    )
    )


    And everything works fine, but code 1 doesn't create new unnecessary numbers like 0016 in the registry if it doesn't already exist for me.

    And code no. 2 makes that after each use I get empty additional digits in which there is only PnPCapabilities, even though there is nothing inside and it did not exist before and there is no DriverDesc.

    upload_2023-5-11_11-6-29.png

    Can anyone of you help me edit this to fix it, i.e. code no.2 so that it does not create new empty entries if they did not exist in the registry before?

    If any of you know a solution, thank you in advance for your help.

    @abbodi1406 @Dark Dinosaur @Carlos Detweiller
     
  16. already told use pwsh tut which will ensure if key name exist then only will write its value.
     
  17. RemixPL1994

    RemixPL1994 MDL Junior Member

    Jul 16, 2017
    54
    6
    0
    #17 RemixPL1994, May 11, 2023
    Last edited: May 11, 2023
    (OP)
    upload_2023-5-11_11-13-21.png

    @(\_/)^(\_/) Can you point me to exactly what the code should look like when updated after using pwsh tut??

    Unfortunately, I don't know how to do it myself.
     
  18. @Dark Dinosaur knows better then me about pwsh coding.
     
  19. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    4,233
    6,084
    150
    I learn from you ......
    highly skilled user.
    big respect :worthy:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  20. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    4,233
    6,084
    150
    anyway ... almost anything you can do with CMD
    you can do this also with PWSH ... so REG query ... FIND ... FINDSTR ...
    I'm sure you will find a way to search for what you look for
    and it is much faster ...... believe me . !!!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...