I'm trying retrieve the path of a variable registry key location using "S/N:" and use it in a REG ADD routine.
The issue is that it is returning the same result 3 times over for each key with "S/N:" and this does not change any of the target data values.
c:\>SET TARGETPATH=VID_05E0&PID_0820\S/N:7014279E32044D4495B3B779E663FCC2_Rev:NBCACAAS1
c:\>SET TARGETPATH=VID_05E0&PID_0820\S/N:7014279E32044D4495B3B779E663FCC2_Rev:NBCACAAS1
c:\>SET TARGETPATH=VID_05E0&PID_0820\S/N:7014279E32044D4495B3B779E663FCC2_Rev:NBCACAAS1
c:\>SET TARGETPATH=VID_05E0&PID_082B\S/N:3D3591F0AC598F44820320E4984DB67A_Rev:NBCHMAAJ1
c:\>SET TARGETPATH=VID_05E0&PID_082B\S/N:3D3591F0AC598F44820320E4984DB67A_Rev:NBCHMAAJ1
c:\>SET TARGETPATH=VID_05E0&PID_082B\S/N:3D3591F0AC598F44820320E4984DB67A_Rev:NBCHMAAJ1
c:\>SET TARGETPATH=VID_05E0&PID_1200\S/N:7014279E32044D4495B3B779E663FCC2_Rev:NBCACAAS3
c:\>SET TARGETPATH=VID_05E0&PID_1200\S/N:7014279E32044D4495B3B779E663FCC2_Rev:NBCACAAS3
c:\>SET TARGETPATH=VID_05E0&PID_1200\S/N:7014279E32044D4495B3B779E663FCC2_Rev:NBCACAAS3
What I am trying to achieve is looping through the Registry location HKLM\SYSTEM\CurrentControlSet\Enum\USB, get the variable location VID_xxxxxx\S/N:xxxxx and set REG_DWORD values under the key "Device Parameters".
Here is my test script which runs without error but makes no changes:
@ECHO OFF
FOR /f "tokens=6,7 delims=\" %%i in ('REG QUERY "HKLM\SYSTEM\CurrentControlSet\Enum\USB" /s ^|find /I "S/N:"') DO (
SET TARGETPATH=%%i\%%j
REG ADD "HKLM\SYSTEM\CurrentControlSet\Enum\USB\%TARGETPATH%\Device Parameters" /v "AllowIdleIrpInD3" /t REG_DWORD /d "0" /f
REG ADD "HKLM\SYSTEM\CurrentControlSet\Enum\USB\%TARGETPATH%\Device Parameters" /v "DeviceSelectiveSuspended" /t REG_DWORD /d "0" /f
REG ADD "HKLM\SYSTEM\CurrentControlSet\Enum\USB\%TARGETPATH%\Device Parameters" /v "EnhancedPowerManagementEnabled" /t REG_DWORD /d "0" /f
REG ADD "HKLM\SYSTEM\CurrentControlSet\Enum\USB\%TARGETPATH%\Device Parameters" /v "EnumerationRetryCount" /t REG_DWORD /d "0" /f
REG ADD "HKLM\SYSTEM\CurrentControlSet\Enum\USB\%TARGETPATH%\Device Parameters" /v "LegacyTouchScaling" /t REG_DWORD /d "0" /f
)
PAUSE
If anyone can point out where I am failing I would really appreciate it.
Reg Query For Loop and Reg Add failure
Moderator: DosItHelp
Re: Reg Query For Loop and Reg Add failure
Next time, please put your output and code between code tags (use the </> button )
That out of the way:
Try your code with
echo REG ADD .. instead of REG ADD ... to see what's going on.
Token 6 and 7 are the same for those.
You need setlocal enabledelayedexpansion for that to work (see SET /? and SETLOCAL /? for details)
This might work (can't test it):
And if it doesn't: change @echo off to @echo on to see in detail what is going on.
If output looks OK, remove the echo in front of REG ADD.
That out of the way:
Try your code with
echo REG ADD .. instead of REG ADD ... to see what's going on.
That will be caused by the /S in your reg query command. That will report HKLM\..\USB\VID...\S/N, but also HKLM\..\USB\VID...\S/N\something\deeper\".
Token 6 and 7 are the same for those.
Code: Select all
FOR /f "tokens=6,7 delims=\" %%i in ('REG QUERY "HKLM\SYSTEM\CurrentControlSet\Enum\USB" /s ^|find /I "S/N:"') DO (
SET TARGETPATH=%%i\%%j
This might work (can't test it):
And if it doesn't: change @echo off to @echo on to see in detail what is going on.
If output looks OK, remove the echo in front of REG ADD.
Code: Select all
@echo off
setlocal
FOR /f "usebackq delims=" %%a in (`REG.exe QUERY "HKLM\SYSTEM\CurrentControlSet\Enum\USB" `) do (
FOR /f "usebackq delims=" %%b in (`REG.exe QUERY "%%a" ^| findstr.exe /i /c:"S/N:"`) DO (
echo ===[ %%b ]==
echo REG.exe ADD "%%b\Device Parameters" /v "AllowIdleIrpInD3" /t REG_DWORD /d "0" /f
echo REG.exe ADD "%%b\Device Parameters" /v "DeviceSelectiveSuspended" /t REG_DWORD /d "0" /f
echo REG.exe ADD "%%b\Device Parameters" /v "EnhancedPowerManagementEnabled" /t REG_DWORD /d "0" /f
echo REG.exe ADD "%%b\Device Parameters" /v "EnumerationRetryCount" /t REG_DWORD /d "0" /f
echo REG.exe ADD "%%b\Device Parameters" /v "LegacyTouchScaling" /t REG_DWORD /d "0" /f
)
)
Re: Reg Query For Loop and Reg Add failure
Thank you!
Well explained. I also found out from your example that there is a difference when using ` vs. ', this I was never aware of, I have been using (and getting away with!) ' for years.
I am reading up on Set local and delayed expansion so I can get an understanding.
Much appreciated!
R
Well explained. I also found out from your example that there is a difference when using ` vs. ', this I was never aware of, I have been using (and getting away with!) ' for years.
I am reading up on Set local and delayed expansion so I can get an understanding.
Much appreciated!
R