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) <> ; 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