在文件和目录搜索的问题.. windows编程 [英] problems with searching in files and directories .. windows programming
问题描述
喜
我正在学习这本书(艾迪生韦斯利的Windows系统编程第4版),我认为它没有用林在搜索code支持递归,因此可以在走向深沉进去的文件和目录的code正在工作(我猜)没有语法错误,但输出的不是我想要的东西放出来的搜索是这样的:
未找到
现在,这里的文件夹:
未找到
搜索在d:\\ iust \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\。。。。。。。。。。 \\。\\。\\。\\。\\。\\。\\。\\。
\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。 \\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。
\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。 \\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。
\\。\\。\\。\\。\\。\\ .. \\电子书\\。\\。\\。\\。\\电子书
文件名称为:d:。。。。。。。\\ iust \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\。 。\\。\\。\\。\\。\\。\\。\\
。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\ 。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\
。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\ 。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\
。\\。\\。\\。\\。\\。\\。\\。\\ .. \\电子书\\。\\。\\。\\。\\电子书\\ *。*测试
未找到
现在,这里的文件夹:
未找到
搜索在d:\\ iust \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\。。。。。。。。。。 \\。\\。\\。\\。\\。\\。\\。\\。
\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。 \\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。
\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。 \\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。
\\。\\。\\。\\。\\。\\..\\电子书\\。\\。\\。\\..
文件名称为:d:。。。。。。。\\ iust \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\。 。\\。\\。\\。\\。\\。\\。\\
。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\ 。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\
。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\ 。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\。\\
。\\。\\。\\。\\。\\。\\。\\。\\ .. \\电子书\\。\\。\\。\\ .. \\ *。*测试
未找到
现在,这里的文件夹:
首先,我认为notiiced什么都我不会只搜索我指定的文件夹内,但在所有的整个驱动器,第二恼人probem是DOTS的。和那些..出现在每个文件夹中的我怎么能避免这个问题。现在我用我之前提到的这本书之前,林说,但我不知道,我只是不喜欢我所做的是有更好的方法,形成我的$ C $ C。
在code:
的#includestdafx.h中
#包括LT&;&WINDOWS.H GT;无效SearchForFile(TCHAR *文件夹,TCHAR *文件){
_tprintf(L在%s \\ n搜索,文件夹); //只是为了显示状态
TCHAR温度[1000]; _stprintf(温度,L%s的\\\\%S,文件夹,文件); //写到这里进入临时位置为文件夹/文件
_tprintf(L的文件名是:%S \\ n,温度);
拉手F;
WIN32_FIND_DATA数据;
F =用FindFirstFile(温度,和放大器;数据);
如果(F == INVALID_HANDLE_VALUE){
_tprintf(L未找到\\ n); }
其他{
_tprintf(L发现这个文件:%s \\ n,data.cFileName);
而(FindNextFile(F,放大器;数据)){
_tprintf(L发现这个文件:%s \\ n,data.cFileName);
}
FindClose(F);
} _stprintf(温度,L%s的\\\\ *,文件夹); //D:\\ *的例子
_tprintf(L现在,这里的文件夹:\\ n);
F =用FindFirstFile(温度,和放大器;数据);
TCHAR TEMP2 [1000];
如果(F == INVALID_HANDLE_VALUE){
_tprintf(L未找到\\ n); }
其他{
如果((data.dwFileAttributes&放大器;!FILE_ATTRIBUTE_DIRECTORY)= 0)
{ // _ tprintf(L发现这个目录:%S \\ n,data.cFileName);
_stprintf(TEMP2,L%s的\\\\%S,文件夹,data.cFileName);
SearchForFile(TEMP2,文件);
}
而(FindNextFile(F,放大器;数据)){// _tprintf(L%D \\ n,data.dwFileAttributes,FILE_ATTRIBUTE_DIRECTORY);
如果((data.dwFileAttributes&放大器;!FILE_ATTRIBUTE_DIRECTORY)= 0)
// _tprintf(L发现这个目录:%S \\ n,data.cFileName);
{
_stprintf(TEMP2,L%s的\\\\%S,文件夹,data.cFileName);
SearchForFile(TEMP2,文件); }
}
FindClose(F);
}
}
INT _tmain(INT ARGC,_TCHAR *的argv [])
{
SearchForFile(LD:\\\\测试,L*测试*);
返回0;
}
您必须筛选出。
和 ..
的每个文件夹中发现的伪文件夹。结果
粗略地说,在你的递归分支:
IF((data.dwFileAttributes&安培;!FILE_ATTRIBUTE_DIRECTORY)= 0
&功放;&安培; data.data.cFileName!=。
&功放;&安培; data.data.cFileName!=..)
Hi I'm studying this book (Addison Wesley Windows System Programming 4th Edition) and I think its useless Im working on a searching code that support the recursive so it can go in deepth in files and directories the code is working ( I guess ) no syntax error but the output is not what I want the out put of the search is like:
not found
Now, here are the folders:
not found
Searching in d:\iust\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\..\e-books\.\.\.\.\E-BOOKS
The file name is: d:\iust\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\..\e-books\.\.\.\.\E-BOOKS\*Test*
not found
Now, here are the folders:
not found
Searching in d:\iust\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\..\e-books\.\.\.\..
The file name is: d:\iust\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\..\e-books\.\.\.\..\*Test*
not found
Now, here are the folders:
First I notiiced that what ever I do it will not search just inside the folder i specified but in all whole drive and the second annoying probem is the DOTS the . and .. those appear in each folder how can I avoid this problem. now as i said before Im using the book I mentioned before but I dont know I just dont like what i did is there a better way to form my code .
the code :
#include "stdafx.h"
#include <windows.h>
void SearchForFile(TCHAR *folder, TCHAR *file){
_tprintf(L"Searching in %s\n",folder); //just to show the state
TCHAR temp[1000];
_stprintf(temp,L"%s\\%s",folder,file); // here wrote into temp the location as folder/file
_tprintf(L"The file name is: %s\n",temp);
HANDLE f;
WIN32_FIND_DATA data;
f=FindFirstFile(temp,&data);
if(f==INVALID_HANDLE_VALUE){
_tprintf(L"not found\n");
}
else{
_tprintf(L"found this file: %s\n",data.cFileName);
while(FindNextFile(f,&data)){
_tprintf(L"found this file: %s\n",data.cFileName);
}
FindClose(f);
}
_stprintf(temp,L"%s\\*",folder); // "d:\*" for example
_tprintf(L"Now, here are the folders:\n");
f=FindFirstFile(temp,&data);
TCHAR temp2[1000];
if(f==INVALID_HANDLE_VALUE){
_tprintf(L"not found\n");
}
else{
if((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
{
//_tprintf(L"found this directory: %s\n",data.cFileName);
_stprintf(temp2,L"%s\\%s",folder,data.cFileName);
SearchForFile(temp2,file);
}
while(FindNextFile(f,&data)){// _tprintf(L"%d %d\n",data.dwFileAttributes,FILE_ATTRIBUTE_DIRECTORY);
if((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
// _tprintf(L"found this directory: %s\n",data.cFileName);
{
_stprintf(temp2,L"%s\\%s",folder,data.cFileName);
SearchForFile(temp2,file);
}
}
FindClose(f);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
SearchForFile(L"d:\\test", L"*Test*");
return 0;
}
You have to filter out the .
and ..
pseudo-folders found in every folder.
Roughly, in your recursive branch:
if((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0
&& data.data.cFileName != "."
&& data.data.cFileName != "..")
这篇关于在文件和目录搜索的问题.. windows编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!