Product DocsMenu

Sample 1 - Add New Document Metadata

Preconversion Sample #1 (JScript Version)

// ****************************************************************************
//  This preconversion script sample adds two new document metadata:
//    fileSize - The number of bytes of the file to convert.
//    fileName - The name of the file to convert.
//****************************************************************************

// Add the "fileSize" metadata.
DocumentInfo.SetFieldValue("fileSize", PreConversion.InputDocument.BytesCount);

// Extract and add the "fileName" metadata.
var documentFileName = GetFileName(DocumentInfo.URI);
DocumentInfo.SetFieldValue("fileName", documentFileName);

// Output information to the CES console.
PreConversion.Trace("The file '" + documentFileName + "' has " + PreConversion.InputDocument.BytesCount.toString() + " bytes.", 1);

// This method extracts the file name from a given URI.
function GetFileName(URI)
{
    var fileName = URI;
    var pos = fileName.lastIndexOf("/");
    if (pos == -1) {
        pos = fileName.lastIndexOf("\\");
    }
    if (pos != -1) {
        fileName = fileName.substring(pos + 1, fileName.length);
    }
    pos = fileName.indexOf("?")
    if (pos != -1) {
        fileName = fileName.substring(0, pos);
    }
    return fileName;
}

Preconversion Sample #1 (VBScript Version)

'****************************************************************************
' This preconversion script sample adds two new document metadata:
'   fileSize - The number of bytes of the file to convert.
'   fileName - The name of the file to convert.
'****************************************************************************
 
Option Explicit

' Add the "fileSize" metadata.
DocumentInfo.SetFieldValue "fileSize", PreConversion.InputDocument.BytesCount

' Extract and add the "fileName" metadata.
Dim fileName: fileName = GetFileName(DocumentInfo.URI)
DocumentInfo.SetFieldValue "fileName", fileName

' Output information to the CES console.
Call PreConversion.Trace("The file '" & fileName & "' has " & CStr(PreConversion.InputDocument.BytesCount) & " bytes.", 1)

' This method extracts the file name from a given URI.
Function GetFileName(ByVal URI)
    Dim pos: pos = InStrRev(URI, "/")
    If pos = 0 Then
        pos = InStrRev(URI, "\")
    End If
    If pos = 0 Then
        GetFileName = URI
    Else
        GetFileName = Right(URI, Len(URI) - pos)
    End If
    pos = InStr(GetFileName, "?")
    If pos > 1 Then
        GetFileName = Left(GetFileName, pos - 1)
    End If
End Function

See also: What are Preconversion Scripts?, How to Create a Preconversion Script, How to Setup a Preconversion ScriptPreconversion Script Samples