Link to home
Start Free TrialLog in
Avatar of kes3
kes3

asked on

Batch process 200 TXT files in one go with PDF creator using the supplied convert2pdf.vbs script

I am using source forge PDFCreator to batch process 200 TXT files to PDF.
I am using W2000 and can drag and drop up to about 8 files on the file convert2pdf.vbs which is a script included with the installation of PDFCreator. If I Drag and drop the whole 200 the popup message complains as follows
"Unable to process - arguments list is too long"
I need to be able to process all 200 at once so I am not sitting there all day.
Can anyone provide a simple batch file that I can use to feed the TXT files ( which are all in one directory ) to the convert2pdf.vbs script.

****************

' Convert2PDF script
' Part of PDFCreator
' License: GPL
' Homepage: http://www.sf.net/projects/pdfcreator
' Version: 1.0.0.0
' Date: September, 1. 2005
' Author: Frank Heindörfer
' Comments: This script convert a printable file in a pdf-file using
'           the com interface of PDFCreator.

Option Explicit

Const maxTime = 30    ' in seconds
Const sleepTime = 250 ' in milliseconds

Dim objArgs, ifname, fso, PDFCreator, DefaultPrinter, ReadyState, _
 i, c, AppTitle, Scriptname, ScriptBasename

Set fso = CreateObject("Scripting.FileSystemObject")

Scriptname = fso.GetFileName(Wscript.ScriptFullname)
ScriptBasename = fso.GetFileName(Wscript.ScriptFullname)

AppTitle = "PDFCreator - " & ScriptBaseName

If CDbl(Replace(WScript.Version,".",",")) < 5.1 then
 MsgBox "You need the ""Windows Scripting Host version 5.1"" or greater!", vbCritical + vbSystemModal, AppTitle
 Wscript.Quit
End if

Set objArgs = WScript.Arguments

If objArgs.Count = 0 Then
 MsgBox "Syntax: " & vbtab & Scriptname & " <Filename>" & vbcrlf & vbtab & "or use ""Drag and Drop""!", vbExclamation + vbSystemModal, AppTitle
 WScript.Quit
End If

Set PDFCreator = Wscript.CreateObject("PDFCreator.clsPDFCreator", "PDFCreator_")
PDFCreator.cStart "/NoProcessingAtStartup"
With PDFCreator
 .cOption("UseAutosave") = 1
 .cOption("UseAutosaveDirectory") = 1
 .cOption("AutosaveFormat") = 0                              ' 0 = PDF


'The following are required to set security of any kind
  .cOption("PDFUseSecurity") = 1
  .cOption("PDFOwnerPass") = 1
  .cOption("PDFOwnerPasswordString") = "password"

'Set document Information

  .cOption("UseStandardAuthor") = 1
  .cOption("StandardAuthor") = ""

  .cOption("UseCreationDateNow") = 1
  .cOption("StandardCreationdate") = ""

  .cOption("StandardModifydate") = ""
  .cOption("StandardSubject") = ""
  .cOption("StandardTitle") = ""



 DefaultPrinter = .cDefaultprinter
 .cDefaultprinter = "PDFCreator"
 .cClearcache
End With

For i = 0 to objArgs.Count - 1
 With PDFCreator
  ifname = objArgs(i)
  If Not fso.FileExists(ifname) Then
   MsgBox "Can't find the file: " & ifname, vbExclamation + vbSystemModal, AppTitle
   Exit For
  End If
  if Not .cIsPrintable(CStr(ifname)) Then
   MsgBox "Converting: " & ifname & vbcrlf & vbcrlf & _
    "An error is occured: File is not printable!", vbExclamation + vbSystemModal, AppTitle
   WScript.Quit
  End if

  ReadyState = 0
  .cOption("AutosaveDirectory") = fso.GetParentFolderName(ifname)
  .cOption("AutosaveFilename") = fso.GetBaseName(ifname)
  .cPrintfile cStr(ifname)
  .cPrinterStop = false

  c = 0
  Do While (ReadyState = 0) and (c < (maxTime * 1000 / sleepTime))
   c = c + 1
   Wscript.Sleep sleepTime
  Loop
  If ReadyState = 0 then
   MsgBox "Converting: " & ifname & vbcrlf & vbcrlf & _
   "An error is occured: Time is up!", vbExclamation + vbSystemModal, AppTitle
   Exit For
  End If
 End With
Next

With PDFCreator
 .cDefaultprinter = DefaultPrinter
 .cClearcache
 WScript.Sleep 200
 .cClose
End With

'--- PDFCreator events ---

Public Sub PDFCreator_eReady()
 ReadyState = 1
End Sub

