Retrieving metadata from a BarTender document Follow

Avatar
Samantha Petro

Overview

The metadata of a .btw file is in human-readable plain text in XML format. If you open a .btw file in Notepad or other text editors, you can read the header and metadata from the file. But how do you read it in an SDK application or via PowerShell?

Applicable to 

Any of the following:

  • BarTender SDK
  • PowerShell Scripts
  • PowerShell Action in Integration Builder

Information

SDK

For the SDK, you can use any standard Windows API to read the metadata, treating the .btw as a text document. There are a variety of methods you can use, so you can pick a method of your choice.

Here is an example in C# which uses System libraries:

using System;
using System.IO;
using System.Text;

namespace ReadBTWMetaData
{
   class Program
   {
      static void Main(string[] args)
      {
         StringBuilder btwHeader = new StringBuilder();         
         using (TextReader sr = File.OpenText(@"D:\temp\ZebraZT410.btw")) // BTW file to read
         {
            for (int oneChar = sr.Read(); oneChar != -1; oneChar = sr.Read())
            {               
               if (oneChar == 26) // CTRL-Z, EOF
                  break;
               else
                  btwHeader.Append((char)oneChar);
            }
         }

         string headerString = btwHeader.ToString();
         if (!string.IsNullOrEmpty(headerString))
         {
            int appStart = headerString.IndexOf("Application:");
            int metaStart = headerString.IndexOf("<Metadata>");
            int metaEnd = headerString.LastIndexOf("----------------------------------------------");

            if ((appStart > 0) && (metaStart > 0) && (metaEnd > 0))
            {
               string infoString = headerString.Substring(appStart, metaStart - appStart);
               string metaDataString = headerString.Substring(metaStart, metaEnd - metaStart);

               Console.WriteLine(infoString);
               Console.WriteLine(metaDataString);
            }
         }
      }
   }
}

Note that the .btw file is read-only. Attempting to write to the header will not change the file properties.

PowerShell

Because a .btw file can be read like a text document, you can use PowerShell to pull the metadata from the file as well. You can use a PowerShell action in Integration Builder to pull the information dynamically and return it as a response or make it available to the integration itself.

Here is an example of a PowerShell script which uses integration variables to locate the document and open it. It then converts the results to JSON for use in a response.

# Get document file properties.
$folder = "C:\Seagull\BarTender\Documents\" + "%folder%" + "\"
$document = "%document%"
$com = (New-Object -ComObject Shell.Application).NameSpace($folder)

$objDocument = New-Object -TypeName PSCustomObject
$com.Items() | ForEach-Object 
{ if ($_.path -eq $folder + $document)
{ for($i=1; $i -le 350; $i++)
{ if ((-not [string]::IsNullOrWhiteSpace($com.GetDetailsOf($_,$i)))
-and (-not [string]::IsNullOrWhiteSpace($com.GetDetailsOf($_.items,$i))))
{ $objDocument | Add-Member -MemberType NoteProperty
-Name $com.GetDetailsOf($_.items,$i) -Value $com.GetDetailsOf($_,$i) } } } } $objDocument | ConvertTo-Json

 

Internal only information

This information comes from DEVQ-4766 

Do you have feedback or questions on this article? We encourage you to post them on our Community Forums