Skip to main content

Batch Script Printing

Batch Script Printing

Bash script allows to control printing with the help of the net print command.

The syntax of this command is given below:

Sytax
print [/d:print_device] [[drive:][path]filename[...]]

where:

  • print_device specifies a print device

For example, the following command will print the example.txt file to the parallel port lpt1.

print c:\example.txt /c /d:lpt1

Command Line Printer Control

The Windows command-line tool can be used to manage most of the Windows configuration. For this purpose, the PRINTUI.DLL and RUNDLL32.EXE commands are used.

The syntax is given below:

Sytax
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry [option] [@commandfile]

where the options available are the following:

  • /dl used to delete the local printer
  • /dn used to delete network printer connection.
  • /dd used to delete the printer driver.
  • /e shows printing preferences.
  • f[file] either inf file or output file.
  • /F[file] location of an INF file that the INF file specified with /f may depend on.
  • /ia installs printer driver using inf file.
  • /id installs printer driver using add printer driver wizard.
  • /if installs printer using inf file.
  • /ii installs printer using add printer wizard with an inf file.
  • /il installs printer using add printer wizard.
  • /in adds network printer connection.
  • /ip installs printer using network printer installation wizard.
  • /k prints test page to specified printer, cannot be combined with command when installing a printer.
  • /l[path] printer driver source path.
  • /m[model] printer driver model name.
  • /n[name] printer name.
  • /o display printer queue view.
  • /p displays printer properties.
  • /Ss stores printer settings into a file.
  • /Sr restores printer settings from a file.
  • /y sets printer as the default.
  • /Xg gets printer settings.
  • /Xs sets printer settings.

Testing if a Printer Exists

It may happen that you are connected to a network printer instead of a local printer. In such cases, it is always useful to check for a printer before printing.

note

The existence of a printer can be evaluated with the help of the RUNDLL32.EXE PRINTUI.DLL.

In the following example, we will first set the printer name and set a file name which will hold the settings of the printer. Then RUNDLL32.EXE PRINTUI.DLL commands will be used to check if the printer actually exists by sending the configuration settings of the file to the file TR-Printers.txt

SET PrinterName = Test Printer
SET file=%TEMP%\TR-Printers.txt
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /Xg /n "%PrinterName%" /f "%file%" /q

IF EXIST "%file%" (
ECHO %PrinterName% printer exists
) ELSE (
ECHO %PrinterName% printer does NOT exists
)