Print file from command line windows

Overview

The PRINT command in Windows CMD is used to print a text file to a printer connected to your computer. Essentially, it sends the text content of specified files directly to the default printer. This command is most effectively used in simple scripting tasks where automated printing of documents is needed, like batch printing text files in a business environment.

Syntax

The basic syntax for the PRINT command is:

PRINT [/D:device] <filename> [<filename2> ...]
  • /D:device : Specifies the print device. If not provided, the command prints to the default printer.
  • <filename> : The name of the file you want to print. Multiple filenames can be specified to print several files at once.

Options/Flags

  • /D:device: This flag is used to define a specific printer device. The device name should be the name that is recognized by the system, for example, /D:LPT1 or /D:\\server\printername. If omitted, the PRINT command will send the document to the default system printer.

Examples

  1. Basic Printing
    Print a single file to the default printer:

    PRINT example.txt
    
  2. Printing Multiple Files
    Print multiple text files simultaneously:

    PRINT doc1.txt doc2.txt doc3.txt
    
  3. Specifying a Printer
    Print a file to a printer connected on LPT1:

    PRINT /D:LPT1 invoice.txt
    

Common Issues

  • Printer Not Recognized: This issue can occur if the specified device is not correctly named or not configured properly. Ensure that the printer device name matches exactly with what is configured in Windows.
  • File Not Found: The PRINT command will fail if it cannot locate the file specified. Ensure that the file paths are correct and accessible to the user running the command.

Integration

The PRINT command can be combined with other commands for more complex tasks. For example, to print all .txt files in a directory:

FOR %G IN (*.txt) DO PRINT %G

This script uses the FOR command to iterate through all .txt files in the current directory and prints each using the PRINT command.

  • COPY: Can be used to send files to a printer device by copying a file directly to a printer’s device interface.
  • TYPE: This command can display the contents of a text file, which can be piped into PRINT for printing.

For more details on using the PRINT command, consult the official Microsoft documentation.

By understanding and utilizing these components, users can effectively manage printing tasks directly from the command line interface in Windows.

The `print` command in CMD is used to print a text file to a specified printer or to the default printer, allowing users to quickly output documents directly from the command line.

print myfile.txt

Understanding the CMD Print Command

What is the CMD Print Command?

The cmd print command is a command-line utility that allows users to send files directly to a printer from the Windows Command Prompt (CMD). This command is particularly useful in scenarios where you need to automate printing tasks or when working on servers without a graphical interface. The print command simplifies the printing process by enabling users to specify a file and the desired printer through simple commands.

Key Terminology

To fully grasp the functionality of the cmd print command, it’s essential to understand some key terminology:

  • CMD: Short for Command Prompt, a command-line interface in Windows that allows users to interact with the operating system through various commands.

  • Print Command: This specific command facilitates printing files from the CMD, giving you control over what gets printed and how.

  • Printer Device: Refers to the physical printer connected to your system or network that processes the print job.

Mastering The Cmd Run Command: A Quick Guide

Mastering The Cmd Run Command: A Quick Guide

How to Use the CMD Print Command

Syntax of the Print Command

The syntax for the cmd print command is quite straightforward. It allows you to specify the device and filename you wish to print. The basic format is:

print [/D:device] [filename]

Here, the `/D:device` option is optional, while the filename is mandatory.

Parameters and Options

`/D:device`

The `/D:device` parameter allows you to specify which printer you want to use, especially important if you have multiple printers installed. To find your printer’s name, you can go to the «Devices and Printers» section in the Control Panel.

`filename`

This parameter allows you to designate which file you want to print. The cmd print command supports various file types, but it’s predominantly used for plain text files (.txt).

Example Usage of the Print Command

Simple Print Command

One of the simplest applications of the cmd print command is printing a text file. For instance:

print C:\Users\YourUser\Documents\example.txt

In this example, CMD sends the «example.txt» file located in the Documents folder to the default printer without additional specifications.

Specifying a Printer

If you need to specify which printer to use, for instance, if «PrinterName» is your printer of choice, the command would look like this:

print /D:\\PrinterName C:\Users\YourUser\Documents\example.txt

This command ensures that your document is printed using the designated printer rather than the default one. To find your printer name, look in the Devices and Printers section, where it’s listed under the installed printers.

Mastering The Cmd List Command: Quick Tips For Success

Mastering The Cmd List Command: Quick Tips For Success

Advanced Usage of the Print Command

Printing from Network Printers

The cmd print command can also be used to print to network printers, allowing for flexibility in your printing options. When printing to a network printer, the syntax remains similar:

print /D:\\NetworkPrinterName\PrinterQueue C:\Users\YourUser\Documents\example.txt

In this scenario, replace `NetworkPrinterName` with the name of your network printer and `PrinterQueue` with the name of the printer’s queue. To locate network printer names, check with your network administrator or find them in the Devices and Printers section.

Error Handling

