Generate prn files in batch

Dell Dell optiplex 755 desktop desktop c...
June 29, 2010 at 11:54:49
Specs: Windows XP SP3, 4GB RAM

I have a directory containing some word documents (*.doc). I need to code a batch file which can open the doc and if the document contains the text "LOGO THREAD COLOR SHEET", I need to create a {filename}.prn file.

Can some one help?



See More: Generate prn files in batch

Report •

June 30, 2010 at 06:35:57
This is easy if the contents of the .prn file aren't specified:
for /f "delims=" %%a in ('findstr /m /c:"LOGO THREAD COLOR SHEET" *.doc') do copy nul "%%~Na.prn" > NUL

For anything more substantial, you're going to have to tell us either what you expect the .prn file to contain.

Report •

June 30, 2010 at 07:07:48

I have entered your command in a text file and saved it as a batch file. Copied this batch file in a directory which contains my *.doc files and ran it.

The *.prn files were generated, but all of them were 0 bytes files.

All the word documents contains standard template with data. It contains some text fields and a table in it.

I don't have an option to attach the word document here. Do you want me to upload the document somewhere so that you can see what it contains?


Report •

June 30, 2010 at 07:55:32
Shouldn't this

do copy nul "%%~Na.prn" > NUL

be this...

do copy %%a "%%~Na.prn" > NUL

Could be wrong.

Report •

Related Solutions

June 30, 2010 at 07:59:30
Thanks for the reply.

Tried your suggesion, but still getting 0 byte files.


Report •

June 30, 2010 at 11:13:36
The problem is I have no idea what .prn files are. Apparently they have something to do with .doc files, but you need to explain this relationship better, pnbalaji.

Report •

June 30, 2010 at 11:31:42

A .prn file is a print file that contains the information from the word document. It can be generated from word's print menu by selecting the option "Print to File".

A .prn file contains binary data which can be interpreted by the printer while printing.

It can be printed using the following command.

copy /b filename.prn LPT1: if the printer is directly attached to the PC via parallel port.

Hope this helps.


Report •

June 30, 2010 at 13:22:11
That's harder to do, and would probably require the use of Word's COM objects. Batch cannot access COM, so here's a VBScript:

Const wordVisible = True 'or False
WScript.Quit Main

Function Main
  On Error Resume Next
  Dim fso, word, f
  Dim doc, r
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set word = CreateObject("Word.Application")
  word.Visible = wordVisible 
  word.DisplayAlerts = False
  For Each f In fso.GetFolder(".").Files
    If LCase(fso.GetExtensionName(f)) = "doc" Then
      Set doc = word.Documents(word.Documents.Open(f.Path, False, True, False))
      Set r = doc.Range
      If r.Find.Execute(searchStr, True, False, False, False, False, True, 1) Then _
        doc.PrintOut False, False, 0, GetPrnName(f), , , 0, 1, , 0, True
      Set r = Nothing
      doc.Close 0
    End If
  Next 'f
  word.Quit 0
  Main = Err.Number
End Function

Function GetPrnName(path) 'As String
  Dim name : name = Split(path, ".")
  name(UBound(name)) = "prn"
  GetPrnName = Join(name, ".")
End Function

Report •

Ask Question