IF to set default printer via batch

Asked

Viewed 345 times

0

We are redesigning the machine park here of the company and the new machines came with Windows 10, unlike the Windows 7 that we used before. The first problem we had was with the way Windows 10 manages printing, through its managed and not through the standard printer. I was able to disable this through a batch file that starts every user who logs into the machine by changing the corresponding record.

reg add "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v LegacyDefaultPrinterMode /t "REG_DWORD" /d "1" /f

The problem now is that the Print Manager is disabled, but Windows puts any of the printers as default. I then continued the batch to, in case you have a printer connected via USB, that printer is used as default

wmic printer where portname="USB001" call setdefaultprinter

However, some computers use remote printers, mapped as local ports, through the computer name " Samsungm’s computer name" (Standard name for sharing printers). I then tried to modify the file so that it would select the default printer itself. If there is a printer connected via USB, use it, otherwise use the printer that has as port "%Samsungm", but my code is not working...

set var=wmic printer get portname
%var% > portas.txt
if /I "%samsungm" == "portas.txt" equal (
wmic printer where "PortName like '%samsungm'" call setdefaultprinter
) else (
wmic printer where portname="USB001" call setdefaultprinter
)

The individual commands to set the printer as default work, so I believe it’s something in my If that’s wrong.

  • I think that part of "Equal" in the if shouldn’t be there either...

1 answer

0

You cannot compare piece of text to an "IF" clause in the DOS, so it will never work.

What you want to do is check if a particular text pattern exists in the output of a command. In DOS, the most appropriate command for this task is FINDSTR.

wmic printer get portname | findstr /eric:"samsungm *">NUL
if "%errorlevel%" == "0" (
    wmic printer where "PortName like '%samsungm'" call setdefaultprinter
) else (
    wmic printer where portname="USB001" call setdefaultprinter
)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.