Delphi Batch Change file name

Source: Internet
Author: User

Procedure Tformswitchfilename.btn_changeclick (Sender:tobject); Replace file name
Var
dir:string;
Filenamelist:tstringlist;
Renamefile:file;
filename:string;
I:integer;
Begin
Dir: = Trim (cxtxt_filename. Text);

Filenamelist: = tstringlist.create;
Enumfileinqueue (PChar (dir), '. * ', filenamelist);
Enumfileinqueue (PChar (dir), '. * ', filenamelist);
For I: = 0 to Filenamelist.count-1 do
Begin
FileName: = StringReplace (Trim (filenamelist.strings[i)), Trim (cxtxt1. Text), Trim (cxtxt2. Text), [Rfreplaceall, Rfignorecase]);
AssignFile (RenameFile, Trim (Filenamelist.strings[i]));//Connect the C:\MyFile.txt file with the F variable, and you can use the F variable to manipulate the file later.
Rename (renamefile, filename);//Change file name
End

Filenamelist.free;
End

Procedure Tformswitchfilename.btn_exitclick (Sender:tobject);
Begin
Close; Exit
End

Procedure Tformswitchfilename.btn_searchclick (Sender:tobject); Search for file names under this folder
Var
dir:string;
Filenamelist:tstringlist;
I:integer;
Begin
Dir: = Trim (cxtxt_filename. Text);
Filenamelist: = tstringlist.create;
Enumfileinqueue (PChar (dir), '. * ', filenamelist);
For I: = 0 to Filenamelist.count-1 do
Begin
Cxm_filename. Lines.add (Filenamelist.strings[i]);
End
Filenamelist.free;
End

Procedure Tformswitchfilename.enumfileinqueue (Path:pchar; fileext:string; filelist:tstringlist);
Var
Searchrec:tsearchrec;
Found:integer;
tmpstr:string;
curdir:string;
Dirs:tqueue;
Pszdir:pchar;
Begin
Dirs: = tqueue.create; Create a directory queue
Dirs. Push (path); Queue the start search path
Pszdir: = dirs. Pop;
CurDir: = Strpas (Pszdir); Out Team
{Start traversal until the queue is empty (that is, there is no directory to traverse)}
while (True) does
Begin
Plus search suffix, get a search path like ' c:\*.* ', ' c:\windows\*.* '
TMPSTR: = CurDir + ' \*.* ';
Find the first file, subdirectory in the current directory
Found: = FindFirst (Tmpstr, Faanyfile, Searchrec);
While found = 0 do//after finding a file or directory
Begin
If a directory is found
if (searchrec.attr and Fadirectory) <> 0 Then
Begin
{'. ' will appear when searching subdirectories under a non-root directory (c: \, D:\) '. ' The "virtual directory"
Probably represents the upper and lower directories ... To filter out to be able}
if (Searchrec.name <> ') and (Searchrec.name <> ' ... ') then
Begin
{The path to the top-level directory is added because there is only a directory name for the subdirectory found
Searchrec.name = ' Windows ';
tmpstr:= ' c:\Windows ';
Add a breakpoint and you'll be clear.
}
TMPSTR: = CurDir + ' \ ' + searchrec.name;
{Queue The directory you are searching for.} Let it hang on first.
Because the data inside the tqueue can only be pointers, the string is converted to Pchar
Also use the Strnew function to re-request a space to deposit data, otherwise you will have entered
The pointer into the queue points to nonexistent or incorrect data (TMPSTR is a local variable). }
Dirs. Push (Strnew (PChar (TMPSTR)));
End
End
else//If a file is found
Begin
{Result records the number of files searched.} But I was using CreateThread to create threads
To call the function, do not know how to get this return value ... I don't want to use global variables}
Add the found file to the memo control
If Fileext = '. * ' Then
Filelist.add (CurDir + ' \ ' + searchrec.name)
Else
Begin
If Sametext (rightstr (curDir + ' \ ' + searchrec.name, Length (Fileext)), Fileext) Then
Filelist.add (CurDir + ' \ ' + searchrec.name);
End
End
Find the next file or directory
Found: = FindNext (SEARCHREC);
End
{After the current directory is found, if there is no data in the queue, all are found;
Otherwise there are subdirectories not found, take one out to continue to find. }
If dirs. Count > 0 Then
Begin
Pszdir: = dirs. Pop;
CurDir: = Strpas (Pszdir);
Strdispose (Pszdir);
End
Else
Break
End
Freeing resources
Dirs. Free;
FindClose (SEARCHREC);
End

Delphi Batch Change 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.