c++获取文件(夹)属性

来源:互联网 发布:相册排版软件 编辑:程序博客网 时间:2024/04/27 19:22

HRESULT GetFolderTime( const wstring wstrPath, wstring &wstrRes)
{
 if ( wstrPath.length() < 3)
 {
  return -1;
 }
 wstring wstrShortPath;
 int iPos2 = wstrPath.rfind( L"//");

 wstrShortPath = wstrPath.substr( 0,iPos2);

 wstring wstrFileFilter;
 wstring wstrFilePath = wstrPath.substr( iPos2 + 1,wstrPath.length() - iPos2);
 HANDLE hFind = NULL;
 WIN32_FIND_DATA fileinfo;
 DOUBLE    dwSize = 0;

 wstrFileFilter = wstrShortPath;

 wstrFileFilter += L"//*";

 hFind = FindFirstFile( wstrFileFilter.c_str(), &fileinfo);
 do
 {
  if(fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  {
   //找到指定文件夹
   if( fileinfo.cFileName == wstrFilePath)
   {
    TCHAR tchBuf[20] = { 0 };
    SYSTEMTIME sysTime;
    FileTimeToSystemTime( &fileinfo.ftCreationTime, &sysTime);
    //创建时间
    //年
    _itow( sysTime.wYear, tchBuf, 10);
    wstrRes += tchBuf;
    //月
    _itow( sysTime.wMonth, tchBuf, 10);
    if( sysTime.wMonth < 10)
    {
     wstrRes += L"-0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L"-";
     wstrRes += tchBuf;
    }

    //日
    _itow( sysTime.wDay, tchBuf, 10);
    if( sysTime.wDay < 10)
    {
     wstrRes += L"-0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L"-";
     wstrRes += tchBuf;
    }

    wstrRes += L",";
    //小时
    _itow( sysTime.wHour, tchBuf, 10);
    if( sysTime.wHour < 10)
    {
     wstrRes += L"0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L":";
     wstrRes += tchBuf;
    }
    //分
    _itow( sysTime.wMinute, tchBuf, 10);
    if( sysTime.wMinute < 10)
    {
     wstrRes += L":0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L":";
     wstrRes += tchBuf;
    }

    //秒
    _itow( sysTime.wSecond, tchBuf, 10);
    if( sysTime.wSecond < 10)
    {
     wstrRes += L":0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L":";
     wstrRes += tchBuf;
    }


    memset( tchBuf, 0, sizeof( tchBuf));
    wstrRes += L"|";

    FileTimeToSystemTime( &fileinfo.ftLastWriteTime, &sysTime);
    //最后修改时间
    //年
    _itow( sysTime.wYear, tchBuf, 10);

    wstrRes += tchBuf;
    //月
    _itow( sysTime.wMonth, tchBuf, 10);
    if( sysTime.wMonth < 10)
    {
     wstrRes += L"-0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L"-";
     wstrRes += tchBuf;
    }

    //日
    _itow( sysTime.wDay, tchBuf, 10);
    if( sysTime.wDay < 10)
    {
     wstrRes += L"-0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L"-";
     wstrRes += tchBuf;
    }

    wstrRes += L",";
    //小时
    _itow( sysTime.wHour, tchBuf, 10);
    if( sysTime.wHour < 10)
    {
     wstrRes += L"0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L":";
     wstrRes += tchBuf;
    }
    //分
    _itow( sysTime.wMinute, tchBuf, 10);
    if( sysTime.wMinute < 10)
    {
     wstrRes += L":0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L":";
     wstrRes += tchBuf;
    }

    //秒
    _itow( sysTime.wSecond, tchBuf, 10);
    if( sysTime.wSecond < 10)
    {
     wstrRes += L":0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L":";
     wstrRes += tchBuf;
    }
    memset( tchBuf, 0, sizeof( tchBuf));
    wstrRes += L"|";

    FileTimeToSystemTime( &fileinfo.ftLastAccessTime, &sysTime);
    //最后访问时间
    //年
    _itow( sysTime.wYear, tchBuf, 10);
    wstrRes += tchBuf;
    //月
    _itow( sysTime.wMonth, tchBuf, 10);
    if( sysTime.wMonth < 10)
    {
     wstrRes += L"-0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L"-";
     wstrRes += tchBuf;
    }

    //日
    _itow( sysTime.wDay, tchBuf, 10);
    if( sysTime.wDay < 10)
    {
     wstrRes += L"-0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L"-";
     wstrRes += tchBuf;
    }

    wstrRes += L",";
    //小时
    _itow( sysTime.wHour, tchBuf, 10);
    if( sysTime.wHour < 10)
    {
     wstrRes += L"0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L":";
     wstrRes += tchBuf;
    }
    //分
    _itow( sysTime.wMinute, tchBuf, 10);
    if( sysTime.wMinute < 10)
    {
     wstrRes += L":0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L":";
     wstrRes += tchBuf;
    }

    //秒
    _itow( sysTime.wSecond, tchBuf, 10);
    if( sysTime.wSecond < 10)
    {
     wstrRes += L":0";
     wstrRes += tchBuf;
    }
    else
    {
     wstrRes += L":";
     wstrRes += tchBuf;
    }
    wstrRes += L"|";

    dwSize = fileinfo.dwFileAttributes;
    _ltow( dwSize, tchBuf, 10);
    wstrRes += tchBuf;//文件类型
    memset( tchBuf, 0, sizeof( tchBuf));
    wstrRes += L"|";
   }
  }


 }while( FindNextFile( hFind, &fileinfo));

 return 0;
}

 


 功 能 描 述:  -取得目录的大小

DWORD64 GetFolderSize( const wstring wstrPath)
{
 if ( wstrPath.length() < 0)
 {
  return -1;
 }

 wstring wstrFileFilter;
 wstring wstrFilePath;
 HANDLE hFind = NULL;
 WIN32_FIND_DATA fileinfo;
 DWORD64    dwSize = 0;

 wstrFileFilter = wstrPath;

 wstrFileFilter += L"//*";

 hFind = FindFirstFile( wstrFileFilter.c_str(), &fileinfo);
 do
 {
  if(fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  {
   if (!wcscmp(fileinfo.cFileName,L".") || !wcscmp(fileinfo.cFileName, L".."))
   {
    //Do nothing for "." and ".." folders
   }
   else
   {
    wstring wstrTmp;
    wstrTmp += wstrPath;
    wstrTmp += L"//";
    wstrTmp += fileinfo.cFileName;
    dwSize = dwSize + GetFolderSize( wstrTmp);
    /* if(dwFolders != NULL)
    {
    ++(*dwFolders);
    }*/
   }
  }
  else
  {
   /* if(dwFiles != NULL)
   {
   ++(*dwFiles);
   }*/
  }

  dwSize += fileinfo.nFileSizeLow;
 }while(FindNextFile(hFind,&fileinfo));
 FindClose(hFind);
 return dwSize;
}

 


功 能 描 述:  -取得目录包含的子目录数目

DWORD64 GetSubFolderAmount( const wstring wstrPath)
{
 if ( wstrPath.length() < 0)
 {
  return -1;
 }

 wstring wstrFileFilter;
 wstring wstrFilePath;
 HANDLE hFind = NULL;
 WIN32_FIND_DATA fileinfo;
 DWORD64    dwSize = 0;

 wstrFileFilter = wstrPath;

 wstrFileFilter += L"//*";

 hFind = FindFirstFile( wstrFileFilter.c_str(), &fileinfo);
 do
 {
  if( INVALID_HANDLE_VALUE != hFind && fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  {
   if (!wcscmp(fileinfo.cFileName,L".") || !wcscmp(fileinfo.cFileName, L".."))
   {
    //Do nothing for "." and ".." folders
   }
   else
   {
    wstring wstrTmp;
    wstrTmp += wstrPath;
    wstrTmp += L"//";
    wstrTmp += fileinfo.cFileName;
    GetSubFolderAmount( wstrTmp);
    ++ dwSize;
   }
  }
  else
  {
   /* if(dwFiles != NULL)
   {
   ++(*dwFiles);
   }*/
  }

 }while(FindNextFile(hFind,&fileinfo));
 FindClose(hFind);
 return dwSize;
}

 


功 能 描 述:  -取得目录包含的文件数目
DWORD64 GetFolderFileAmount( const wstring wstrPath)
{
 if ( wstrPath.length() < 0)
 {
  return -1;
 }

 wstring wstrFileFilter;
 wstring wstrFilePath;
 HANDLE hFind = NULL;
 WIN32_FIND_DATA fileinfo;
 DWORD64    dwSize = 0;

 wstrFileFilter = wstrPath;

 wstrFileFilter += L"//*";

 hFind = FindFirstFile( wstrFileFilter.c_str(), &fileinfo);
 do
 {
  if( INVALID_HANDLE_VALUE != hFind && fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  {
   if (!wcscmp(fileinfo.cFileName,L".") || !wcscmp(fileinfo.cFileName, L".."))
   {
    //Do nothing for "." and ".." folders
   }
   else
   {
    wstring wstrTmp;
    wstrTmp += wstrPath;
    wstrTmp += L"//";
    wstrTmp += fileinfo.cFileName;
    dwSize += GetFolderFileAmount( wstrTmp);

   }
  }
  else if( INVALID_HANDLE_VALUE != hFind)
  {
   ++ dwSize;
   printf( "-------%s/n", fileinfo.cFileName);
  }

 }while( FindNextFile(hFind,&fileinfo));
 FindClose(hFind);
 return dwSize;
}