0

我有以下带参数的函数

aFile = 完整的文件名

aFolder = 要复制/移动到的文件夹名称

aGuid = 分配给文档的 guid

aAction = 如何处理文件(移动或复制)

如果旧文件与新文件相同,我猜如果 Trim(NewFile) = Trim(aFile) then Exit 应该停止代码执行任何操作。但事实并非如此。即使文件相同,也会执行 if FileExists(NewFile) 的行。

在我的调试日志中,我有

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

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

据我所知,这些名字是一样的

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

3 回答 3

5

比较是区分大小写的,您的文件名indboIndbo文件名中的比较。你可以比较例如

UpperCase(f1)=UpperCase(f2)

或者

if SameText(f1,f2) then ...
于 2013-05-30T06:38:12.403 回答
2

除了比较可能导致误报的字符串之外,您还可以使用SHParseDisplayName()or将文件路径转换为 ​​PIDL IShellFolder.ParseDisplayName(),然后使用IShellFolder.CompareIDs(). 这将使您不仅可以比较混合大小写的文件,还可以比较短文件名和长文件名等。

于 2013-05-30T07:48:09.570 回答
0

看起来您在有意义的部分之后将垃圾数据保留在宽字符串中,您可以在两个字符串上尝试 Length(aMessage) 并找出长度是否相同..

于 2013-05-30T06:41:20.750 回答