Reg Query For Loop and Reg Add failure

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rickdanna
Posts: 2
Joined: 02 May 2020 07:12

Reg Query For Loop and Reg Add failure

#1 Post by rickdanna » 02 May 2020 08:04

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.

Eureka!
Posts: 102
Joined: 25 Jul 2019 18:25

Re: Reg Query For Loop and Reg Add failure

#2 Post by Eureka! » 02 May 2020 16:53

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.

rickdanna wrote:
02 May 2020 08:04
The issue is that it is returning the same result 3 times over for each key with "S/N:
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
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.

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
	)
)


rickdanna
Posts: 2
Joined: 02 May 2020 07:12

Re: Reg Query For Loop and Reg Add failure

#3 Post by rickdanna » 02 May 2020 20:28

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

Post Reply