gpt4 book ai didi

delphi - 增加文件名的函数

转载 作者:行者123 更新时间:2023-12-03 15:49:57 25 4
gpt4 key购买 nike

我正在尝试创建一个可以增加文件名的函数。如果字符串的最后一个字符是数字,则将其递增。如果最后一个字符是字母,则添加 _1 或 _2 或 _3(也递增)。我必须确保文件名是唯一的,但我不能在文件名中使用日期时间,因为所有文件名必须为 <32 个字符,无扩展名。

EX: Apple_99.txt =>Apple_100
Ex: Apple_173 => Apple_174
EX: This_is_my_first_text.txt => This_is_my_first_text_1.txt
Ex: This_is_my_first_text_9.txt => This_is_my_first_text_10.txt

我需要使用它来重命名文件,然后将其上传到 ftp 服务器。我找到了一个可以执行类似操作的函数,但它仅在文件名仅包含大写字母时才有效。如何修改此函数以访问小写字母和大写字母字符串?

这是函数:

    function IncStr(Str: String; Amount: Integer; Index: Integer = -1): String;
const
MIN_VAL = 65; // 'A'
MAX_VAL = 90; // 'Z'
var
Digit, ToAdd, ToCarry: Integer;
begin

if (Index = 0) and (Amount > 0) then
begin
Result := Char(MIN_VAL + Amount - 1) + Str;
Exit;
end;

if Index = -1 then Index := Length(Str);

ToCarry := 0;

Digit := Ord(Str[Index]);

while not (Digit in [MIN_VAL..MAX_VAL]) do
begin
Dec(Index);
Digit := Ord(Str[Index]);
end;

ToAdd := Digit + Amount;

while (ToAdd > MAX_VAL) do
begin
Dec(ToAdd, 26);
Inc(ToCarry);
end;

Result := Str;
Result[Index] := Char(ToAdd);

if (ToCarry > 0) then
Result := IncStr(Result, ToCarry, Index - 1);

end;


procedure TForm1.Button1Click(Sender: TObject);
var
S: String; // holds string to increment
C: Integer; // amount to increment by
begin
// make sure that Edit1 starts with a valid character
// i.e. 'A' to 'Z'
S := Edit1.Text;
C := StrtoIntDef(Edit2.Text, 0);
// test it, place result in Edit3
Edit3.Text := IncStr(S, C);
{
Example data:

Edit1 := AAZ
Edit2 := 2
= Edit3 := ABB

Edit1 := BZY
Edit2 := 3
= Edit3 := CAB

Edit1 := ZZZ
Edit2 := 1
= Edit3 := AAAA

Edit1 := AA-AC
Edit2 := 3
= Edit3 := AA-AF

Edit1 := AA/Z
Edit2 := 5
= Edit3 := AB/E

... etc

Here's one to try too :-)
Edit1 := ZZZ
Edit2 := 264172
}
end;

谢谢!

最佳答案

就像许多编程问题一样,关键是将问题分解成小块。首先,让我们编写一个函数来将原始文件名解码为其组成部分:

procedure DecodeFileName(const Input: string; out Stem, Ext: string; out Number: Integer);
var
P: Integer;
begin
Ext := TPath.GetExtension(Input);
Stem := TPath.GetFileNameWithoutExtension(Input);
Number := 0;

P := Stem.LastIndexOf('_');
if P = -1 then begin
exit;
end;

if TryStrToInt(Stem.Substring(P+1), Number) then begin
Stem := Stem.Substring(0, P);
end;
end;

以下演示了其工作原理:

DecodeFileName('test.txt', Stem, Ext, Number);
Writeln(Stem, ', ', Number, ', ', Ext);

DecodeFileName('test_dd.txt', Stem, Ext, Number);
Writeln(Stem, ', ', Number, ', ', Ext);

DecodeFileName('test_23.txt', Stem, Ext, Number);
Writeln(Stem, ', ', Number, ', ', Ext);

输出为:

test, 0, .txttest_dd, 0, .txttest, 23, .txt

So now you can make a new filename like this:

function IncrementedFileName(const FileName: string): string;
var
Stem, Ext: string;
Number: Integer;
begin
DecodeFileName(FileName, Stem, Ext, Number);
Result := Format('%s_%d%s', [Stem, Number+1, Ext]);
end;

然后我们可以看看它是如何执行的:

Writeln(IncrementedFileName('test.txt'));
Writeln(IncrementedFileName('test_dd.txt'));
Writeln(IncrementedFileName('test_23.txt'));
Writeln(IncrementedFileName('test_28'));

输出为:

test_1.txttest_dd_1.txttest_24.txttest_29

If you don't have access to the string helper methods then you can code it like this:

procedure DecodeFileName(const Input: string; out Stem, Ext: string; out Number: Integer);
var
P: Integer;
begin
Ext := TPath.GetExtension(Input);
Stem := TPath.GetFileNameWithoutExtension(Input);
Number := 0;

P := LastDelimiter('_', Stem);
if P = 0 then begin
exit;
end;

if TryStrToInt(Copy(Stem, P+1, MaxInt), Number) then begin
Stem := Copy(Stem, 1, P-1);
end;
end;

我还没有执行这个最终函数,所以如果它有错误,请不要感到惊讶。

关于delphi - 增加文件名的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28220015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com