批量改名的问题!急!急!急!(100分)

  • 主题发起人 bianconeri
  • 开始时间
B

bianconeri

Unregistered / Unconfirmed
GUEST, unregistred user!
源程序如下,是从一个文本文件中读取相应的字符串作为新文件名,现在可以给第一个文件重命名(在我的机器上面已经可以,但是不知道怎么在别人机器上不行,这个现在不是主要问题),主要是不知道应该怎么使用findnext来给其他文件重命名。不用考虑子目录,程序和文本在同一目录下。暂时只需使用指定目录,不用太麻烦。
同时这样改名连扩展名也一起改了,有没有办法不改文件的扩展名?

procedure TForm1.Button1Click(Sender: TObject);
var
i, Counter,iBegin, iEnd, iLong: integer;
S, Tempname1,Tempname2: String;
List: TStringList;
SR:Tsearchrec;

begin
if findfirst('E:/*.*',faArchive,SR)=0 then
Tempname1:=ExtractFilename(SR.name);
List := TStringList.Create;
List.LoadFromFile('test.txt');
for Counter := 0 to List.Count - 1 do
begin
S := List[Counter];
i := Pos(Tempname1, S);
if i <> 0 then
begin
iBegin := Pos('[', S);
iEnd := Pos(']', S);
iLong := iEnd - iBegin;
tempname2:= Copy(S, iBegin + 1, iLong - 1);
end;
end;
renamefile(tempname1,tempname2);
List.Free;
 
你可以用ExtractFileExt获得原来文件的扩展名
 
扩展名可以用上面地方法
为使它们能够工作,我们需要一个TSearchRec记录,其中含有文件搜索条件信息。我们使用像下面的代码:
var
SearchRec: TSearchRec;
begin
if FindFirst('C:/WINDOWS/*.INI', attributes, SearchRec) = 0 then
begin
repeat
// 这里我们处理找到的每一个文件。
// 它的消息在SearchRec中。
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
end;
end;
 
以下是FindFirst FindNext 的用法,要用循环:
procedure TForm1.Button1Click(Sender: TObject);

var
sr: TSearchRec;
FileAttrs: Integer;
begin
StringGrid1.RowCount := 1;
if CheckBox1.Checked then
FileAttrs := faReadOnly
else
FileAttrs := 0;
if CheckBox2.Checked then
FileAttrs := FileAttrs + faHidden;
if CheckBox3.Checked then
FileAttrs := FileAttrs + faSysFile;
if CheckBox4.Checked then
FileAttrs := FileAttrs + faVolumeID;
if CheckBox5.Checked then

FileAttrs := FileAttrs + faDirectory;
if CheckBox6.Checked then
FileAttrs := FileAttrs + faArchive;
if CheckBox7.Checked then

FileAttrs := FileAttrs + faAnyFile;

if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then

begin
with StringGrid1 do
begin
if (sr.Attr and FileAttrs) = sr.Attr then
begin
Cells[1,RowCount-1] := sr.Name;
Cells[2,RowCount-1] := IntToStr(sr.Size);
end;
while FindNext(sr) = 0 do
begin
if (sr.Attr and FileAttrs) = sr.Attr then
begin
RowCount := RowCount + 1;
Cells[1, RowCount-1] := sr.Name;

Cells[2, RowCount-1] := IntToStr(sr.Size);
end;
end;
FindClose(sr);
end;
end;
end;

要仅改文件名,可以分别提取文件名和扩展名:
ExtractFileName
ExtractFileExt

改名时要考虑和目录中的其它文件重名的问题!!
 
接受答案了!
 
顶部