4

How can I use a batch file to send a text file or a .doc or similar, to a printer plugged into the computer through a USB port?

Paul
  • 2,620
  • 2
  • 17
  • 27
  • `c:\> print /d:name_of_printer file_to_print` – Marc B Sep 15 '15 at 20:57
  • what google does not answer with "print from batch file" ? just type that and you'll have many solutions available – Frederic Henri Sep 15 '15 at 20:58
  • @MarcB, the printer device to redirect is called `prn`, isn't it? – aschipfl Sep 15 '15 at 21:58
  • @aschipfl: pretty much. but prn is simply the system default printer, and if you've got multiple printers, you may want to target one specifically, which is why there's `print`. – Marc B Sep 16 '15 at 14:32
  • @MarcB, I misinterpreted your comment (maybe it was too late... ;-)), I read the `>` as redirection operator rather than part of the command prompt... – aschipfl Sep 16 '15 at 14:49

4 Answers4

4

Also to get the printer name:

wmic printer get name /value | findstr Name

It will list all printers like:

Name=PDF
Name=Microsoft XPS Document Writer
Name=Fax

And if you know part of the name, you may include it in a variable dynamically with FOR.

@echo off

for /f "tokens=2 delims==" %%a in (
    'wmic printer get name /value ^| findstr PartOfThePrinterName'
) do (
    set "printer_name=%%a"
)

REM Also you can remove the FOR command if you want to set the variable as static.
REM ie. "set printer_name=MyPrinterName"

print filename.txt /D:"%printer_name%"

exit /b 0

note the double quotes and no white space after /D: to be sure it get the right printer.

Another method is to set the default printer and print the document through the notepad.

RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "%printer_name%"
start /min notepad /P filename.txt
Paul
  • 2,620
  • 2
  • 17
  • 27
1

You can use the PRINT command like below to print ASCII files. Use print /? in command prompt to know more about the command. Here, /D is the switch fr device name since by default it's LPT1.

PRINT filename.txt /D:<printer_name>

Also, see this Article for much more information on printing PDF's etc.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • I tried it but i get the console displaying the text "C:\Python27> print message.txt /D: Canon Inkjet MP760 Series" infinite times. am I missing something? – Nicholas Anderson Sep 15 '15 at 21:35
  • @NicholasAnderson the printer name should be inside double quotes – Paul Sep 15 '15 at 22:47
  • @Paul I have the printer name in double quotes but it is still doing the same thing. `print message.txt /D: "Canon Inkjet MP760 Series"` [what it looks like](https://www.dropbox.com/s/z7rxmk7esgrodbq/Capture3.PNG?dl=0) – Nicholas Anderson Sep 15 '15 at 22:59
  • Don't put space, do this like: `echo Test the print command>_printing.txt &print _printing.txt /D:"Canon Inkjet MP760 Series"` – Paul Sep 15 '15 at 23:13
  • 1
    @Paul I get the error: "Unable to initialize device Canon Inkjet MP760 Series" Could the printer have a problem? – Nicholas Anderson Sep 15 '15 at 23:33
  • @NicholasAnderson I get this issue too but I don't know how to resolve it. You should ask in http://superuser.com/ – Paul Sep 15 '15 at 23:46
1

Here is a native way, with anything required, but only ASCII, and has to open a window:

notepad /P file.txt
sld
  • 93
  • 10
0

It is possible to print *.doc and *.xls using libreoffice, like this:

"C:\Program Files\LibreOffice\program\soffice.exe" -p "YourFilePAth"
sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40