Pregunta

I am trying to write a batch script that will loop through the following registry entry and update the different sub keys:

"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\"

For example:

reg add HKLM\System\CurrentControlSet\Control\Print\<Some Printer>\PrinterDriverData /t REG_DWORD /v SSNPNotifyEventSetting /d 0

I need to run that update for each printer installed and I think a FOR statement should be able to. I am just no good with FOR statements which is why I need help. This is what I have researched/tried so far:

set var1="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\"

FOR /F "PrinterDriverData" %%F IN ('REG QUERY %var1%') DO REG ADD %var1%\%%F\PrinterDriver\Data /t REG_DWORD /v SSNPNotifyEventSetting /d 0

I know this is probably way off, but its as far as I could figure out on my own. Any help is appreciated!

¿Fue útil?

Solución

Here is the solution I came up with. Not sure it is the best way or simplest, but it works so far. Feel free to critique or improve please.

set key="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers"

reg query %key% > temp.txt

FOR /F "delims=" %%a IN ('findstr /B "HKEY_LOCAL_MACHINE" temp.txt') DO (
    if /I NOT "%%a" == %key% (
        reg add "%%a\PrinterDriverData" /f /t REG_DWORD /v SSNPNotifyEventSetting /d 0 
    )
)

First, I query the registry and save all the needed reg keys into a text file. Then, inside the FOR loop, I search the file for the different registry keys and add the new sub key if not in he root key. I've added this to the tool I'm building to fix print servers and it has been great so far.

Otros consejos

Please refer to following batch file which can list the printers from the registry key, then you can pass the printer name to your script to do more stuff.

list-printers.bat

@echo off
for /f "tokens=1-8 delims=\" %%a in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers"') do if "%%g" NEQ "" @echo %%g
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top