{
  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;
