Tutorials

Nine common jobs, from your first print run to an unattended nightly batch. Every command can be copied and run as is — just replace Office A with your printer name.

01Print a folder, subfolders included

-src-options scansfdryrunsort

Print every printable file in a folder in file-name order — after a dry run to make sure nothing surprises you.

  1. Find the printer name

    Copy the name exactly as listed and put it after -prn (in quotes if it has spaces).

    Command Prompt
    cmd4printer -options showprnlist
  2. Dry run

    Nothing is printed; you see the pages and sheets for each file and any file that cannot be opened.

    Command Prompt
    cmd4printer -src "D:\ToPrint" -prn "Office A" -options scansf:yes sort:name dryrun:yes
  3. Print

    Drop dryrun:yes. The summary line at the end shows submitted, failed and skipped files and the exit code.

    Command Prompt
    cmd4printer -src "D:\ToPrint" -prn "Office A" -options scansf:yes sort:name
Only some file types? Use a wildcard such as -src "D:\ToPrint\*.pdf"; several wildcards can follow one -src.

02Per-file copies with a CSV list

-src @list.csv#copies#pages#printer

Different copies, pages or even printers for each file? Put them in a spreadsheet.

  1. Prepare the list

    Create a CSV in Excel or Notepad (an .xlsx works too). The first row holds the column names: #file is required, #copies, #pages and #printer are optional — empty cells fall back to the command line. Relative paths are resolved from the list's folder.

    D:\Jobs\list.csv
    #file,#copies,#pages,#printer
    contracts\purchase.pdf,3,,
    quote.pdf,1,1,
    drawings\site-plan.pdf,2,1-2,Plotter A3
    acceptance.docx,2,last,
  2. Check the copies with a dry run
    Command Prompt
    cmd4printer -src "@D:\Jobs\list.csv" -prn "Office A" -options dryrun:yes
  3. Print

    Rows without #printer go to the printer given with -prn.

    Command Prompt
    cmd4printer -src "@D:\Jobs\list.csv" -prn "Office A" -props duplex:vertical
Note: the #printer column only applies when each file is its own job; with spjob:yes everything goes into one job on one printer.

03Duplex printing from a specific tray

-props duplextraypssp
  1. Look up the tray names

    Tray names come from the printer driver, for example Tray 1, Tray 2 or Manual Feed.

    Command Prompt
    cmd4printer -prn "Office A" -options showtraylist
  2. Long-edge duplex from tray 2

    vertical flips on the long edge (like a book), horizontal on the short edge (like a notepad).

    Command Prompt
    cmd4printer -src "D:\Reports\*.pdf" -prn "Office A" -props duplex:vertical tray:"Tray 2"
  3. Letterhead for the first page

    Load letterhead into tray 1 and use pssp so page 1 comes from tray 1 and the rest from tray 2 — still one print job.

    Command Prompt
    cmd4printer -src "D:\Letters\*.docx" -prn "Office A" -props duplex:simplex pssp:"1=Tray 1;2-last=Tray 2"

04Print a booklet

-multipps bookletbooklet_binding

Turn an A4 (or Letter) document into a half-size booklet you can fold and staple.

  1. Check the sheet count

    Each sheet carries two pages per side, and the page count is padded to a multiple of four.

    Command Prompt
    cmd4printer -src "D:\Manual.pdf" -prn "Office A" -multipps enabled:yes booklet:yes -options dryrun:yes
  2. Print

    Short-edge duplex is switched on for you; no need to set duplex. Use booklet_binding:right for right-to-left documents.

    Command Prompt
    cmd4printer -src "D:\Manual.pdf" -prn "Office A" -props papersize:A4 -multipps enabled:yes booklet:yes booklet_binding:left
  3. Fold and staple

    Fold the whole stack in half and staple along the fold.

No duplex unit? Booklets need two-sided printing; on a simplex printer use plain N-up instead, e.g. hor_pages:2.

05Add a watermark and a QR code

-oper textwatermark-oper barcodemacros

Stamp paid invoices with a translucent red “PAID” and add a QR code with the file name in the bottom-right corner.

  1. Preview by printing to PDF

    Use the built-in Microsoft Print to PDF and write the result to a file to check position and color.

    Command Prompt
    cmd4printer -src "D:\Invoices\*.pdf" -prn "Microsoft Print to PDF" -options outputfile:"D:\Preview\{SrcFileName}.pdf" -oper textwatermark text:"PAID" fontsize:36 halign:center valign:center angle:30 color:"r:200,g:0,b:0,a:160" bgcolor:"r:255,g:255,b:255,a:0" -oper barcode format:qr_code contents:"{SrcFileName}" halign:right valign:bottom size:15
  2. Adjust

    a: is the alpha value (0–255); a:0 in bgcolor makes the text background transparent; add pages:1 to stamp the first page only.

  3. Send it to the real printer

    Switch -prn to your printer and drop outputfile. You can also save the stamps on the Watermarks page of the settings window so you don't have to type them each time.

06Nightly scheduled printing with archiving

Task Scheduler-postproc movesilentmin_age

