Gets all the file names under the specified directory, including the sub-directory functions

Source: Internet
Author: User

Gets all file names under the specified directory, which is a method for directory traversal:

function Tpathwatch.flist (asourfile:string): TStrings;   Find subdirectories Astrings Store find out the path, asourcefile to find the directory Varsour_path, sour_file:string; Source path, source file name type tmplist:tstringlist;   Filerec, Subfilerec:tsearchrec;i:integer;beginresult: = Tstringlist.create;   Within a function, result is not required to be freed, and when called, the defined object has been freed sour_file: = ' * * '; Any file type can choose the file type you specify if copy (Asourfile, Length (Asourfile), 1) <> ' then//after the path with a backslash sour_path: = INC Ludetrailingpathdelimiter (Trim (asourfile)) Elsesour_path: = Trim (asourfile); if not directoryexists (Sour_path) Thenbeginresult.clear;exit;end; Tmplist: = tstringlist.create; Tmplist.clear;if FindFirst (Sour_path + ' * * * ', faanyfile, Filerec) = 0 thenrepeatif ((filerec.attr and Fadirectory) <&gt ; 0) thenbeginif ((Filerec.name <> ') and (Filerec.name <> ' ... ')) thenflist (Sour_path); End;until FindNext ( FILEREC) <> 0; FindClose (FILEREC); if FindFirst (Sour_path + sour_file, faanyfile, Subfilerec) = 0 thenrepeatif ((subfilerec.attr and FaDi Rectory) = 0)Thentmplist.add (Sour_path + subfilerec.name); until FindNext (Subfilerec) <> 0; FindClose (SUBFILEREC); for I: = 0 to Tmplist.count-1 doresult.add (Tmplist.strings[i]); Tmplist.free;end;

And then under XE, I found a better way to cycle through this method, looking at the trouble, in xe in the Ioutils tdirectory and Tfile unit has helped solve:

Procedure Tform2.speedbutton5click (sender:tobject); vara:tstringdynarray;i:integer;begina:= IOUtils.TDirectory.GetFiles (' d:\zzz ', tsearchoption.soalldirectories,nil);    Get all filenames under a directory, including subdirectories for I:=0 to Length (a)-1 does Beginlog (a[i]+ ' + ' getcreationtime: '  +datetimetostr ( IOUtils.TFile.GetCreationTime  (A[i]) + ' + ' GetLastAccessTime: ' +datetimetostr ( IOUtils.TFile.GetLastAccessTime (A[i]) + ' + ' GetLastWriteTime: ' +datetimetostr (IOUtils.TFile.GetLastWriteTime (a[i ]) + "+"); end;end;

Written as a function, the return value is tstrings, can be timestamp, without

function Flist (asourfile:string; Timestate:boolean = False): tstrings;vara:tstringdynarray;i:integer;beginresult: = tstringlist.create;if TimeState = T Rue then Begina: = IOUtils.TDirectory.GetFiles (Asourfile,tsearchoption.soalldirectories,nil);    Get all filenames under a directory, including subdirectories for I: = 0 to Length (a)-1 doresult.add (A[i] + ': ' + datetimetostr (IOUtils.TFile.GetCreationTime (a[i) )));  File name + creation time End Elsebegina: = IOUtils.TDirectory.GetFiles (Asourfile,tsearchoption.soalldirectories,nil);    Get all filenames under a directory, including subdirectories for I: = 0 to Length (a)-1 doresult.add (A[i]);  File name + creation time end;

Get file creation time: only to the day

Procedure Tform2.speedbutton4click (Sender:tobject); Vardatetime:tdatetime;beginfileage (' D:\AAA.txt ', DateTime); ShowMessage (Datetimetostr (DateTime)); end;

Gets all the file names under the specified directory, including the sub-directory functions

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.