Windows bat file print

Print Files from Batch Files

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

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:

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:

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

Unlike Notepad, Wordpad does have a /PT switch:

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

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:

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:

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:

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.

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).
Читайте также:  Установка eplan windows 10

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

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.
Читайте также:  Realtek wifi ������� ��� linux

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:

PrintAny.bat, Version 2.01

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

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).

Отправить файл на печать

Добрый день.
В виду отсутствия информации в поиске, прошу помочь с вопросом по batch.

Отправить файл на печать print /D:название_принтера 4.txt не работает, напишите пожалуйста на примере, как должна выглядеть рабочая команда

Отправить Html файл на печать
Добрый день. Если ли способ программно отправить некий html файл на печать? При выполнении.

Как отправить файл на печать
Как отправить файл на печать ?

Как текстовый файл отправить на печать
Как используя возможности C# -а можно настроить принтер(не диалоговом окне а программно) и.

Как отправить на печать файл *.odt средствами python?
python 2.7.3, ОС — МСВС 3.0. Требуется отправить на печать документ odt с возможностью задания.

Заказываю контрольные, курсовые, дипломные и любые другие студенческие работы здесь или здесь.

Отправить картинку на печать
Никто не знает как отправить картинку (не текст) на печать! Так и живем 🙁 Мой ход мысли примерно.

Отправить форму в печать
В мебельной программе имеется редактор скриптов для создания скриптов для программы. Написал.

Как отправить на печать
Подскажите пожалуйста, есть у меня приложение, в ней есть DataGridView со столбцами. Необходимо.

Как отправить таблицу на печать?
Как отправить активную таблицу на печать? С помощью каких инструментов? Какой код или что-то в этом.

How do I echo and send console output to a file in a bat script?

I have a batch script that executes a task and sends the output to a text file. Is there a way to have the output show on the console window as well?

Is there a way to have the output of dir display in the console window as well as put it into the text file?

12 Answers 12

No, you can’t with pure redirection.
But with some tricks (like tee.bat) you can.

I try to explain the redirection a bit.

You redirect one of the ten streams with > file or , this means the stream 1 (STDOUT) will be redirected.
So you can redirect any stream with prepending the number like 2> err.txt and it is also allowed to redirect multiple streams in one line.

In this example the «standard output» will go into files.txt, all errors will be in err.txt and the stream3 will go into nothing.txt (DIR doesn’t use the stream 3).
Stream0 is STDIN
Stream1 is STDOUT
Stream2 is STDERR
Stream3-9 are not used

But what happens if you try to redirect the same stream multiple times?

«There can be only one», and it is always the last one!
So it is equal to dir > two.txt

Ok, there is one extra possibility, redirecting a stream to another stream.

2>&1 redirects stream2 to stream1 and 1>files.txt redirects all to files.txt.
The order is important here!

are different. The first one redirects all (STDOUT and STDERR) to NUL,
but the second line redirects the STDOUT to NUL and STDERR to the «empty» STDOUT.

As one conclusion, it is obvious why the examples of Otávio Décio and andynormancx can’t work.

Both try to redirect stream1 two times, but «There can be only one», and it’s always the last one.
So you get

And in the first sample redirecting of stream1 to stream1 is not allowed (and not very useful).

Open text file and program shortcut in a Windows batch file

I have two files in the same folder that I’d like to run. One is a .txt file, and the other is the program shortcut to an .exe . I’d like to make a batch file in the same location to open the text file and the shortcut then close the batch file (but the text file and program remain open).

I tried this with no luck:

Also didn’t work:

12 Answers 12

I was able to figure out the solution:

This would have worked too. The first quoted pair are interpreted as a window title name in the start command.

Don’t put quotes around the name of the file that you are trying to open; start «myfile.txt» opens a new command prompt with the title myfile.txt , while start myfile.txt opens myfile.txt in Notepad. There’s no easy solution in the case where you want to start a console application with a space in its file name, but for other applications, start «» «my file.txt» works.

The command-line syntax for opening a text file is:

File types supported by this command include (but are not limited to): .doc, .txt, .html, .log

If the contents is too long, you can add «|more» after «type filename.txt», and it will pause after each screen; to end the command before the end of the file, you can hold Ctrl + C .

to open the file.

Another example is

You can also do:

The C:\Users\kemp\Install\ is your PATH. The Text1.txt is the FILE.

«location of notepad file» > notepad Filename

C:\Users\Desktop\Anaconda> notepad myfile

In some cases, when opening a LNK file it is expecting the end of the application run.

In such cases it is better to use the following syntax (so you do not have to wait the end of the application):

To open a TXT file can be in the way already indicated (because notepad.exxe not interrupt the execution of the start command)

The command start [filename] opened the file in my default text editor.

This command also worked for opening a non-.txt file.

If you are trying to open an application such as Chrome or Microsoft Word use this:

And repeat this for all of the applications you want to open.

Читайте также:  Windows 10 personal data
Оцените статью