Delphi modifies the folder name and file name

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.