Common Errors

While using the cmd print command, you may encounter several common issues:

  • File Not Found: This error occurs if the specified file path is incorrect or if the file does not exist.

  • Access Denied: This message may indicate that you do not have the necessary permissions to access the file or printer.

Troubleshooting Steps

If you experience issues, consider the following troubleshooting steps:

  • Double-check the file path and ensure the file exists.
  • Verify printer connectivity (for both local and network printers).
  • Ensure that you have the correct permissions to access the file and send print commands.

Mastering Cmd DNS Commands: A Quick Guide

Mastering Cmd DNS Commands: A Quick Guide

Practical Applications

Use Cases for the Print Command

The cmd print command proves invaluable in various scenarios:

  • Office Environments: Automating printing tasks can save time during peak operational hours.

  • Education: Instructors may prefer printing handouts or assignments directly via CMD, streamlining the process.

  • Development: Developers can incorporate print commands in scripts to produce reports directly from logs or processed outputs.

Mastering Cmd Boot Commands for Quick System Control

Mastering Cmd Boot Commands for Quick System Control

Alternatives to the CMD Print Command

Other Command Line Interactions

While the cmd print command is useful, there are other command-line alternatives, such as PowerShell commands. PowerShell provides more extensive functionalities for printing, including access to printer-related cmdlets that allow for more complex interactions.

GUI Alternatives

Although CMD provides a powerful way to print files, using graphical user interfaces (GUIs) is also a common practice. Tools such as Microsoft Word or Adobe Acrobat allow users to print files more visually. However, using CMD can streamline your workflow when dealing with multiple print tasks in batch formats.

Mastering The Cmd IP Command: Quick And Simple Guide

Mastering The Cmd IP Command: Quick And Simple Guide

Conclusion

In summary, the cmd print command is a powerful tool for managing print jobs directly from the Command Prompt. By mastering its syntax, parameters, and troubleshooting methods, you can enhance your productivity, particularly in environments requiring frequent and automated printing tasks. Practice using this command, and you’ll find it increasingly useful for your printing needs.

Cmd Best Commands: Your Quick Guide to Mastery

Cmd Best Commands: Your Quick Guide to Mastery

Frequently Asked Questions (FAQs)

  • Can I print non-text files using the cmd print command?

    • The cmd print command primarily supports plain text (.txt) files. For other file types, you may need to use a different method or application.
  • What if my network printer isn’t recognized by CMD?

    • Ensure that you are connected to the same network as the printer and that its name is spelled correctly in the command.

Mastering The Cmd Scan Command Quickly and Easily

Mastering The Cmd Scan Command Quickly and Easily

Additional Resources

For those looking to deepen their understanding of the cmd print command and other CMD commands, consider exploring official Microsoft documentation, online courses, or community forums dedicated to CMD expertise.

Although there are many ways of saving documents electronically, most office workers still need to print many documents daily, such as Adobe Acrobat PDFs. Inconveniences arise here not just because of the waste of paper but also in the repetitive printing process itself.

Skip ahead to:

  • What’s wrong with printing PDFs manually
  • Command-line tool for PDF printing
  • How to print PDFs from the Command Line
  • 2Printer usage options

If you’re looking for a command line PDF printing solution, please watch our tutorial video. It describes how to print multiple PDFs from Command Prompt (cmd.exe) or any other command line interpreter:

Download 2Printer

What’s wrong with printing PDFs manually

Well, printing any document manually seems quite simple: 

  • Open a PDF document in Adobe Acrobat Reader (or any other PDF reader);
  • Click the «Print» icon, or use Ctrl + P combination;
  • Change print settings like color or double-sided mode and click «Print». 

Or, it goes down to right-clicking the file and selecting the «Print» function. It seems easy, but what if hundreds of documents need to be printed every day? What a huge waste of time!

There are not too many resources available that can simplify the printing process. Adobe Reader is quite convenient for opening PDF docs, but it won’t help if there are lots of them to be printed. Windows Explorer can give a helping hand in some situations: you can open the Windows Explorer, select PDF files, then right-click the selected area and click «print». The documents will be printed automatically, but there’s a limitation: no more than 15 files can be printed at once. It looks like Microsoft does not plan to introduce any innovations in this respect. That’s why it’s quite reasonable to seek additional third-party tools.

Command-line tool for PDF printing

As a solution, you can try a desktop app for batch printing, such as Print Conductor. However, if you are not afraid of the command line interface (CLI) and want to make printing in the office centralized and/or user-unattended, 2Printer is the right choice. This command line software has been developed specifically for making the batch printing of documents easy and convenient. 

Command line tool for PDF printing on Windows

How to print PDFs from the Command line step by step

How does 2Printer work? You’ll need a plain text command with an indication of source – where your PDFs are located. You can also specify a printer name (if you don’t, the printer set to default in Windows system will be used automatically). Here is an example showing how easy it is to print all the PDF files from a folder:

