Unit Unit1;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls,filectrl,shellapi;
Type
TForm1 = Class (Tform)
Button1:tbutton;
Memo1:tmemo;
Procedure Button1Click (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
End
Var
Form1:tform1;
Countd,countf:integer;
Sum:integer;
Implementation
{$R *.DFM}
Calling method: Shellfileoperation (' c:/afile.txt ', ' d:/afile2.txt ', fo_copy); Fo_move
Procedure Shellfileoperation (fromfile:string; tofile:string; Flags:integer);
Var
Shellinfo:tshfileopstructa;
Begin
With Shellinfo do
Begin
WND: = Application.handle;
Wfunc: = Flags;
Pfrom: = PChar (FromFile);
PTo: = PChar (ToFile);
End
SHFileOperation (Shellinfo);
End
Example, Beispiel:
function rendirectory (const oldname,newname:string): Boolean;
Var
Fo:tshfileopstruct;
Begin
Fillchar (FO, SizeOf (FO), 0);
With Fo do
Begin
Wnd: = 0;
Wfunc: = Fo_rename;
Pfrom: = PChar (oldname+ #0);
PTo: = Pchar (newname+ #0);
Fflags: = fof_noconfirmation+fof_silent;
End
Result: = (shfileoperation (FO) = 0);
End
Procedure search (dir:string);
Var
targetpath:string; {Destination path name}
Sr:tsearchrec;
Begin
{First stage: Find all the files in the initial dir directory,
Where the dir variable value is determined by the Text property of Edit1}
Targetpath:=extractfilepath (dir); {Break out destination path name}
If FindFirst (DIR,FAANYFILE,SR) =0 Then
Repeat
if ((sr.name<> '. ') and (sr.name<> ' ... ') {Exclude parent directory and this directory two fake files}
and ((Filegetattr (targetpath+sr.name) and fadirectory) <>fadirectory) {file only}
Then
Begin
sum:=sum+1;
RenameFile (Targetpath+sr.name, Targetpath+inttostr (sum));
countf:=countf+1;
End
Until FindNext (SR) <>0;
If FindFirst (DIR,FAANYFILE,SR) =0 Then
Repeat
if ((sr.name<> '. ') and (sr.name<> ' ... ')) {Exclude parent directory and this directory two fake files}
and ((Filegetattr (targetpath+sr.name) and fadirectory) =fadirectory) {Exclude file}
Then
Begin
sum:=sum+1;
Rendirectory (Targetpath+sr.name, Targetpath+inttostr (sum));
Search (TARGETPATH+INTTOSTR (sum) + '/*.* '); {Recursive Call}
Search (targetpath+sr.name+ '/*.* '); {Recursive Call}
countd:=countd+1;
End
Until FindNext (SR) <>0;
End
Procedure Tform1.button1click (Sender:tobject);
Var
curpath,s:string;
Dt:tdatetime;
fdt:double;
Begin
S: = ';
If Selectdirectory (' Select folder to modify name ', ' ', S) then
Begin
countf:=0;
countd:=0;
Randomize
sum:= 10000+random (1000000-10000+1);
Search (s); {Call the search () function}
ShowMessage (' Total change directory: ' +inttostr (COUNTD) + ', File: ' +inttostr (countf) + ')
End
End
End.
RenameFile (' oldname ', ' Newname ');
CopyFile (PChar (' oldname '), PChar (' Newname '), False);
MoveFile (PChar (' oldname '), PChar (' Newname '));
Delphi modifies the folder name and file name