1.
windows程序中文件查找函数
SetCurrentDirectory函数;
FindFirstFile函数;
GetFullPathName函数;
CStringList& CFileSearchEngine::SearchFile(
CString strPath,
CStringList &listFileName,
CString strFileName)
{
char szFullPathName[MAX_PATH];
/*static*/ WIN32_FIND_DATAfindData;
SetCurrentDirectory(strPath);
HANDLE hFindHandle = FindFirstFile(strFileName, &findData);
if ((hFindHandle != INVALID_HANDLE_VALUE))
{
if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
GetFullPathName(findData.cFileName, MAX_PATH, szFullPathName, NULL);
if(!(::GetFileAttributes(findData.cFileName) & FILE_ATTRIBUTE_DIRECTORY))
{
//文件过滤
if(m_strFilter=="")
listFileName.AddTail(szFullPathName);
else if(_MultiMatching( (LPCTSTR)GetFileName(szFullPathName), (LPCTSTR)m_strFilter))
listFileName.AddTail(szFullPathName);
}
}
while (FindNextFile(hFindHandle, &findData) != 0)
{
if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
GetFullPathName(findData.cFileName, MAX_PATH, szFullPathName, NULL);
if(!(::GetFileAttributes(findData.cFileName) & FILE_ATTRIBUTE_DIRECTORY))
{
//文件过滤
if(m_strFilter=="")
listFileName.AddTail(szFullPathName);
else if(_MultiMatching( (LPCTSTR)GetFileName(szFullPathName), (LPCTSTR)m_strFilter))
listFileName.AddTail(szFullPathName);
}
}
}
FindClose(hFindHandle);
}
hFindHandle = FindFirstFile("*", &findData);
if ((hFindHandle != INVALID_HANDLE_VALUE))
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (findData.cFileName[0] != '.')
{
SearchFile(findData.cFileName, listFileName,strFileName);
SetCurrentDirectory("..");
}
}
while (FindNextFile(hFindHandle, &findData) != 0)
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (findData.cFileName[0] != '.')
{
SearchFile(findData.cFileName, listFileName,strFileName);
SetCurrentDirectory("..");
}
}
}
FindClose(hFindHandle);
}
return listFileName;
}
在linux下实现文件查找
CStringList& CFileSearchEngine::SearchFile(
CStdString strPath,
CStringList &listFileName,
CStdString strFileName)
{
CStdString szFullPathName;
chdir(strPath);
DIR *pDir ;
struct dirent *file ;
if(!(pDir = opendir(strPath)))
{
CStdString str;
str.Format("error opendir %s!!!\n",strPath);
AxNote(1,str);
}
while((file = readdir(pDir)) != NULL)
{
if (file->d_type & DT_DIR)
{
if (strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)
continue;
}
if(strcmp(file->d_name,strFileName) == 0)
{
szFullPathName.Format("%s/%s",strPath,file->d_name);
struct stat status;
stat(szFullPathName,&status);
if(!S_ISDIR(status.st_mode))
{
//文件过滤
if (m_strFilter == "")
listFileName.push_back(szFullPathName);
else if (_MultiMatching((LPCTSTR) file->d_name,(LPCTSTR) m_strFilter))
listFileName.push_back(szFullPathName);
}
}
}
int pos = strPath.ReverseFind('/');
strPath.Delete(pos, strPath.length() - pos);
if (strPath != "") //这里是从最里面的文件夹往外层搜索直到根目录递归调用
{
SearchFile(strPath,listFileName,strFileName);
}
closedir(pDir);
return listFileName;
}
2.windows程序中_splitpath函数在linux中的实现(转自网上资料)
- void _splitpath(const char *path, char *drive, char *dir, char *fname, char *ext)
- {
- char *p_whole_name;
- drive[0] = '\0';
- if (NULL == path)
- {
- dir[0] = '\0';
- fname[0] = '\0';
- ext[0] = '\0';
- return;
- }
- if ('/' == path[strlen(path)])
- {
- strcpy(dir, path);
- fname[0] = '\0';
- ext[0] = '\0';
- return;
- }
- p_whole_name = rindex(path, '/');
- if (NULL != p_whole_name)
- {
- p_whole_name++;
- _split_whole_name(p_whole_name, fname, ext);
- snprintf(dir, p_whole_name - path, "%s", path);
- }
- else
- {
- _split_whole_name(path, fname, ext);
- dir[0] = '\0';
- }
- }
- static void _split_whole_name(const char *whole_name, char *fname, char *ext)
- {
- char *p_ext;
- p_ext = rindex(whole_name, '.');
- if (NULL != p_ext)
- {
- strcpy(ext, p_ext);
- snprintf(fname, p_ext - whole_name + 1, "%s", whole_name);
- }
- else
- {
- ext[0] = '\0';
- strcpy(fname, whole_name);
- }
- }
3.wndows程序中获取磁盘剩余空间大小
函数:BOOL ret = GetDiskFreeSpaceEx(
drive,
(PULARGE_INTEGER)&qwFreeBytesToCaller,
(PULARGE_INTEGER)&qwTotalBytes,
(PULARGE_INTEGER)&qwFreeBytes
);
linux下可以使用
int statfs(const char *path, struct statfs *buf);
int fstatfs(int fd, struct statfs *buf);
头文件 #include <sys/vfs.h> /* or <sys/statfs.h> */