问题
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?
回答1:
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
回答2:
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.
回答3:
It is possible to print *.doc and *.xls using libreoffice, like this:
"C:\Program Files\LibreOffice\program\soffice.exe" -p "YourFilePAth"
来源:https://stackoverflow.com/questions/32595421/is-it-possible-to-send-a-file-to-a-printer-with-a-batch-file