分享

判断某文件是文件夹还是文件

 quasiceo 2013-01-17
DWORD GetFileAttributes(
  LPCTSTR lpFileName   // name of file or directory
);

if( 返回值 & FILE_ATTRIBUTE_DIRECTORY )
var
  C: Cardinal;
begin
  C := GetFileAttributes('d:\a');
  if C = $FFFFFFFF then ShowMessage('文件或文件夹不存在!')
  else if C and FILE_ATTRIBUTE_DIRECTORY <> 0 then ShowMessage('是文件夹!')
  else ShowMessage('是文件!')
end;

uses filectrl;
s:='c:\a';
if fileexists(s) then showmessage('is File')
else if directoryexists(s) then showmessage('is Dir')
else showmessage('not exists');



function IsDirectory(FileName: string): boolean;
var
sr: TSearchRec;
begin
if FindFirst(FileName, faAnyFile, sr) = 0 then
Result :=(sr.Attr and FaDirectory <>FaDirectory)
 else
Result := false;

FindClose(sr);
end;

function FileSize(FileName: string): Int64;
var
sr: TSearchRec;
begin
if FindFirst(FileName, faAnyFile, sr) = 0 then
Result := Int64(sr.FindData.nFileSizeHigh) shl 32 + Int64(sr.FindData.nFileSizeLow)
else
Result := 0;

FindClose(sr);
end;

判断某文件是文件夹还是文件

 

复制代码
 1 //头文件 
 2 #include "stdio.h" 
 3 #include "stdlib.h" 
 4 #include <sys/stat.h> 
 5 //代码 
 6 int main() 
 7 
 8     char* fileName = "aa.txt"
 9     struct _stat buf; 
10     int result; 
11     result = _stat( fileName, &buf ); 
12     if(_S_IFDIR & buf.st_mode){ 
13         printf("folder\n"); 
14     }else if(_S_IFREG & buf.st_mode){ 
15         printf("file\n"); 
16     } 
17 
18     return 0
19 }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多