Browse Source

AutoChangeFileName增加两个参数

KngStr 1 year ago
parent
commit
c7bc70b2d1
1 changed files with 16 additions and 6 deletions
  1. 16 6
      Source/ksString.pas

+ 16 - 6
Source/ksString.pas

@@ -2,7 +2,7 @@
 {                                                       }
 {       Methods of Sting                                }
 {                                                       }
-{       CopyRight (C) 2018-2020 KngStr                  }
+{       CopyRight (C) 2018-2023 KngStr                  }
 {                                                       }
 {   Some Code from                                      }
 {     QDAC of swish, Inno                               }
@@ -21,7 +21,7 @@ function CreateBlankFile(S: string): Boolean;
 /// <summary>
 /// 自动为已存在文件名加数字
 /// </summary>
-function AutoChangeFileName(AFile: string): string;
+function AutoChangeFileName(AFile: string; AHaveExt: Boolean = True; AFormat: string = '(%d)'): string;
 /// <summary>
 /// 自动替换文件名中的非法字符为
 /// </summary>
@@ -138,18 +138,28 @@ begin
   end;
 end;
 
-function AutoChangeFileName(AFile: string): string;
+function AutoChangeFileName(AFile: string; AHaveExt: Boolean; AFormat: string): string;
 var
   I: Integer;
   sPath, sExt: string;
 begin
   Result := AFile;
+
+  if AHaveExt then begin
+    sPath := ChangeFileExt(Result, '');
+    sExt := ExtractFileExt(Result);
+  end
+  else begin
+    sPath := Result;
+    sExt := '';
+  end;
+
+  // 注意:目录名不能写死到format里
+  // 因为什么文件名都有,避免引起格式化错误
   I := 0;
-  sPath := ChangeFileExt(Result, '');
-  sExt := ExtractFileExt(Result);
   while FileExists(Result) or DirectoryExists(Result) do begin
     Inc(I);
-    Result := Format('%s(%d)%s', [sPath, I, sExt]);
+    Result := Format('%s%s%s', [sPath, Format(AFormat, [I]), sExt]);
   end;
 end;