- Printing from Windows 7 command line
- 5 Answers 5
- Printing PDFs from Windows Command Line
- 12 Answers 12
- How do I display a text file content in CMD?
- 12 Answers 12
- Print Document. Print Метод
- Определение
- Исключения
- Примеры
- Комментарии
- Print Files from Batch Files
- 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
Printing from Windows 7 command line
I’m looking to automate some printing in Windows 7. Ideally from command line. Just as if I would’ve right-clicked and selected «Print».
Tried the command:
That printer is supposed to created PDF files (like the XPS printer) but haven’t managed to get anything show up in the printer queue. Ideas?
5 Answers 5
The print command uses the following synatx for the /d: switch:
Specifies the printer on which you want to print the job. You can specify a local printer by specifying the port on your computer to which the printer is connected. Valid values for parallel ports are LPT1, LPT2, and LPT3. Valid values for serial ports are COM1, COM2, COM3, and COM4. You can also specify a network printer by its queue name (\ServerName\ShareName). If you do not specify a printer, the print job is sent to LPT1.
This means you can’t use «CutePDF Writer» as an argument.
There are two ways that should work:
Assign a COM port to your printer.
- Open Start → Control Panel → Devices and Printers.
- Right-click the CutePDF Writer and select Printer Properties.
- In the Ports tab, assign an unused port from COM1: to COM4: to your printer.
If you chose, e.g., COM3 , print using the following command:
Share the printer.
- Open Start → Control Panel → Devices and Printers.
- Right-click the CutePDF Writer and select Printer Properties.
- In the Sharing tab, decide to share the printer and assign a share name to it.
Printing PDFs from Windows Command Line
I’m trying to print all pdfs in current dir. When I call this bash script in cmd ( singlepdf.sh ): ‘»C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe»‘ /t Gemeinde_348_BioID_842_alt.pdf everything’s working fine.
When calling multiplepdfs.sh with this content:
The echo shows that files are addressed correctly in the loop — but then I get the error «C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe»: No such file or directory
Can someone help out with this issue?
Edit: BTW, I have msys mingw installed
12 Answers 12
I know this is and old question, but i was faced with the same problem recently and none of the answers worked for me:
- Couldn’t find an old Foxit Reader version
- As @pilkch said 2Printer adds a report page
- Adobe Reader opens a gui
After searching a little more i found this: http://www.columbia.edu/
It’s a simple exe that you call with the filename and it prints to the default printer (or one that you specify). From the site:
PDFtoPrinter is a program for printing PDF files from the Windows command line. The program is designed generally for the Windows command line and also for use with the vDos DOS emulator.
To print a PDF file to the default Windows printer, use this command:
To print to a specific printer, add the name of the printer in quotation marks:
If you want to print to a network printer, use the name that appears in Windows print dialogs, like this (and be careful to note the two backslashes at the start of the name and the single backslash after the servername):
How do I display a text file content in CMD?
I want to display the content of a text file in a CMD window. In addition, I want to see the new lines that added to file, like tail -f command in Unix.
12 Answers 12
You can use the more command. For example:
We can use the ‘type’ command to see file contents in cmd.
More information can be found HERE.
I don’t think there is a built-in function for that
This opens the files in the default text editor in windows.
This displays the file in the current window. Maybe this has params you can use.
There is a similar question here: CMD.EXE batch script to display last 10 lines from a txt file So there is a «more» command to display a file from the given line, or you can use the GNU Utilities for Win32 what bryanph suggested in his link.
You can use the ‘more’ command to see the content of the file:
Using a single PowerShell command to retrieve the file ending:
It applies to PowerShell 3.0 and newer.
Another option is to create a file called TAIL.CMD with this code:
To do this, you can use Microsoft’s more advanced command-line shell called «Windows PowerShell.» It should come standard on the latest versions of Windows, but you can download it from Microsoft if you don’t already have it installed.
To get the last five lines in the text file simply read the file using Get-Content , then have Select-Object pick out the last five items/lines for you:
If you want it to display the content of the file live, and update when the file is altered, just use this script:
That will repeat forever until you close the cmd window.
You can do that in some methods:
One is the type command: type filename Another is the more command: more filename With more you can also do that: type filename | more
The last option is using a for for /f «usebackq delims=» %%A in (filename) do (echo.%%A) This will go for each line and display it’s content. This is an equivalent of the type command, but it’s another method of reading the content.
If you are asking what to use, use the more command as it will make a pause.
There is no built in option available with Windows. To constantly monitor logs you can use this free application BareTailPro.
You can get the TAIL utility from the Windows Server 2003 Resource Kit Tools.
If you want to display for example all .config (or .ini ) file name and file content into one doc for user reference (and by this I mean user not knowing shell command i.e. 95% of them), you can try this :
- ForFiles : loop on a directory (and child, etc) each file meeting criteria
- able to return the current file name being process (@file)
- able to return the full path file being process (@path)
- Type : Output the file content
Print Document. Print Метод
Определение
Запускает процесс печати документа. Starts the document’s printing process.
Исключения
Принтер, заданный свойством PrinterName, не существует. The printer named in the PrinterName property does not exist.
Примеры
Следующий пример кода выводит файл, указанный в командной строке, на принтер по умолчанию. The following code example prints the file that is specified through the command line to the default printer.
Для этого примера требуется, чтобы каждая строка соответствовала ширине страницы. The example requires that each line fits within the page width.
Комментарии
Укажите выходные данные для печати, обрабатывая PrintPage событие и используя объект, Graphics входящий в PrintPageEventArgs . Specify the output to print by handling the PrintPage event and by using the Graphics included in the PrintPageEventArgs.
Используйте PrinterSettings.PrinterName свойство, чтобы указать, какой принтер должен печатать документ. Use the PrinterSettings.PrinterName property to specify which printer should print the document.
PrintМетод выводит документ на печать без использования диалогового окна печати. The Print method prints the document without using a print dialog. Используйте, PrintDialog Если вы хотите предоставить пользователю возможность выбора параметров печати. Use a PrintDialog when you want to offer the user the ability to choose print settings.
Если исключение, которое не обрабатывается методом, Print вызывается во время печати, печать документа прерывается. If an exception that is not handled by the Print method is thrown during printing, the printing of the document is aborted.
Print Files from Batch Files
Print Text 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.
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:
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). |
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.
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).