Every day at 22:00, print whatever landed in the inbox folder and archive it, so all that is left in the morning is a glance at the failure list.

  1. Write a batch file

    Save it as D:\Scripts\nightly-print.bat. min_age:120 skips files written in the last two minutes; silent:yes alerts:no keeps an unattended run free of pop-ups.

    nightly-print.bat
    @echo off
    rem Nightly print: files in D:\ToPrint are printed and moved to D:\Printed, failures to D:\Failed
    cmd4printer -src "D:\ToPrint\*.*" -prn "Office A" ^
      -props duplex:vertical ^
      -options scansf:yes sort:name min_age:120 silent:yes alerts:no log:yes logpath:"D:\PrintLogs" unprinted_files_log:"D:\PrintLogs\failed.txt" ^
      -postproc passed:move passed_dir:"D:\Printed" failed:move failed_dir:"D:\Failed"
    set RC=%ERRORLEVEL%
    echo %date% %time% exit code %RC%>>"D:\PrintLogs\nightly.txt"
    exit /b %RC%
  2. Run it once by hand

    Run it from Command Prompt and check that the log and archive folders appear as expected.

    Command Prompt
    D:\Scripts\nightly-print.bat
  3. Create the scheduled task

    This creates a task for the current user that runs daily at 22:00 (while that user is logged on):

    Command Prompt
    schtasks /Create /TN "Cmd4Printer nightly print" /TR "D:\Scripts\nightly-print.bat" /SC DAILY /ST 22:00

    To run it while nobody is logged on, open the task in Task Scheduler, choose “Run whether user is logged on or not” and enter the account's password.

Printing Office files? For “Run whether user is logged on or not”, log on once with that account and open Word/Excel to finish their first-run setup, and make sure C:\Windows\System32\config\systemprofile\Desktop exists (plus C:\Windows\SysWOW64\config\systemprofile\Desktop on 64-bit Windows).

07Using exit codes in a batch file

%ERRORLEVEL%unprinted_files_log-options output:json

Let the exit code decide what happens next: finish on success, reprint only the failures, or ask a person to check when the result is unknown.

Exit codeMeaningWhat to do
0All files were submitted to the print queue—
1Internal errorExport logs with -options export_path and contact support
2Usage error: wrong switch, value or syntaxRead the allowed values and the example in the error message
3Environment problem: printer, tray, paper or files not found, or the print spooler is stoppedCheck names with showprnlist / showtraylist
4Some files failed; the others were submittedCheck unprinted_files_log and reprint only those files
5Submit result unknown; the job may already be queuedCheck the print queue before reprinting
6Cancelled (Ctrl+C)—
  1. Batch file

    Compare exactly with if %RC% EQU n; if errorlevel n means “n or higher” and is easy to get wrong.

    print-invoices.bat
    @echo off
    cmd4printer -src "D:\Invoices\*.pdf" -prn "Office A" -options silent:yes unprinted_files_log:"D:\Invoices\failed.txt"
    set RC=%ERRORLEVEL%
    if %RC% EQU 0 goto done
    if %RC% EQU 4 goto partly
    if %RC% EQU 5 goto check
    echo Nothing printed: exit code %RC% (2 = bad command, 3 = printer or files not found)
    exit /b %RC%
    :partly
    echo Some files failed; retrying only those in 10 minutes
    timeout /t 600 /nobreak >nul
    cmd4printer -src "@D:\Invoices\failed.txt" -prn "Office A" -options silent:yes
    exit /b %ERRORLEVEL%
    :check
    echo Submit result unknown: check the print queue before reprinting
    exit /b 5
    :done
    echo All files submitted
    exit /b 0
  2. PowerShell: read the JSON result

    Use output:json when you need the state of each file.

    PowerShell
    $r = cmd4printer -src "D:\Invoices\*.pdf" -prn "Office A" -options output:json | Out-String | ConvertFrom-Json
    "Exit code: " + $r.exitCode
    $r.items | Where-Object state -eq 'failed' | Select-Object source, @{n='error'; e={$_.error.message}}

08Several printers in one run

-cmdlines

Files for different departments go to different printers, or the command is too long for one line: put the commands in a text file and run them with -cmdlines.

  1. Write the command file
    D:\Jobs\commands.txt
    ; one command per line, "cmd4printer" may be omitted; lines starting with ; # :: rem are comments
    -src "D:\Finance\*.pdf" -prn "Finance HP M404" -props duplex:vertical
    -src "D:\Warehouse\Labels\*.zpl" -prn "Warehouse label printer"
    -src "D:\Drawings\*.dxf" -prn "Plotter A3" -props papersize:A3 orient:landscape
  2. Run it

    Lines run in order; a fatal problem such as a bad command or a missing printer stops the run, and the exit code is the most severe one of all lines.

    Command Prompt
    cmd4printer -cmdlines "D:\Jobs\commands.txt"

09Calling Cmd4Printer from Python, C# and others

output:jsonsubprocessProcess.Start

Any language that can start a process can drive Cmd4Printer. Pass arguments as a list (don't build quoted strings yourself) and read the exit code or the JSON result.

Python
import json, subprocess
p = subprocess.run(["cmd4printer", "-src", r"D:\Inbox\*.pdf", "-prn", "Office A",
                    "-options", "output:json"], capture_output=True, text=True, encoding="utf-8")
result = json.loads(p.stdout)
print(p.returncode, result["counts"])
C#
var psi = new ProcessStartInfo("cmd4printer",
    "-src \"D:\\Inbox\\*.pdf\" -prn \"Office A\" -options silent:yes")
{ UseShellExecute = false, CreateNoWindow = true };
using (var p = Process.Start(psi)) { p.WaitForExit(); Console.WriteLine(p.ExitCode); }