查找某个目录下的所有文件(包括子目录)
2015-10-08 10:40
351 查看
#include "stdio.h"
#include "windows.h"
#include "winbase.h"
void FindFileInDir(char* rootDir);
main()
{
FindFileInDir("C:\\");
}
void FindFileInDir(char* rootDir)//, char* strRet)
{
char fname[1024];
ZeroMemory(fname, 1024);
WIN32_FIND_DATA fd;
ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
HANDLE hSearch;
char filePathName[256];
char tmpPath[256];
ZeroMemory(filePathName, 256);
ZeroMemory(tmpPath, 256);
strcpy(filePathName, rootDir);
BOOL bSearchFinished = FALSE;
if( filePathName[strlen(filePathName) -1] != '\\' )
{
strcat(filePathName, "\\");
}
strcat(filePathName, "*");
hSearch = FindFirstFile(filePathName, &fd);
//Is directory
if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&& strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
{
strcpy(tmpPath, rootDir);
strcat(tmpPath, fd.cFileName);
if( tmpPath[strlen(tmpPath) -1] != '\\' )
{
strcat(tmpPath, "\\");
}
FindFileInDir(tmpPath);
}
while( !bSearchFinished )
{
if( FindNextFile(hSearch, &fd) )
{
if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&& strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
{
strcpy(tmpPath, rootDir);
strcat(tmpPath, fd.cFileName);
if( tmpPath[strlen(tmpPath) -1] != '\\' )
{
strcat(tmpPath, "\\");
}
FindFileInDir(tmpPath);
}
else if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
{
printf("%s%s\n",rootDir,fd.cFileName);
}
}
else
{
if( GetLastError() == ERROR_NO_MORE_FILES )
{
bSearchFinished = TRUE;
}
else
bSearchFinished = TRUE;
}
}
FindClose(hSearch);
}
#include "windows.h"
#include "winbase.h"
void FindFileInDir(char* rootDir);
main()
{
FindFileInDir("C:\\");
}
void FindFileInDir(char* rootDir)//, char* strRet)
{
char fname[1024];
ZeroMemory(fname, 1024);
WIN32_FIND_DATA fd;
ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
HANDLE hSearch;
char filePathName[256];
char tmpPath[256];
ZeroMemory(filePathName, 256);
ZeroMemory(tmpPath, 256);
strcpy(filePathName, rootDir);
BOOL bSearchFinished = FALSE;
if( filePathName[strlen(filePathName) -1] != '\\' )
{
strcat(filePathName, "\\");
}
strcat(filePathName, "*");
hSearch = FindFirstFile(filePathName, &fd);
//Is directory
if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&& strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
{
strcpy(tmpPath, rootDir);
strcat(tmpPath, fd.cFileName);
if( tmpPath[strlen(tmpPath) -1] != '\\' )
{
strcat(tmpPath, "\\");
}
FindFileInDir(tmpPath);
}
while( !bSearchFinished )
{
if( FindNextFile(hSearch, &fd) )
{
if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&& strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
{
strcpy(tmpPath, rootDir);
strcat(tmpPath, fd.cFileName);
if( tmpPath[strlen(tmpPath) -1] != '\\' )
{
strcat(tmpPath, "\\");
}
FindFileInDir(tmpPath);
}
else if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
{
printf("%s%s\n",rootDir,fd.cFileName);
}
}
else
{
if( GetLastError() == ERROR_NO_MORE_FILES )
{
bSearchFinished = TRUE;
}
else
bSearchFinished = TRUE;
}
}
FindClose(hSearch);
}
相关文章推荐
- java四中引用
- C与指针
- C#中三种弹出信息窗口的方式
- 深入解读Quartz的原理
- 奋斗了一个假期
- .net c#线程使用心得
- VMware虚拟机克隆CentOS 6.5后网卡修改方法
- 查看自己的eclipse都安装了哪些插件
- UI:动画
- WPF XAML之bing使用StringFormat
- 深入解读Quartz的原理
- [Training Video - 1] [Introduction to Web services]
- Spring @Aspect切面参数传递
- 使用EntitysCodeGenerate
- ThinkPHP与EasyUI整合之三(searchbox):在datagrid中查询指定记录
- js小数运算出现多位小数如何解决
- jQuery实现仿微软首页感应鼠标变化滑动窗口效果
- 93格式条码打印到屏幕上可以扫出来,但是打印到打印机扫不来的的问题
- android-工程目录结构
- PHP计划任务:如何使用Linux的Crontab执行PHP脚本