一些比较实用的小函数

扫描文件夹极其子目录,列出所有文件,已经测试通过,绝对可用
代码
procedure  FindAllFile( const  Dir:  string ;List: TStringlist);
var
  hFindFile: THandle;
  FindFileData: WIN32_FIND_DATA;
  FullName,FName,s:
string ;
begin
  s:
= IncludeTrailingPathDelimiter(Dir);
  hFindFile :
=  FindFirstFile(pchar(s + ' *.* ' ), FindFileData);
  
if  hFindFile  <>   0   then
  
begin
    
repeat
      FName:
= FindFileData.cFileName;
      FullName:
= s + FName;
      
if  (FName = ' . ' or  (FName = ' .. ' then   continue ;
      
if  (FindFileData.dwFileAttributes  and  FILE_ATTRIBUTE_DIRECTORY)  =  FILE_ATTRIBUTE_DIRECTORY  then
        FindAllFile(FullName,List)
      
else
        
begin
          List.Add(FullName);
        
end ;
    
until  FindNextFile(hFindFile, FindFileData)  =  false;
    windows.FindClose(hFindFile);
  
end ;
end ;

删除目录及其子目录和文件

 

代码

function  DeleteDirectory(NowPath:    string ):   Boolean;
  
var
      search:   TSearchRec;
      ret:   integer;
      key:   
string ;
  
begin
      
if  NowPath[Length(NowPath)] <> ' \ '   then
        NowPath:
= NowPath + ' \ ' ;
        key:
= Nowpath + ' *.* ' ;
        ret:
= findFirst(key,faanyfile,search);
      
while  ret = 0   do
      
begin
        
if ((search.Attr  and  fadirectory)  =  faDirectory) then
        
begin
          
if (Search.Name <> ' . ' ) and (Search.name <> ' .. ' ) then
            DeleteDirectory(NowPath   
+    Search.name);
        
end
        
else
        
begin
          
if ((search.attr  and  fadirectory) <> fadirectory) then
             
begin
               deletefile(NowPath   
+    search.name);
             
end ;
          
end ;
        ret:
= FindNext(search);
      
end ;
      findClose(search);
      removedir(NowPath);
      result:
= True;
  
end ;

 

获取文件扩展名

 

代码
function  ExtractExteName( const  FileName:  string ):  string ;
var
  P: Integer;
begin
  P :
=  Length(FileName);
  
while  (P  >   0 ) and (FileName[P]  <>   ' . ' do
      Dec(P);
  Result :
=  Copy(FileName, P, Length(FileName) - P + 1 );
end ;

 

判断文件是否存在

 

代码

function  FileExists( const  FileName:  string ): Boolean;
  
function  FileAge( const  FileName:  string ): Integer;
  
type
    LongRec 
=   packed   record
      
case  Integer  of
        
0 : (Lo, Hi: Word);
        
1 : (Words:  array  [ 0 .. 1 of  Word);
        
2 : (Bytes:  array  [ 0 .. 3 of  Byte);
    
end ;
  
var
    Handle: THandle;
    FindData: TWin32FindData;
    LocalFileTime: TFileTime;
  
begin
    Handle :
=  FindFirstFile(PChar(FileName), FindData);
    
if  Handle  <>  INVALID_HANDLE_VALUE  then
    
begin
      Windows.FindClose(Handle);
      
if  (FindData.dwFileAttributes  and  FILE_ATTRIBUTE_DIRECTORY)  =   0   then
      
begin
        FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
        
if  FileTimeToDosDateTime(LocalFileTime, LongRec(Result).Hi, LongRec(Result).Lo)  then
          Exit;
      
end ;
    
end ;
    Result :
=   - 1 ;
  
end ;
begin
  Result :
=  FileAge(FileName)  <>   - 1 ;
end ;

 

 

 

 

你可能感兴趣的:(函数)