2Printer.exe -src «C:\PDFs\*.PDF» -prn «iR C3025»

2Printer command line syntax explained:

2Printer command line syntax explained

What types of PDF can be printed? 2Printer supports a large number of formats, including digitally created, image-only, searchable, password-protected PDF, as well as PDF/A, PDF/X, PDF Portfolio files, and PDFs with attachments.

Learn more about Adobe PDF format

Besides PDF documents, 2Printer command line tool can print Microsoft Office (Office 365) file formats such as DOCX, XLSX, PPSX, and many other files used in document workflow in practically every office!

2Printer usage options

Here’s how 2Printer can be used:

  • Manually. You can enter a 2Printer.exe command and run it in any command line interpreter, e.g., Command Prompt or Windows PowerShell;
  • Automatically, on a schedule via Windows Task Scheduler. Want to start a print job every Monday morning before reaching the office? It’s easy with 2Printer.
  • Via a script. Perform repetitive printing tasks by running a batch file (a special text file with .BAT file extension) that will start printing automatically! 
  • From another software. 2Printer can be integrated into existing code or another software as a fast and reliable printing component. That’s why 2Printer is especially popular among system administrators, power users, software developers, and IT specialists in general.

2Printer – How it works

2Printer can be easily integrated into any office environment thanks to its command line interface. It’s a command line tool that can automate the printing process, for example, through Visual Basic and Java scripts. This way, any number of files can be printed in one session without any limitations. 

2Printer command-line tool is free for non-commercial purposes and is compatible with any printer, be it private or corporate, and connected to the computer by any possible means.

Download 2Printer

Need support or advice from our developers? Please leave us a message, and we will get back to you shortly.

Get in touch with us

Rob van der Woude's Scripting Pages

  • Print Text Files
  • Print Registered File Types
    • PrintHTM.bat
    • PrintPDF.bat
      • Explanation of PrintPDF.bat:
    • PrintHTM.bat, Version 2.00
    • PrintAny.bat, Version 2.01
  • Parameters in File Associations
  • Related Stuff
  • Or were you looking for this…?

Print Text Files

In Windows, ASCII files can be printed using the «classic» PRINT command:

PRINT mytext.txt /D:LPT2

By default, PRINT will send its output to LPT1.
By using the /D switch, a different printer port can be used.

On most modern computers, PRINT will be useless, because it needs a printer port like LPT1 or COM1 etc., whereas most printers nowadays are connected either to USB ports or over a TCP/IP connection.

In those cases, a better alternative to the PRINT command is Notepad with its /P switch:

NOTEPAD /P whatever.reg

will print whatever.reg, even if .REG files aren’t associated with Notepad at all (by default they aren’t, and they shouldn’t be).

Notepad will send its output to Windows’ default printer.

Likewise, Wordpad can print plain text as well as rich text using its /P switch:

WORDPAD /P whatever.rtf

will open whatever.rtf in Wordpad and then open the «Print» dialog.

Unlike Notepad, Wordpad does have a /PT switch:

WORDPAD /PT whatever.rtf MyPrinter

will open whatever.rtf in Wordpad, print whatever.rtf on the printer named MyPrinter and then close Wordpad again.

Print Registered File Types

Files that have a file type association in Windows (95/98/NT/2000) can be printed using the right mouse button (assuming you didn’t swap mouse buttons).
However, most visitors to this site are probably more interested in using the command line instead of the GUI.
On this page I will explain how you can use the file association to print a file from the command line.
There is a limitation to this technique, however: for most file types, «Print» opens Windows’ print dialogue for the file, so the user still has to click the «Print» or «OK» button.
Usually, «PrintTo» commands are executed without user interaction, but they do require a printer name as their second command line argument.

PrintHTM.bat

When you start Windows’ Explorer and click the «View» menu entry, then «Options…», and choose the «File Types» tab, you will see a list of all file types with their associations.
If it was just the associations we were after, we could have used ASSOCIATE.EXE from the Resource Kit, but in this case we are more interested in the print commands.

Scroll through the list and select «HTML Document».
Depending on your Windows version, click the «Advanced» or «Edit» button.
In the «Actions» list you should now see at least these 3 options:

Edit
open
print

The option in bold face is the one started when you double-click a file of this type.

Select «print» and click the «Edit» button beside or below the «Actions» list.
This will open a dialogue titled «Edit action for type: HTML Document».
It is the value of the field «Application used to perform action» that we are after.
On most systems it will read something like this:

rundll32.exe C:\WINDOWS\System\mshtml.dll,PrintHTML "%1"

or:

rundll32.exe C:\WINNT\System32\mshtml.dll,PrintHTML "%1"

Since the directory for the DLL is where RUNDLL32.EXE will look by default, we can leave it out to make the command more generic:

RUNDLL32.EXE MSHTML.DLL,PrintHTML "%1"

