0

I am looking for a batch-function to check if a printer does already exist on a windows xp system.

I made up the following script:

set PRINTEREXISTS=0
For /F "Tokens=8 delims=\" %%I IN ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers" ^|find /I"r016-printer01"') Do (
    echo %%I
    set PRINTEREXISTS=1
)
if "%PRINTEREXISTS%"=="0" (
    echo Printer doesn't exist
) else (
    echo Printer exists
)

However, this sometimes outputs the printer name and then Printer doesn't exist even though it shouldn't because of PRINTEREXISTS=1... (NOT an issue of the reg query but with the batch-script somehow)

Any ideas?

Zulakis
  • 4,153
  • 14
  • 48
  • 76

1 Answers1

2

A batch file? Why not a vbscript at least. Something like:

strPrinterName ="r016-printer01"

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer where name = '" & strPrinterName & "'")

If colPrinters.Count <> 0 Then

       wscript.echo "Printer exists" 

else

       wscript.echo "Printer doesn't exist"

End If
TheCleaner
  • 32,627
  • 26
  • 132
  • 191