Windows遍历文件夹

最近在弄遍历文件夹的事情,也了解一些情况。

调用FindFirstFile的结果是文件夹    "."

第一次调用FindNextFile的结果是文件夹   ".."

 

   
   type LPSTR is access all String;
   type LPSTR_ARRAY is array(Natural range<>) of LPSTR;
   type PPSTRS is access all LPSTR_ARRAY;

   function GetFileName(s:String) return String is
   begin
      for i in s'Range loop
         if s(i)=Character'First then
            return s(s'First..i-1);
         end if;
      end loop;
      return s;
   end GetFileName;

   subtype FILETIME is QWord;
   type WIN32_FIND_DATAA is record
      FileAttributes     : DWORD;
      CreationTime       : FILETIME;
      LastAccessTime     : FILETIME;
      LastWriteTime      : FILETIME;
      FileSize           : QWORD:=0;
      Reserved0          : QWORD;
      FileName           : String (1 .. 260);
      AlternateFileName  : String (1 .. 14);
   end record with Pack;
   type lpWIN32_FIND_DATAA is access all WIN32_FIND_DATAA;
   
   function FindFirstFile(Path:String;pf:access WIN32_FIND_DATAA) return int with Import,Convention=>Stdcall,external_Name=>"FindFirstFileA";
   function FindNextFile(h:int;pf:access WIN32_FIND_DATAA) return int with Import,Convention=>Stdcall,external_name=>"FindNextFileA";
   function FindClose (hFindFile : int) return int with Import,Convention=>Stdcall,external_name=>"FindClose";
   
   
   function GetFileName(s:String) return String is
   begin
      for i in s'Range loop
         if s(i)=Character'First then
            return s(s'First..i-1);
         end if;
      end loop;
      return s;
   end GetFileName;
   
   --
   -- 获取目录中的子目录与文件
   --
   function GetDirFiles(s:String) return PPSTRS is
   begin
      if not IsExist(s) then
         return null;
      end if;
      
      declare
         fs:PPSTRS:=null;
         num:int:=0;
         handle:int;
         fi:WIN32_FIND_DATAA;
      begin
         
         handle:=FindFirstFile(s&"/*.*"&Character'First,fi'Unrestricted_Access);
         
         if handle=-1 then 
            return null;
         end if;
         
         if FindNextFile(handle,fi'Unrestricted_Access) /= 0 then
            null;
         end if;
         
         -- 第一个文件是 "." 文件夹
         -- 第二个文件是 ".."文件夹
         
         while (FindNextFile(handle,fi'Unrestricted_Access) /= 0) loop
            num:=num+1;
         end loop;
         
         fs:=new LPSTR_Array(1..Num);
         Num:=0;
         
         handle:=FindFirstFile(s&"/*.*"&Character'First,fi'Unrestricted_Access);
         if FindNextFile(handle,fi'Unrestricted_Access) /= 0 then
            null;
         end if;
                  
         while (FindNextFile(handle,fi'Unrestricted_Access) /= 0) loop
            num:=num+1;
            -- debug
            -- Put_Line(fi.cFileName(1..260));
            -- Ada.Text_IO.Put_Line(fi.nFileSize'img);
            if (fi.dwFileAttributes and 16#10#) = 16#10# then 
               fs(num):=new String'(GetFileName(fi.cFileName)&"/");
            else
               fs(num):=new String'(GetFileName(fi.cFileName));
            end if;
         end loop;
         
         handle:=FindClose(handle);
         
         return fs;
         
      end;
      
   end GetDirFiles;

 

 

 

你可能感兴趣的:(Ada)