VB Script Samples

Script samples demonstrate a way to use the TextConverter component from multiple scripting environments. You can:

  • create a simple .vbs file and run it with cscript.exe or wscript.exe
  • create a simple web page with your own user interface and use JavaScript to pass data to TextConverter and run conversions
  • use TextConverter as a server component for your web application and automate it from an ASP page.

This sample creates an instance of the TextConverter object, loads a project file and runs the conversion.

Dim cnv, path, n_recs

Set cnv = CreateObject( "ConverterX.ConverterX.1" )

path = cnv.GetStartPath()

path = path & "Samples/TextConverter/Manual Mode/1 Well formed input/1.1 Products.ConverterX"

cnv.OpenProject( path )

n_recs = cnv.Convert()

MsgBox n_recs & " records were converted successfully.", vbOKOnly, "TextConverter Sample"

The following sample script creates the TextConverter object, sets the output database table, sets several conversion options, loads an input text file and runs the process.

Option Explicit
Dim cnv, path, f, fc, err, erp, dsp, numFiles, src, ok, filter
Dim fso : Set fso = CreateObject( "Scripting.FileSystemObject" )
Dim args : set args = WScript.Arguments
'If args.Count = 0 Then
'elseIf args.Item(0) <> tag32b Then TryRerun32
'WScript.Echo args.Item(0)
'WScript.Quit 0

path = fso.GetParentFolderName(WScript.ScriptFullName) & "\" '- Setting path variable to carrent folder
Set cnv = CreateObject( "ConverterX.ConverterX.1" )         '- Creating TextConverter automation object

src = path & "Data\"        '- Folder containing input files
dsp = path & "Processed\"   '- Folder where successfuly processed input files are moved (create automaticaly)
erp = path & "Errors\"      '- Folder where unsuccessfuly processed input files are moved (create automaticaly)
filter = ".pdf"

numFiles = ScanFolders( fso.GetFolder( src ), False ) '- Counting file

ok = MsgBox( "File to Process: " & numFiles & ". Would you like to start processing?", VbOkCancel)
If ok = VbOk Then

cnv.OpenProject( path & "Run.converterx" )                   '- Openning TextConverter project
cnv.append = False                                           '- Setting TextConverter to re-create output file on run
numFiles = ScanFolders( fso.GetFolder( src ), True ) 'Processing input folder by TextConverter project for each input file

MsgBox "Done: " & numFiles
End If

Function ScanFolders( folder, act )
Dim sfs, sf, fls, nf, fl, dst
Set sfs = folder.SubFolders

For Each sf in sfs: nf = nf + ScanFolders(sf, act): Next '- Recursive call for processing subfolders

Set fls = folder.Files
For Each fl in fls
If InStr(LCase(fl.name), LCase(filter)) > 0 Then '- Filtering input files
If fso.FileExists(fl.path) And fl.Size > 0 Then
If act Then
cnv.LoadTextFile fl.path, false               '- Setting input file
err = cnv.Convert()                           '- Running TextConverter
cnv.append = True
If err = -1 Then                             '- Checking of the error return code
MoveFile erp, fl
'MoveFile dsp, fl
End If
End If
nf = nf + 1
End If
End If
ScanFolders = nf
End Function

Function MoveFile(fldr, ByRef fl)
Dim dst
If not fso.FolderExists(fldr) Then fso.CreateFolder(fldr)
dst = fldr & fl.name
If fso.FileExists(dst) Then fso.DeleteFile dst, true
fso.MoveFile fl.path, dst
End Function

Sub TryRerun32
if InStr( WScript.Path, "SysWOW64" ) > 0 then exit sub
Dim so, cscript32_path
Set so = WScript.CreateObject("WScript.Shell")
cscript32_path = so.ExpandEnvironmentStrings("%windir%") & "\SysWOW64\" & fso.GetFileName(WScript.FullName)
If fso.FileExists( cscript32_path ) Then
Dim cmd_line, a
cmd_line = cscript32_path & " """ & WScript.ScriptFullName & """"
for each a in args
   cmd_line = cmd_line & " " & a
so.Run cmd_line, 1, TRUE
WScript.Quit 0
End if
End Sub

See also:

Working with API
API Methods and Properties