OZ8HP
OZ8HP

Reputation: 1513

Why is this compare failing

I have the following function with parameters

aFile = a full filename

aFolder = a foldername to copy/move to

aGuid = the guid that the document is assigned

aAction = what to do with the fil (move or copy)

I would guess the line if Trim(NewFile) = Trim(aFile) then Exit should stop the code from doing anything if the old file is the same as the new. But it doesn't. The line if FileExists(NewFile) is executed even if the files are the same.

In my debug log I have

30-05-2013 08:10:34:840 # New file: C:_Delphi_Compiled\HomeSuite\Debug\indbo\computerfladskaerm\968ED02C-21B5-4582-8A49-8463E01ADCB3.pdf

30-05-2013 08:10:34:841 # Old file: C:_Delphi_Compiled\HomeSuite\Debug\Indbo\computerfladskaerm\968ED02C-21B5-4582-8A49-8463E01ADCB3.pdf

and as far as I can tell these names are the same

function DocumentHandle(aFile, aFolder, aGuid: string; aAction: TDocumentAction): string;
const
  CopyMsg = 'Der findes allerede en fil med det navn!' + sLineBreak +
            'Filen omdøbes derfor til et unikt navn';
var
  NewFile: string;
begin
  Result := aFile;
  try
    NewFile := ExtractFileName(aFile);
    NewFile := aFolder + NewFile;
    if Trim(NewFile) = Trim(aFile) then
      Exit;
    if FileExists(NewFile) then
      begin
        NewFile := ExtractFileExt(aFile);
        NewFile := aFolder + CleanGuid(aGuid) + NewFile;
        MessageDlg(CopyMsg, mtWarning, [mbOk], 0);
      end;
    case aAction of
      daCopy:
        begin
          if CopyFile(PwideChar(aFile), PwideChar(NewFile), False) then
            Result := NewFile;
        end;
      daMove:
        begin
          if MoveFile(PwideChar(aFile), PwideChar(NewFile)) then
            Result := NewFile;
        end;
    end;
  except
    on E: exception do
      Logfile.Error('U_Documents.DocumentHandle: ' + E.Message);
  end;
end;

Upvotes: 0

Views: 215

Answers (3)

Remy Lebeau
Remy Lebeau

Reputation: 596186

Rather than comparing strings, which can lead to false positives, you could alternatively convert the file paths to PIDLs using SHParseDisplayName() or IShellFolder.ParseDisplayName(), and then compare those using IShellFolder.CompareIDs(). That would allow you to not only compare files of mixed cases, but also compare short vs long file names, etc.

Upvotes: 2

Jack Daniel's
Jack Daniel's

Reputation: 2613

It looks like you're keeping garbage data in your wide string after the meaningful part, can you try Length(aMessage) on both the string and find out if length is same..

Upvotes: 0

bummi
bummi

Reputation: 27377

Comparison is CaseSensitive you have indbo vs. Indbo in your filenames. You could compare e.g.

UpperCase(f1)=UpperCase(f2)

or

if SameText(f1,f2) then ...

Upvotes: 5

Related Questions