Resource

Resource Basics

Resource Name Read Metadata for Files in Text file
Resource Version 1.0
Category Catalog
Downloads 1128
Release Date 1/23/2018 6:00:04 PM
 

Resource Details

Description This script will take a text file as input and for every file in the text file (one file name per line) it will read the metadata from the file to the catalog.
Installation Instructions 1. Download the script file
2. Store the script file on your hard drive
3. Open Tools->Scripter
4. Load the downloaded script
5. Run the script
Usage Instructions Prepare the text file. It should contain a list of file names, one per line
Changes in this version n.a.
Special Requirements
 

Author Information

Author Name IDimager Support
 

Script

{
  Author: IDimager
  Initial Date: 23 jan 2018
  Version: 1.0
}
var
  ANonExistFiles: TTntStringList;
  ANonExistCatalog: TTntStringList;
  ASyncItems: TCatalogItems;

  procedure HandleFile(AFileName: WideString);
  var
    ACatItem: TCatalogItem;
    AImg: TImageItem;
  begin
    if not WideFileExists(AFileName) then
    begin
      ANonExistFiles.Add(AFileName);
      exit;
    end;

    AImg := TImageItem.Create(nil);
    ACatItem := TCatalogItem.Create(nil);

    AImg.FileName := AFileName;
    AImg.UpdateFileInfo(True);
    if PublicCatalog.FindItemCombined(AImg.FileName, AImg.MediumType, AImg.MediumName, AImg.MediumSerial, ACatItem, False, False, vptNone) then
    begin
      ACatItem.CopyTo(ASyncItems, False);
    end
    else
      ANonExistCatalog.Add(AFileName);

    ACatItem.Free;
    AImg.Free;

  end;

  procedure HandleFiles(AFileList: TTntStringList);
  var
    i: Integer;
    APro: TxomProgress;
  begin
    ANonExistFiles   := TTntStringList.Create;
    ANonExistCatalog := TTntStringList.Create;
    ASyncItems       := TCatalogItems.Create(TCatalogItem, '');

    APro := TxomProgress.Create(nil);
    APro.ProgressText := 'Preparing the import...';
    APro.Max := AFileList.Count;
    for i := 0 to AFileList.Count - 1 do
    begin
      APro.Pos := i + 1;
      HandleFile(AFileList.Strings[i]);
    end;
    APro.Free;

    if ANonExistCatalog.Count > 0 then
      Say2('These files are not handled because they don''t exist in the Catalog', ANonExistCatalog.Text);
    if ANonExistFiles.Count > 0 then
      Say2('These files are not handled because they don''t exist on the file system', ANonExistFiles.Text);

    if ASyncItems.Count > 0 then
    begin
      if AskYN('Are you sure you want to read metadata for the ' + IntToStr(ASyncItems.Count) + ' files as listed in the selected file?') then
      begin
        PublicCatalog.SynchronizeToItems (
                                   ASyncItems,
                                   PublicOptions.XMPSyncReadSettings,
                                   PublicOptions.XMPSyncWriteSettings,
                                   False,
                                   True,
                                   5,
                                   True,
                                   scForcedRead
                                 );
      end;
    end;

    ASyncItems.Free;
    ANonExistCatalog.Free;
    ANonExistFiles.Free;
  end;

var
  AFileName: WideString;
  ASl: TTntStringList;
begin
  OpenDialog.Filter := 'All Text Files|*.txt|All CSV Files|*.csv|All Files|*.*';
  if OpenDialog.Execute then
  begin
    ASl := TTntStringList.Create;
    ASl.LoadFromFile(OpenDialog.FileName);

    if ASl.Count > 0 then
    begin
      HandleFiles(ASl);
    end;

    ASl.Free;
  end;
  OpenDialog.Filter := '';
end;