TCSCode
asked on
How do i rename mutiply files with rename or renamefile?
How do i rename mutiply files with rename or renamefile, I want to rename about 200 jpg files by just renameing the first part of the file to 0001.jpg 0002.jpg by clicking on 1 button.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Adjusted points to 100
Hi TCDCode
Boy, I see, did I make mistakes.... ok here comes a tested one :
procedure TForm1.Button1Click(Sender : TObject);
var
SearchRec: TSearchRec;
ErrorCode : integer;
Number : Integer;
NewName : String;
PATH : string;
begin
Number := 0;
PATH := 'c:\test\';
showmessage('starting');
ErrorCode := FindFirst(PATH + '*.jpg', faAnyFile, SearchRec);
NewName := IntToStr(Number)+'.jpg';
If ErrorCode = 0 then RenameFile(PATH+SearchRec. Name, PATH+NewName)
else showmessage('no jpg-files or error');
While (FindNext(SearchRec) = 0) do
begin
Inc(Number);
NewName := IntToStr(Number)+'.jpg';
RenameFile(PATH+SearchRec. Name, PATH+NewName);
end;
FindClose(SearchRec);
showmessage('ended');
end;
Regards, Zif.
Boy, I see, did I make mistakes.... ok here comes a tested one :
procedure TForm1.Button1Click(Sender
var
SearchRec: TSearchRec;
ErrorCode : integer;
Number : Integer;
NewName : String;
PATH : string;
begin
Number := 0;
PATH := 'c:\test\';
showmessage('starting');
ErrorCode := FindFirst(PATH + '*.jpg', faAnyFile, SearchRec);
NewName := IntToStr(Number)+'.jpg';
If ErrorCode = 0 then RenameFile(PATH+SearchRec.
else showmessage('no jpg-files or error');
While (FindNext(SearchRec) = 0) do
begin
Inc(Number);
NewName := IntToStr(Number)+'.jpg';
RenameFile(PATH+SearchRec.
end;
FindClose(SearchRec);
showmessage('ended');
end;
Regards, Zif.
The code is missing two dots ("."):
NewName := IntToStr(Number)+'jpg';
should be:
NewName := IntToStr(Number)+'.jpg';
The code isn't very foolproof since if there's already a file with a name like 5.jpg, the fifth file won't be renamed. Then sorting the files won't be very beautiful, so...:
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMainForm = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
Filenames, NumericFilenames: TStringList;
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
sDirectory: string = 'x:\web_download\images\te st';
implementation
{$R *.DFM}
function IntToPaddedStr(iValue, iDigits: Integer): string;
var
iIndex: Integer;
sTemp : string;
begin
Result := '';
sTemp := IntToStr(iValue);
for iIndex := 1 to iDigits - Length(sTemp) do
AppendStr(Result, '0');
AppendStr(Result, sTemp);
end;
procedure PopulateFileList(sPath, sMask: string; list: TStringList);
var
sr: TSearchRec;
begin
if not Assigned(list) then
Exit;
list.BeginUpdate;
list.Clear;
if sPath[Length(sPath)]<>'\' then
AppendStr(sPath, '\');
if FindFirst(sPath+sMask, faAnyFile, sr) = 0 then
repeat
with sr do
if Attr and (faDirectory or faVolumeID) = 0 then
list.Add(sPath+Name);
until FindNext(sr)<>0;
FindClose(sr);
list.EndUpdate;
end;
procedure HandleNumericFilenames(lis t, numlist: TStringList);
var
iIndex, iValue, iCount, iDigits, iErrorPos: Integer;
sPath, sName, sExt: string;
begin
if not Assigned(list) then
Exit;
if not Assigned(numlist) then
Exit;
list.BeginUpdate;
numlist.BeginUpdate;
numlist.Clear;
iCount := list.Count;
iDigits := Trunc(Ln(iCount)/Ln(10))+2 ;
for iIndex := iCount - 1 downto 0 do
begin
sName := ExtractFileName(list[iInde x]);
sExt := ExtractFileExt(sName);
SetLength(sName, Length(sName)-Length(sExt) );
Val(sName, iValue, iErrorPos);
if (iErrorPos = 0) and (iValue <= iCount) then
if (IntToPaddedStr(iValue, iDigits) = sName) then
begin
numlist.Add(list[iIndex]);
list.Delete(iIndex);
end;
end;
numlist.EndUpdate;
list.EndUpdate;
end;
procedure RenameFiles(list, numlist: TStringList);
var
iIndex, iValue, iCount, iDigits, iPos: Integer;
sFilename, sPath, sName, sExt: string;
begin
if not Assigned(list) then
Exit;
if not Assigned(numlist) then
Exit;
numlist.Sorted := True; { this is absolutely necessary for numlist.Find! }
iValue := 1;
iCount := list.Count;
if (iCount=0) then
Exit;
iDigits := Trunc(Ln(iCount)/Ln(10))+2 ;
for iIndex := 0 to iCount - 1 do
begin
sPath := ExtractFilePath(list[iInde x]);
sExt := ExtractFileExt(list[iIndex ]);
repeat
sFilename := sPath + IntToPaddedStr(iValue, iDigits) + sExt;
Inc(iValue);
until not numlist.Find(sFilename,iPo s);
RenameFile(list[iIndex], sFilename); { no error handling, this is up to you... }
end;
end;
procedure TMainForm.Button1Click(Sen der: TObject);
begin
try
Filenames := TStringList.Create;
NumericFilenames := TStringList.Create;
PopulateFileList(sDirector y, '*.jpg', Filenames);
HandleNumericFilenames(Fil enames, NumericFilenames);
RenameFiles(Filenames, NumericFilenames);
finally
NumericFilenames.Free;
Filenames.Free;
end;
end;
end.
This code is from a tool I made a while back. It's a little bit complicated, but works better, is much faster the other time around and doesn't re-index already indexed files.
/// John
NewName := IntToStr(Number)+'jpg';
should be:
NewName := IntToStr(Number)+'.jpg';
The code isn't very foolproof since if there's already a file with a name like 5.jpg, the fifth file won't be renamed. Then sorting the files won't be very beautiful, so...:
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMainForm = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
Filenames, NumericFilenames: TStringList;
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
sDirectory: string = 'x:\web_download\images\te
implementation
{$R *.DFM}
function IntToPaddedStr(iValue, iDigits: Integer): string;
var
iIndex: Integer;
sTemp : string;
begin
Result := '';
sTemp := IntToStr(iValue);
for iIndex := 1 to iDigits - Length(sTemp) do
AppendStr(Result, '0');
AppendStr(Result, sTemp);
end;
procedure PopulateFileList(sPath, sMask: string; list: TStringList);
var
sr: TSearchRec;
begin
if not Assigned(list) then
Exit;
list.BeginUpdate;
list.Clear;
if sPath[Length(sPath)]<>'\' then
AppendStr(sPath, '\');
if FindFirst(sPath+sMask, faAnyFile, sr) = 0 then
repeat
with sr do
if Attr and (faDirectory or faVolumeID) = 0 then
list.Add(sPath+Name);
until FindNext(sr)<>0;
FindClose(sr);
list.EndUpdate;
end;
procedure HandleNumericFilenames(lis
var
iIndex, iValue, iCount, iDigits, iErrorPos: Integer;
sPath, sName, sExt: string;
begin
if not Assigned(list) then
Exit;
if not Assigned(numlist) then
Exit;
list.BeginUpdate;
numlist.BeginUpdate;
numlist.Clear;
iCount := list.Count;
iDigits := Trunc(Ln(iCount)/Ln(10))+2
for iIndex := iCount - 1 downto 0 do
begin
sName := ExtractFileName(list[iInde
sExt := ExtractFileExt(sName);
SetLength(sName, Length(sName)-Length(sExt)
Val(sName, iValue, iErrorPos);
if (iErrorPos = 0) and (iValue <= iCount) then
if (IntToPaddedStr(iValue, iDigits) = sName) then
begin
numlist.Add(list[iIndex]);
list.Delete(iIndex);
end;
end;
numlist.EndUpdate;
list.EndUpdate;
end;
procedure RenameFiles(list, numlist: TStringList);
var
iIndex, iValue, iCount, iDigits, iPos: Integer;
sFilename, sPath, sName, sExt: string;
begin
if not Assigned(list) then
Exit;
if not Assigned(numlist) then
Exit;
numlist.Sorted := True; { this is absolutely necessary for numlist.Find! }
iValue := 1;
iCount := list.Count;
if (iCount=0) then
Exit;
iDigits := Trunc(Ln(iCount)/Ln(10))+2
for iIndex := 0 to iCount - 1 do
begin
sPath := ExtractFilePath(list[iInde
sExt := ExtractFileExt(list[iIndex
repeat
sFilename := sPath + IntToPaddedStr(iValue, iDigits) + sExt;
Inc(iValue);
until not numlist.Find(sFilename,iPo
RenameFile(list[iIndex], sFilename); { no error handling, this is up to you... }
end;
end;
procedure TMainForm.Button1Click(Sen
begin
try
Filenames := TStringList.Create;
NumericFilenames := TStringList.Create;
PopulateFileList(sDirector
HandleNumericFilenames(Fil
RenameFiles(Filenames, NumericFilenames);
finally
NumericFilenames.Free;
Filenames.Free;
end;
end;
end.
This code is from a tool I made a while back. It's a little bit complicated, but works better, is much faster the other time around and doesn't re-index already indexed files.
/// John
There's a bad bug in the procedure HandleNumericFilenames:
iCount := list.Count;
iDigits := Trunc(Ln(iCount)/Ln(10))+2 ;
...should be:
iCount := list.Count;
if (iCount>0) then
iDigits := Trunc(Ln(iCount)/Ln(10))+2 ;
Otherwise the app will dive if there are no JPEGs in the folder.
Sorry!
/// John
iCount := list.Count;
iDigits := Trunc(Ln(iCount)/Ln(10))+2
...should be:
iCount := list.Count;
if (iCount>0) then
iDigits := Trunc(Ln(iCount)/Ln(10))+2
Otherwise the app will dive if there are no JPEGs in the folder.
Sorry!
/// John
Man! The code I sent you was just a piece of crap!
Here's a decrappitated version (still not perfect, but hey, nobody is):
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMainForm = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
Filenames, NumericFilenames: TStringList;
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
iDigits: Integer; { changed here! }
sDirectory: string = 'x:\web_download\images\te st';
implementation
{$R *.DFM}
function IntToPaddedStr(iValue, iDigits: Integer): string;
var
iIndex: Integer;
sTemp : string;
begin
Result := '';
sTemp := IntToStr(iValue);
for iIndex := 1 to iDigits - Length(sTemp) do
AppendStr(Result, '0');
AppendStr(Result, sTemp);
end;
procedure PopulateFileList(sPath, sMask: string; list: TStringList);
var
sr: TSearchRec;
begin
if not Assigned(list) then
Exit;
list.BeginUpdate;
list.Clear;
if sPath[Length(sPath)]<>'\' then
AppendStr(sPath, '\');
if FindFirst(sPath+sMask, faAnyFile, sr) = 0 then
repeat
with sr do
if Attr and (faDirectory or faVolumeID) = 0 then
list.Add(sPath+Name);
until FindNext(sr)<>0;
FindClose(sr);
list.EndUpdate;
end;
procedure HandleNumericFilenames(lis t, numlist: TStringList);
var
iIndex, iValue, iCount, iErrorPos: Integer;
sPath, sName, sExt: string;
begin
if not Assigned(list) then
Exit;
if not Assigned(numlist) then
Exit;
list.BeginUpdate;
numlist.BeginUpdate;
numlist.Clear;
iCount := list.Count;
{ changed here! }
for iIndex := iCount - 1 downto 0 do
begin
sName := ExtractFileName(list[iInde x]);
sExt := ExtractFileExt(sName);
SetLength(sName, Length(sName)-Length(sExt) );
Val(sName, iValue, iErrorPos);
if (iErrorPos = 0) and (iValue <= iCount) then
if (IntToPaddedStr(iValue, iDigits) = sName) then
begin
numlist.Add(list[iIndex]);
list.Delete(iIndex);
end;
end;
numlist.EndUpdate;
list.EndUpdate;
end;
procedure RenameFiles(list, numlist: TStringList);
var
iIndex, iValue, iCount, iPos: Integer;
sFilename, sPath, sName, sExt: string;
begin
if not Assigned(list) then
Exit;
if not Assigned(numlist) then
Exit;
numlist.Sorted := True; { this is absolutely necessary for numlist.Find! }
iValue := 1;
iCount := list.Count;
{ changed here! }
for iIndex := 0 to iCount - 1 do
begin
sPath := ExtractFilePath(list[iInde x]);
sExt := ExtractFileExt(list[iIndex ]);
repeat
sFilename := sPath + IntToPaddedStr(iValue, iDigits) + sExt;
Inc(iValue);
until not numlist.Find(sFilename,iPo s);
RenameFile(list[iIndex], sFilename); { no error handling, this is up to you... }
end;
end;
procedure TMainForm.Button1Click(Sen der: TObject);
begin
try
Filenames := TStringList.Create;
NumericFilenames := TStringList.Create;
PopulateFileList(sDirector y, '*.jpg', Filenames);
if (Filenames.Count>0) then { changed here! }
begin
iDigits := Trunc(Ln(Filenames.Count)/ Ln(10))+2;
HandleNumericFilenames(Fil enames, NumericFilenames);
RenameFiles(Filenames, NumericFilenames);
end;
finally
NumericFilenames.Free;
Filenames.Free;
end;
end;
end.
I guess it's time for me to go home...
/// John
Here's a decrappitated version (still not perfect, but hey, nobody is):
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMainForm = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
Filenames, NumericFilenames: TStringList;
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
iDigits: Integer; { changed here! }
sDirectory: string = 'x:\web_download\images\te
implementation
{$R *.DFM}
function IntToPaddedStr(iValue, iDigits: Integer): string;
var
iIndex: Integer;
sTemp : string;
begin
Result := '';
sTemp := IntToStr(iValue);
for iIndex := 1 to iDigits - Length(sTemp) do
AppendStr(Result, '0');
AppendStr(Result, sTemp);
end;
procedure PopulateFileList(sPath, sMask: string; list: TStringList);
var
sr: TSearchRec;
begin
if not Assigned(list) then
Exit;
list.BeginUpdate;
list.Clear;
if sPath[Length(sPath)]<>'\' then
AppendStr(sPath, '\');
if FindFirst(sPath+sMask, faAnyFile, sr) = 0 then
repeat
with sr do
if Attr and (faDirectory or faVolumeID) = 0 then
list.Add(sPath+Name);
until FindNext(sr)<>0;
FindClose(sr);
list.EndUpdate;
end;
procedure HandleNumericFilenames(lis
var
iIndex, iValue, iCount, iErrorPos: Integer;
sPath, sName, sExt: string;
begin
if not Assigned(list) then
Exit;
if not Assigned(numlist) then
Exit;
list.BeginUpdate;
numlist.BeginUpdate;
numlist.Clear;
iCount := list.Count;
{ changed here! }
for iIndex := iCount - 1 downto 0 do
begin
sName := ExtractFileName(list[iInde
sExt := ExtractFileExt(sName);
SetLength(sName, Length(sName)-Length(sExt)
Val(sName, iValue, iErrorPos);
if (iErrorPos = 0) and (iValue <= iCount) then
if (IntToPaddedStr(iValue, iDigits) = sName) then
begin
numlist.Add(list[iIndex]);
list.Delete(iIndex);
end;
end;
numlist.EndUpdate;
list.EndUpdate;
end;
procedure RenameFiles(list, numlist: TStringList);
var
iIndex, iValue, iCount, iPos: Integer;
sFilename, sPath, sName, sExt: string;
begin
if not Assigned(list) then
Exit;
if not Assigned(numlist) then
Exit;
numlist.Sorted := True; { this is absolutely necessary for numlist.Find! }
iValue := 1;
iCount := list.Count;
{ changed here! }
for iIndex := 0 to iCount - 1 do
begin
sPath := ExtractFilePath(list[iInde
sExt := ExtractFileExt(list[iIndex
repeat
sFilename := sPath + IntToPaddedStr(iValue, iDigits) + sExt;
Inc(iValue);
until not numlist.Find(sFilename,iPo
RenameFile(list[iIndex], sFilename); { no error handling, this is up to you... }
end;
end;
procedure TMainForm.Button1Click(Sen
begin
try
Filenames := TStringList.Create;
NumericFilenames := TStringList.Create;
PopulateFileList(sDirector
if (Filenames.Count>0) then { changed here! }
begin
iDigits := Trunc(Ln(Filenames.Count)/
HandleNumericFilenames(Fil
RenameFiles(Filenames, NumericFilenames);
end;
finally
NumericFilenames.Free;
Filenames.Free;
end;
end;
end.
I guess it's time for me to go home...
/// John
ASKER
Thank's Alot ZifNab And erajoj.
ASKER
Thank's Alot ZifNab And erajoj.
ASKER