惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
D
Docker
有赞技术团队
有赞技术团队
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
H
Help Net Security
美团技术团队
MyScale Blog
MyScale Blog
B
Blog RSS Feed
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
小众软件
小众软件
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
G
Google Developers Blog
月光博客
月光博客
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs

博客园 - 吴文力

VC 开机自动启动程序代码 Symbian中音频API的使用(摘录) symbian 游戏开发中一些技术细节(摘录) Symbian中声音处理 动态库的调用等技术 VC小技巧 15个问题 - 吴文力 - 博客园 VC 常见问题百问 VC/MFC Q&A 200409 DELL 电脑的区别 报表式CListCtrl的使用详解初稿 俄罗斯科学家语出惊人:怀孕前就能预设生男生女 PE文件格式详解 如何让程序自动管理线程 WinNT & Win2K下实现进程的完全隐藏——宿主程序 木马病毒是如何利用文件关联和设置名感染的 104种清除木马完全攻略 Programming Applications for Microsoft Windows(四) Programming Applications for Microsoft Windows(三) Programming Applications for Microsoft Windows(二)
怎样在目录中寻找文件
吴文力 · 2006-01-09 · via 博客园 - 吴文力

下面的代码说明了怎样在一个给定的目录中从上到下地搜索整个目录树.本例子只将结果输出到system debug screen.

调用下面的类函数,搜索完成之后,将出现一个信息框.

void CTestView::OnSearch() 
{

// szFilename is declared in the header as array of char
// look for MyFile.txt (or whatever)

strcpy(szFilename,"MyFile.txt");

// go to root directory (or to whichever directory that you wish)

_chdir("C:\\");

// search for the filename

SearchDirectory();

// announce when done

MessageBox("Done Searching"); 
}

函数 SearchDirectory() 在函数 OnSearch()中被调用. SearchDirectory() 然后反复回调,直到整个目录,包括子目录都被搜索.

void CTestView::SearchDirectory() 
{
struct _finddata_t filestruct;
long hnd;
char buffer[_MAX_PATH];

// set _findfirst to find everthing

hnd = _findfirst("*",&filestruct);

// if handle fails, drive is empty...

if((hnd == -1)) return;

// get first entity on drive - check if it's a directory

if(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY 
&& !(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_HIDDEN)) { 

// if so, change to that directory and recursively call SearchDirectory

if(*filestruct.name != '.') { 

_chdir(filestruct.name);

SearchDirectory();

// go back up one directory level

_chdir("..");
}

else {

// if it's not a directory and it matches what you want...

if(!stricmp(filestruct.name,szFilename)) {

// output the filename with path to debugger

_getcwd(buffer,_MAX_PATH);
strcat(buffer,"\\");
strcat(buffer,filestruct.name); 
strcat(buffer,"\r\n");
OutputDebugString(buffer);

}

while(!(_findnext(hnd,&filestruct))) {

if(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY 
&& !(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_HIDDEN)) {

if(*filestruct.name != '.') { 
_chdir(filestruct.name);

SearchDirectory();

_chdir("..");
}
}
else {

if(!stricmp(filestruct.name,szFilename)) {
_getcwd(buffer,_MAX_PATH); 
strcat(buffer,"\\");
strcat(buffer,filestruct.name);
strcat(buffer,"\r\n");
OutputDebugString(buffer);
}
}
}

_findclose(hnd); 
}