Public Sub PDFCreator_eError()
 MsgBox "An error is occured!" & vbcrlf & vbcrlf & _
  "Error [" & PDFCreator.cErrorDetail("Number") & "]: " & PDFcreator.cErrorDetail("Description"), vbCritical + vbSystemModal, AppTitle
 Wscript.Quit
End Sub
Avatar of RobSampson
RobSampson
Flag of Australia image

Are all of these files in one folder?  You should be able to set up a parent script that takes, say 10 files at a time and called the VBS like this:
wscript c:\convert2pdf.vbs file1.txt file2.txt "file 3.txt" ....etc

If they are all in the one folder, this becomes much easier....

Regards,

Rob.
i just threw this together. name it findnrun.vbs and throw it in the same directory as the Convert2PDF.vbs.

haven't tested it, but it looks like it should do what you need.  
double click on it and it will tell you what to do.


On Error Resume Next
  Dim fso, folder, files, NewsFile,sFolder
 
  Set fso = CreateObject("Scripting.FileSystemObject")
  sFolder = Wscript.Arguments.Item(0)
  If sFolder = "" Then
      Wscript.Echo "No Folder parameter was passed"
      Wscript.Echo "findnrun.vbs <folder location With all the pdf files>"
      Wscript.Echo "example:"
      Wscript.Echo "cscript findnrun.vbs c:\temp\pdf"
      Wscript.Quit
  End If
  Set NewFile = fso.CreateTextFile(sFolder&"\FileList.txt", True)
  Set folder = fso.GetFolder(sFolder)
  Set files = folder.Files
 
  For each folderIdx In files
    NewFile.WriteLine(folderIdx.Name)
  Next
  NewFile.Close
 
 
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(sFolder&"\FileList.txt", ForReading)
i = 0
Do While objTextFile.AtEndOfStream <> True
 strNextLine = objTextFile.Readline
 objDictionary.Add i, strNextLine
 i = i + 1
Loop
Set objShell=CreateObject("WScript.Shell")
For Each objItem in objDictionary

objShell.Run("cscript Convert2PDF.vbs " & objitem )
 
Next
Avatar of kes3
kes3

ASKER

Rob, Weeilo - thanks.

Rob : I want a script where I don't have to name the TXT files in the batch file.

Weellio : PDFCreator does not like more than one copy of itself running at once. What is needed is a window then a delay say of 1.5 mins before a new window is called to give PDF creator enough time to finish the previous file run ( some of the TXT files are huge ).

Thanks ...
kes3, that's fine.  What I meant by sample was, if all of your files are in one folder, it's easy to set up a VBS that can read all of the file names, and start the Convert2PDF.vbs file for you.

Regards,

Rob.
Avatar of kes3

ASKER

Rob, Yes they are all in one folder. Thanks.
Hi try this, but make sure to test it.  I'm not sure if your Convert2PDF.vbs will wait for it to finish its processes before it will go back to this script.  As you said it doesn't like running itself more than once at a time, this could be a problem,......

'===================
strFolderPath = "C:\Temp\"
intMaxFiles = 10
strExtension = ".txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolderPath)
Set objShell = CreateObject("WScript.Shell")
intFiles = 0
strArgs = ""
For Each objFile In objFolder.Files
      strFileName = Mid(objFile.Name, InStrRev(objFile.Name, "\") + 1)
      If LCase(Right(strFileName, 4)) = strExtension Then
            intFiles = intFiles + 1
            strArgs = strArgs & " """ & strFileName & """"
            If intFiles = intMaxFiles Then
                  strCommand = "wscript.exe """ & Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Convert2PDF.vbs""" & strArgs
                  Set objExec = objShell.Exec(strCommand)
                  While objExec.Status
                     ' wait until the script finishes
                  Wend
                  intFiles = 0
                  strArgs = ""
            End If
      End If
Next
MsgBox "Done"
'================

Regards,

Rob.
Avatar of kes3

ASKER

Thanks Rob.
It produces the same error as when I try to process more than 8 or so files with Convert2PDF.vbs by dragging them onto that file. The error is given in a pop up box : "Unable to execute - arguments list is too long". I think this means the command line cannot accept all the file names on the same line. Can we have a batch file / vbs file that has all 200 dragged onto it but only passes one at a time to convert2pdf.vbs.
ASKER CERTIFIED SOLUTION
Avatar of William Elliott
William Elliott
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kes3

ASKER

Folks,
I got them both working after a fashion so I'll distribute points evenly : I discovered that I can open PDFCreator and drag all 200 TXT files into there and it does the lot.
Many Thanks
Kez3
Fair enough!  How strange that it can be done like that, but not directly onto the VBS.  The GUI must do something else......maybe pass single files behind the scenese.....

Anyway, well done.

Rob.
heh, i even knew that,.. never tried 200, but quite a few.

thanks