This command can be used to print HTML files from a batch file.
The following example is NT only, but with a few modifications it can be used in Windows 95/98 as well.
Do not expect these modifications to be easy reading, though.

@ECHO OFF
SETLOCAL

:: Command line parsing
SET File2Print=%*
ECHO.%File2Print% | FIND "?" >NUL
IF NOT ERRORLEVEL 1 GOTO Syntax
:: Strip leading space in NT 4 only
VER | FIND "Windows NT" >NUL
IF NOT ERRORLEVEL 1 IF DEFINED File2Print SET File2Print=%File2Print:~1%
IF DEFINED File2Print SET File2Print=%File2Print:"=%
IF NOT DEFINED File2Print GOTO Syntax
IF NOT EXIST "%File2Print:"=%" GOTO Syntax

:: Actual print command
RUNDLL32.EXE MSHTML.DLL,PrintHTML "%File2Print%"

:: Done
GOTO End

:Syntax
ECHO.
ECHO PrintHTM.bat,  Version 1.11 for Windows NT
ECHO Prints a local HTML file from the command line
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO.
ECHO Usage:  %~n0  ^<html_file^>

:End
ENDLOCAL

PrintPDF.bat

Now that one was easy, using RUNDLL32 and an almost (!) native DLL.
How about PDF files, for example?
Can a batch file for printing PDF files be made generic enough to cope with different installations and versions of Adobe Acrobat Reader?

You may have guessed: I wouldn’t have asked if the answer had been NO.

Note: There is one restriction, though: this batch file will only work when PDF files’ default association is any Acrobat Reader version.
This means that if you have Acrobat Writer installed on your PC, this batch file will fail.
As of version 1.20 a message will be displayed to explain why it fails.
Thanks to Chuck Hicks for this addition.
Thanks to Michael Lintner for his modification to enable long file names.
Thanks to Fabio Quieti for adding the /T switch, which automatically closes the Adobe Reader window after printing the file (note: this /T switch has only been tested with Adobe Reader 7).

By using ASSOC, FTYPE and REGEDIT to read the PDF print command from the registry we can print PDF files from within batch files:

@ECHO OFF
:: Check Windows version, abort if not NT 4 or later
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
SETLOCAL ENABLEDELAYEDEXPANSION

:: Initialize variables
SET PrintCmd=
SET Temp=%Temp:"=%
SET NumFiles=0
SET MultiPrint=0
SET ListTool=

:: Check command line arguments
IF     "%~1"=="" GOTO Syntax
IF NOT "%~3"=="" GOTO Syntax
IF     "%~2"=="" (
	SET FileSpec=%~1
) ELSE (
	IF /I "%~1"=="/M" SET FileSpec=%~2
	IF /I "%~2"=="/M" SET FileSpec=%~1
)
ECHO.%* | FIND /I "/M" >NUL && SET MultiPrint=1
ECHO.%FileSpec% | FIND "/" >NUL && GOTO Syntax
IF NOT EXIST "%FileSpec%" GOTO Syntax

:: Count the number of files specified by filespec
FOR %%A IN (%FileSpec%) DO SET /A NumFiles = !NumFiles! + 1
IF %NumFiles% EQU 0 GOTO Syntax

:: Check if we need to have access to a list of processes
:: currently running, and if so, which one is available
IF %NumFiles%   GTR 1 SET  MultiPrint=1
IF %MultiPrint% EQU 0 CALL :GetListTool

:: Get the file association from the registry
FOR /F "tokens=1* delims==" %%A IN ('ASSOC .PDF') DO (
	FOR /F "tokens=1 delims==" %%C IN ('FTYPE ˆ| FIND /I "%%~B"') DO (
		CALL :GetPrintCommand %%C
	)
)

:: Check if a print command was found
IF NOT DEFINED PrintCmd (
	ECHO.
	ECHO No print command seems to be associated with .PDF files on this computer.
	GOTO Syntax
)

:: Print the file using the print command we just found
FOR %%A IN (%FileSpec%) DO CALL :ExecPrintCommand "%%~fA"

