Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to check if printer exists locally, and if so, deletes it

I'm writing a windows batch file and need to check whether the print exists on the local computer, and if so, deletes the mapped printer from the computer. Here is the code that I'm using to delete the printer.

RUNDLL32 printui.dll,PrintUIEntry /n \\server\printerName /dn

This works fine, but now I need a conditional statement before this so I check if that printer exists first. Then run that line. I'm not sure how to write this.

like image 440
dremme Avatar asked Oct 09 '22 04:10

dremme


2 Answers

You could try something like this, just replace the string to find with the printer you want to find.

For /F "Tokens=4 delims=\" %%I In ('reg query HKCU\Printers\Connections ^|find /I "560C"') Do If "%%I"==",,ServerName,HP DeskJet 560C" goto :REMOVE
goto :SKIP
:REMOVE
RUNDLL32 printui.dll,PrintUIEntry /n \\server\printerName /dn
:SKIP

Or just run the command and if it doesn't exist it will error, if it does it will work?

Hope this helps!

like image 124
Bali C Avatar answered Oct 13 '22 10:10

Bali C


You can just skip the check and run your deletion command anyway. To suppress the error popup just add the /q option as specified in the rundll32 printui.dll documentation. this yields:

RUNDLL32 printui.dll,PrintUIEntry /n \\server\printerName /dn /q
like image 31
Felix Avatar answered Oct 13 '22 10:10

Felix