:: A final message
IF "%MultiPrint%"=="1" (
	ECHO.
	ECHO You will need to close the Acrobat Reader window manually after the printing
	ECHO is finished.
	IF "%NumFiles%"=="1" IF "%ListTool%"=="" (
		ECHO To close that window automatically next time you print a single PDF file,
		ECHO download and install PSLIST from the Microsoft website:
		ECHO https://technet.microsoft.com/en-us/sysinternals/pstools	)
)

:: Done
GOTO End


:ExecPrintCommand
CALL echo START /MIN "PrintPDF" %PrintCmd%
GOTO:EOF


:GetListTool
:: Now we need to find a tool to check for processes.
:: In XP and later this will be the native TASKLIST command,
:: in NT 4 and 2000 we'll need to find a non-native tool.
:: First we'll try TASKLIST ...
TASKLIST >NUL 2>&1
IF ERRORLEVEL 1 (
	REM ... if TASKLIST isn't available we'll try TLIST next ...
	TLIST >NUL 2>&1
	IF ERRORLEVEL 1 (
		REM ... and if that isn't available either we'll try PSLIST ...
		PSLIST >NUL 2>&1
		IF NOT ERRORLEVEL 1 SET ListTool=PSLIST
	) ELSE (
		SET ListTool=TLIST
	)
) ELSE (
	SET ListTool=TASKLIST
)
:: Don't worry if we didn't find ANY tool to list processes, in
:: that case we'll just assume multiple PDFs need to be printed
IF "%ListTool%"=="" SET MultiPrint=1
GOTO:EOF


:GetPrintCommand
:: Get the print command for this file type from the registry
START /WAIT REGEDIT.EXE /E "%Temp%.\pdf.dat" "HKEY_CLASSES_ROOT\%1\shell\print\command"
IF NOT EXIST "%Temp%.\pdf.dat" GOTO:EOF
FOR /F "tokens=1* delims==" %%D IN ('TYPE "%TEMP%.\pdf.dat" ˆ| FIND "@="') DO SET PrintCmd=%%E
DEL "%Temp%.\pdf.dat"
SET PrintCmd=%PrintCmd:\"="%
SET PrintCmd=%PrintCmd:""="%
SET PrintCmd=%PrintCmd:\\=\%
:: The /T switch terminates Acrobat Reader after printing.
:: Thanks to Fabio Quieti for sending me this tip.
:: However, as Michael Butler pointed out, it should not be
:: used when printing lots of files.
:: So I introduced a /M switch for this batch file, stating
:: that multiple files are to be printed.
:: Without specifying the /M switch, this will also be true
:: when wildcards are used in the filespec.
:: Finally, if another Adobe Reader process is running right
:: now, we won't be using the /T switch either.
IF %MultiPrint% EQU 0 CALL :CheckProcess %PrintCmd%
IF %MultiPrint% EQU 0 (
	SET PrintCmd=%PrintCmd:"%1"=/t "%%%~1"%
	rem SET PrintCmd=%PrintCmd% /t "%%~1"
) ELSE (
	SET PrintCmd=%PrintCmd:"%1"="%%%~1"%
	rem SET PrintCmd=%PrintCmd% "%%~1"
)
GOTO:EOF


:CheckProcess
IF "%ListTool%"=="" (
	SET MultiPrint=1
) ELSE (
	%ListTool% 2>&1 | FIND /I "%~n1" >NUL && SET MultiPrint=1
)
GOTO:EOF

:Syntax
ECHO.
ECHO PrintPDF.bat,  Version 3.10 for Windows NT 4 / 2000 / XP / Server 2003
ECHO Prints PDF files from the command line
ECHO.
ECHO Usage:  PRINTPDF  pdf_filespec  [ /M ]
ECHO.
ECHO Where:  "pdf_filespec"  is the file name or filespec of the PDF file(s)
ECHO                         to be printed; wildcards allowed (*); use double
ECHO                         quotes for long file names
ECHO.
ECHO Notes:  This batch file has been tested with Acrobat Reader versions 5-7 only.
ECHO         It requires Adobe/Acrobat Reader, and will NOT work if Acrobat "Writer"
ECHO         is installed.
ECHO         Thanks to Fabio Quieti, as of version 3.00, you no longer need to close
ECHO         the minimized Acrobat Reader window manually, after printing the file.
ECHO         Thanks to Michael Butler, printing lots of PDFs will no longer make the
ECHO         computer slow down to a crawl or even hang.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com


:End
IF "%OS%"=="Windows_NT" ENDLOCAL

A note by Michael Butler on printing lots of PDFs from the command line:

I think your PDF printing methods listed may not work well for a list of over 50 PDFs.
I had a script using

CALL START /MIN AcroRd32.exe /h /p [filename]

for each PDF in a list, but it stopped queuing them up at about the 35th-50th one (had a list of 90), depending on the computer.
I think it is related to how memory is allocated with CALL START.
After a lot of troubleshooting, I found that you can FIRST launch Adobe Reader using

CALL START /MIN AcroRd32.exe /h

and then later in the script just call (without using CALL START)

AcroRd32.exe /h /p [filename]

for each file name, and it will allow execution to return to the batch file because Adobe Reader is already open.
This worked well for printing/queuing up over 100 PDFs.

I tried your recent /T switch option for Reader 7 and it seemed to open a new instance of AcroRd32.exe for every PDF — not feasible when printing more than 10 PDFs.
The downside is that without /T, one instance of AcroRd32.exe will still be left open after the script ends, albeit minimized.

Well, I think I may have solved this dilemma by adding «a little bit» of extra code to the batch file.
As of version 3.10, a /M switch can be used to tell PrintPDF.bat that multiple batch files will be printed. In that case, the /T switch for the Acrobat/Adobe Reader will not be added, and the window will not be closed automatically, but at least all printing will be done in a single «instance» of the Reader.
Also added in this version: support for wildcards. The batch file will check if the wildcards translate into multiple files, and, if so, won’t use the /T switch either.
Finally, a piece of code was added to check if an Acrobat Reader window is open, and, if so, assume multiple PDF printing again.
To make this work in Windows NT 4 or 2000 requires some «third party» software.
If necessary, the batch file will display a message where to download this software, and in the mean time it will assume multiple file printing (leaving the window open after printing).

So, to automatically close the Adobe/Acrobat Reader window after printing, the following requirements must all be met:

  • A single file name must be specified on the command line (wildcards are still allowed, just as long as they «translate» into a single file name)
  • The batch file’s /M switch must not be used
  • No other instance of Adobe/Acrobat Reader must be active

Explanation of PrintPDF.bat:

  • First the command line is checked, and quotes surrounding the file name are removed.
    This is necessary because the print command will surround the file name in quotes again.
    Unfortunately, this makes the batch file useless for file names with commas or ampersands.
  • Then the file type for this extension is read from the registry, using ASSOC and FTYPE.
    Instead of looking up the associated file type returned by ASSOC directly, FTYPE is used to list all file types, and FIND is used to filter out only the appropriate file types.
    This workaround was made necessary by Adobe Reader 7, which doesn’t replace the old file type and association in the registry, but adds a new file type instead.
    This new file type (AcroExch.Document.7) is not the default file type for PDF files.
    Adobe Reader 7 does remove the print command from the «old» default file type, however.
  • Next, the print command(s) for the associated file type(s) are read from the registry, using REGEDIT /E to export the value, FOR /F to read it, and SET to remove the surrounding quotes and remove the escape characters (effectively translating to ‘\’ and ‘\»‘ to ‘»‘).
    It is here that I «cheat» and insert a /T command line switch of my own to close the Adobe Reader window afterwards (thanks to Fabio Quieti for this tip).
    After some tests by Michael Butler, I decided to insert this /T switch only if the batch file can be 100% certain that only one single PDF file is being printed.
    The batch file won’t insert the /T switch if either the /M switch was used on the batch file’s command line, or if more than one file was specified on the command line, or if Acrobat Reader is already active in memory.
    Note: At this point the batch file will probably fail if you have Acrobat Writer installed on your system.
    I’m not 100% certain, but I guess this is caused by lack of a command line print command in Acrobat Writer.
  • The resulting print command is executed with a strangely constructed CALL command, which is necessary to replace «%1» by the PDF file name.

This code has also been «translated» into KiXtart, Perl, Rexx and VBScript versions (without the /T switch, this will follow soon).

A better VBScript version, by Charles Hicks, is available at Planet PDF.

PrintHTM.bat, Version 2.00

Next, a new version of PrintHTM.bat, which skips the printer dialog windows (using KiXtart’s SendKeys( ) function) and accepts a printer name for its second command line parameter:

@ECHO OFF
SETLOCAL

:: Command line parsing
ECHO.%* | FIND "?" >NUL
IF NOT ERRORLEVEL 1 GOTO Syntax
ECHO.%* | FIND "*" >NUL
IF NOT ERRORLEVEL 1 GOTO Syntax
IF NOT [%3]==[] GOTO Syntax
:: Check first parameter (file to be printed)
SET File2Print=%1
IF NOT DEFINED File2Print GOTO Syntax
SET File2Print=%File2Print:"=%
SET File2Print="%File2Print%"
IF NOT DEFINED File2Print GOTO Syntax
IF NOT EXIST "%File2Print:"=%" GOTO Syntax
:: Check second parameter (printer)
SET Printer=%2
IF NOT DEFINED Printer GOTO Print
SET Printer=%Printer:"=%
SET Printer="%Printer%"
REGEDIT /E %TEMP%.\%~n0.dat "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers"
TYPE %TEMP%.\%~n0.dat | FIND "[" | FIND /I %Printer% >NUL
IF ERRORLEVEL 1 (
	ECHO Invalid printer name, using default printer
	SET Printer=
)
DEL %TEMP%.\%~n0.dat

:: Create temporary Kix file to "press" Print button
> %TEMP%.\%~n0.kix ECHO.; Wait a few seconds for the Print dialog to appear
>>%TEMP%.\%~n0.kix ECHO SLEEP 2
>>%TEMP%.\%~n0.kix ECHO.; Press "Print" (Enter) using SendKeys function
:: Replace "Print" with the actual title of the
:: Print dialog for non-US Windows versions
>>%TEMP%.\%~n0.kix ECHO IF SETFOCUS( "Print" ) = 0
>>%TEMP%.\%~n0.kix ECHO    $RC = SENDKEYS( "{ENTER}" )
>>%TEMP%.\%~n0.kix ECHO ENDIF

:Print
:: Actual print command
START RUNDLL32.EXE MSHTML.DLL,PrintHTML %File2Print% %Printer%

:: Call temporary Kix file to "press" Print button, then delete it
START /WAIT KIX32.EXE %TEMP%.\%~n0.kix
DEL %TEMP%.\%~n0.kix

:: Done
GOTO End

:Syntax
ECHO.
ECHO PrintHTM.bat,  Version 2.00 for Windows NT 4 / 2000
ECHO Prints a local HTML file from the command line
ECHO.
ECHO Usage:  %~n0  ˆ<html_fileˆ>  [ ˆ<printerˆ> ]
ECHO.
ECHO Use quotes around the file and printer names if they contain spaces.
ECHO This version uses Kix's SendKeysˆ( ˆ) function to "press" the Print
ECHO button in the Print dialog. Make sure that you have Kix installed on
ECHO your system and that KIX32.EXE can be found in the PATH.
ECHO Modify this batch file if you are using a non-US Windows version
ECHO ˆ(read the comment lines for more informationˆ).
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com

:End
ENDLOCAL

PrintAny.bat, Version 2.01

Finally, a version that will print any known file type by using the associated print command from the registry.

@ECHO OFF
:: For Windows NT 4 or later only
IF NOT "%OS%"=="Windows_NT" GOTO Syntax

:: Localize variables
SETLOCAL
SET PrintCmd=
SET FileType=
SET Temp=%Temp:"=%

:: Command line parsing
IF NOT "%~2"=="" GOTO Syntax
IF     "%~1"=="" GOTO Syntax
IF    "%~n1"=="" GOTO Syntax
IF    "%~x1"=="" GOTO Syntax
ECHO."%~1" | FIND "/" >NUL && GOTO Syntax
ECHO."%~1" | FIND "?" >NUL && GOTO Syntax
ECHO."%~1" | FIND "*" >NUL && GOTO Syntax
IF NOT EXIST "%~1" GOTO Syntax

:: Get the file association from the registry
FOR /F "tokens=1* delims==" %%A IN ('ASSOC %~x1') DO (
	FOR /F "tokens=1 delims==" %%C IN ('FTYPE ^| FIND /I "%%~B"') DO (
		CALL :GetPrintCommand %%~C
	)
)

:: Check if a print command was found
IF NOT DEFINED PrintCmd GOTO NoAssoc

:: Print the file using the print command we just found
CALL START /MIN "PrintAny" %PrintCmd%

:: Done
GOTO End


:GetPrintCommand
:: Get the print command for this file type from the registry
FOR /F "tokens=3*" %%D IN ('REG.EXE Query HKCR\%1\shell\print\command /ve 2^>NUL') DO SET PrintCmd=%%E
IF NOT DEFINED PrintCmd GOTO:EOF
:: "Unescape" the command
SET PrintCmd=%PrintCmd:\"="%
SET PrintCmd=%PrintCmd:""="%
SET PrintCmd=%PrintCmd:\\=\%
:: Remove double double quotes in file name if applicable
ECHO.%PrintCmd% | FINDSTR.EXE /R /C:"\"%%1\"" >NUL && SET PrintCmd=%PrintCmd:"%1"="%%%~1"%
GOTO:EOF


:NoAssoc
CLS
ECHO.
ECHO Sorry, this batch file works only for known file types with associated
ECHO print commands defined in the registry hive HKEY_CLASSES_ROOT.
ECHO No print command seems to be associated with %~x1 files on this computer.
ECHO.


:Syntax
ECHO.
ECHO PrintAny.bat,  Version 2.01 for Windows 7 and later
ECHO Prints any known file type from the command line
ECHO.
ECHO Usage:  PRINTANY  file_to_print
ECHO.
ECHO Where:  "file_to_print"  is the name of the file to be printed
ECHO                          (use double quotes for long file names)
ECHO.
ECHO Notes:  For this batch file to work, the file type's print command has
ECHO         to be defined in the registry hive HKEY_CLASSES_ROOT, and that
ECHO         print command must NOT use DDE for printing.
ECHO         Unfortunately, most Office file types use DDE and hence won't
ECHO         print with this batch file.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com

:End
IF "%OS%"=="Windows_NT" ENDLOCAL

Parameters in File Associations

Often we find "%1" in the commands associated with file types (HKEY_CLASSES_ROOT\FileType\shell\open\command).
In Windows 9x through 2000 (and possibly XP) this would evaluate to the short notation of the fully qualified path of the file of type FileType.
To get the long file name, use "%L" instead of "%1".

I ran some tests in Windows 7 and found the following parameters for file associations:

Parameter Evaluates to
%1 Long fully qualified path of file
%D Long fully qualified path of file
%H 0
%I :number:number
%L Long fully qualified path of file
%S 1
%V Long fully qualified path of file
%W Long fully qualified path of parent folder

So far I don’t have a clue as to what %H, %I and %S are for.
And it seems we can no longer get a short file name even if we want to.
For backward compatibility it would seem prudent to stick with %1, %L and %W (though I haven’t tested %W in older Windows versions yet).

  • GetPrint.vbs
    Lists non-DDE-based Print and PrintTo commands for all registered file types.
    Read the script’s on-screen help to find out how to filter the print commands for a specific file type or extension.
  • PrintDoc.bat
    Uses Microsoft Word and CMCDDE to print a Word document on the default or any specified printer.
  • PrintDoc.vbs
    Based on a script by Arnout van der Vorst, this script prints a specified Word document on the default printer.
  • PrintPPT.bat
    A simple batch file that uses PowerPoint or the PowerPoint Viewer to open a print dialog for a powerPoint file.
  • ClassExec
    A command line utility to execute any command, including DDE commands, associated with a file type or extension.
    Use it to open, print, view or edit files, whatever is registered for that file type in HKEY_CLASSES_ROOT.
  • Command Line Switches
    Open, print, or sometimes even convert files on the command line with GUI programs!
  • Command Line Printer Control
    using RUNDLL32 PRINTUI.DLL,PrintUIEntry ...
  • PrintAny.exe can print «any» file, as long as it has a registered print command
  • Since Excel doesn’t have a registered print command, PrintExcel.exe was created to print Excel sheets

Or were you looking for this…?

  • To print to (create) PDF have a look at the PDF printers listed here

page last modified: 2016-09-19; loaded in 0.0047 seconds

  • SS64
  • CMD
  • How-to

PRINT.exe

Print a file or files to a local or network printer.

Syntax
      PRINT [/D:device] [pathname(s)]

Key
   device    Either a local printer (LPTx, COMx )
             or a network printer by its sharename (\\servername\print_share)

   pathname  The file or files to be printed.

The default device is PRN. The values PRN and LPT1 refer to the same parallel port.
The PRINT command is most often used in conjunction with an LPR print queue — sending plain ASCII text.

All recent versions of Windows have automatic default printer management enabled by default. This can be enabled or disabled via policy:
‘User Configuration ➞ Policies ➞ Administrative Templates ➞ Control Panel ➞ Printers’: ‘Turn off Windows default printer management’.

When printing to file, the destination file must not already exist.
PRINT will add a FormFeed character (ASCII 12) to the end of each print job.

Examples

Delete a print job:

Use Control Panel ➞ Printers (GUI) or use
NET PRINT job# /DELETE

It is possible to delete the spool .spl and .shd files from %SystemRoot%\system32\spool\PRINTERS but this should be a last resort — stop the spooler service before deleting any files.

Status Messages

Many modern laser printers have a status display panel, the ‘Ready’ message can be changed using HP Job Language (PCL/PJL) like this:
Echo @PJL RDYMSG DISPLAY =»Ready Message» >\\server\printQ
e.g.
Echo @PJL RDYMSG DISPLAY =»Ready Printer64″ >\\printserv1\printer64

It’s not essential to include the word «Ready» in the ready message, but
the status display has 16 characters, which is usually enough space
for «Ready» plus the printer Name/Number.
e.g. «Ready Printer123», «Ready Prn1234567», «Ready #123-45678» etc

Note that when a printer error or jam occurs the status message will change, so adding a printer identifier to the status display does not replace the need for a physical label. It can still be very useful when the physical
label is wrong or missing.

Printing requires the Spooler service to be running.

“Without books the development of civilization would have been impossible. They are the engines of change, windows on the world, ‘Lighthouses’ as the poet said ‘erected in the sea of time’. They are companions, teachers, magicians, bankers of the treasures of the mind, Books are humanity in print” ~ Arthur Schopenhauer

Related commands

Print to File — Configure a printer to always print to a file.
NET PRINT — View and Delete print jobs.
NET VIEW — List all printer share names for a print server.
Defptr — Default Printer. (Win 2K ResKit)
PRNCNFG — Display, configure or rename a printer.
PRINTBRM — Print queue Backup/Recovery.
WMIC PRINTER — Set printing options through WMI.
Q234270 — Group Policies to Control Printers.
HP PCL 5 Printer Language Technical Reference Manual — Full PCL Manual.
Resource Kit: prncnfg.vbs, prndrvr.vbs, prnjobs.vbs, prnmngr.vbs, prnport.vbs, prnqctl.vbs, pubprn.vbs
Equivalent PowerShell: Out-Printer — Send the output to a printer (lp).
Equivalent bash command (Linux): printf — Format and print data.


Copyright © 1999-2025 SS64.com
Some rights reserved

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Как перепрошить компьютер на windows 7
  • The windows installer service could not be accessed this can occur if you
  • Ошибка 0x00000007a windows 7
  • Man pages for windows
  • Как исправить ошибку windows не удалось найти драйвер для сетевого адаптера