一个在某个目录中查找所有属于某个确定扩展名的文件的程序,但在辨析子目录是文件夹还是文件上产生了漏洞,请高人赐教
比如查找在 D:\测试
中的所有.doc类型的文件(含子文件夹中的)
将具体的文件保存目录和文件总数显示在界面上的列表框控件上,并产生一个记事本文件(文件保存目录和文件总数的汇总结果)保存在输出地址目录中。改程序无法解决子文件夹里面的符合条件的文件查找,怎么办求高手指教。资源描述: 三个编辑框控件,输入查找的目录地址(控件变量名:m_InputAddress),输出结果的目录地址(控件变量名:m_OutputAddress,该地址生成一个记事本文件),查找的文件类型(控件变量名:m_FileType)。
一个列表框控件,(控件变量名:m_ResultList)具体函数程序如下:
void CFindFileDlg::OnOK()
{
// TODO: Add extra validation here
int k;
DWORD val;
long FileHandle;
CString InputAddress,OutputAddress,Filetype,Path,str;
FILE *fs;
CStringArray Result;
_finddata_t ft;
m_InputAddress.GetWindowText(InputAddress);
m_OutputAddress.GetWindowText(OutputAddress);
m_FileType.GetWindowText(Filetype);
if (InputAddress.IsEmpty() || OutputAddress.IsEmpty())
{
AfxMessageBox("待查找文件的地址或者输出结果的地址不正确。");
return;
}
if (Filetype.IsEmpty())
{
AfxMessageBox("待查找文件的类型未知。");
return;
}
Path=InputAddress+"\\*";
OutputAddress=OutputAddress+"\\搜索结果.txt";
if ((fs=fopen(OutputAddress,"w"))==NULL)
{
AfxMessageBox("未能生成有效的搜索结果文件记录。");
return;
}
if ((FileHandle=_findfirst(Path,&ft))!=-1)
{
do
{
m_FileType.GetWindowText(Filetype);
val=::GetFileAttributes(ft.name);
if (val & FILE_ATTRIBUTE_DIRECTORY)/////如果查找的是目录的话SetCurrentDirectory(路径名);
{
Filetype="*."+Filetype;
str=InputAddress+"\\"+ft.name;
SearchFile(Filetype,str,fs,Result);
::SetCurrentDirectory(InputAddress);////设置当前目录为原始目录。
}
else/////是一个文件的话
{
Filetype="."+Filetype;
if (strstr(ft.name,Filetype))
{
fprintf(fs,"查找到的文件在 %s 目录下: %s\n",InputAddress,ft.name);
Result.Add(ft.name);
}
}
} while (_findnext(FileHandle,&ft)!=-1);
_findclose(FileHandle);
}
fclose(fs);
if (Result.GetSize()>0)
{
for (k=0;k<Result.GetSize();++k)
{
m_ResultList.InsertString(k,Result[k]);
}
str.Format("\n总共找到%d个文件",Result.GetSize());
m_ResultList.InsertString(k,str);
}
else
{
AfxMessageBox("在指定的根目录下没有找到指定类型的文件。");
}
}int CFindFileDlg::SearchFile(CString FileType, CString Directory,FILE *&fs,CStringArray &Result)
{/////////该函数需要自身递归
long FileHandle;
int k;
DWORD val;
_finddata_t ft;
CString str,Path,Type;
Type=FileType;
k=Directory.ReverseFind('\\');
if (Directory.Mid(k+1)=="." || Directory.Mid(k+1)=="..")
{
return 0;
}
Path=Directory+"\\*";
if ((FileHandle=_findfirst(Path,&ft))!=-1)
{
do
{
FileType=Type;
val=::GetFileAttributes(ft.name);
str=Directory+"\\"+ft.name;
if (val == FILE_ATTRIBUTE_DIRECTORY)/////如果查找的是目录的话SetCurrentDirectory(路径名);
{
SearchFile(FileType,str,fs,Result);
::SetCurrentDirectory(Path);////设置当前目录为原始目录。
}
else/////是一个文件的话
{
FileType="."+FileType;
if (strstr(ft.name,FileType))
{
fprintf(fs,"查找到的文件在 %s 目录下: %s\n",str,ft.name);
Result.Add(ft.name);
return 1;
}
}
} while (_findnext(FileHandle,&ft)!=-1);
_findclose(FileHandle);
}
return 0;
}
比如查找在 D:\测试
中的所有.doc类型的文件(含子文件夹中的)
将具体的文件保存目录和文件总数显示在界面上的列表框控件上,并产生一个记事本文件(文件保存目录和文件总数的汇总结果)保存在输出地址目录中。改程序无法解决子文件夹里面的符合条件的文件查找,怎么办求高手指教。资源描述: 三个编辑框控件,输入查找的目录地址(控件变量名:m_InputAddress),输出结果的目录地址(控件变量名:m_OutputAddress,该地址生成一个记事本文件),查找的文件类型(控件变量名:m_FileType)。
一个列表框控件,(控件变量名:m_ResultList)具体函数程序如下:
void CFindFileDlg::OnOK()
{
// TODO: Add extra validation here
int k;
DWORD val;
long FileHandle;
CString InputAddress,OutputAddress,Filetype,Path,str;
FILE *fs;
CStringArray Result;
_finddata_t ft;
m_InputAddress.GetWindowText(InputAddress);
m_OutputAddress.GetWindowText(OutputAddress);
m_FileType.GetWindowText(Filetype);
if (InputAddress.IsEmpty() || OutputAddress.IsEmpty())
{
AfxMessageBox("待查找文件的地址或者输出结果的地址不正确。");
return;
}
if (Filetype.IsEmpty())
{
AfxMessageBox("待查找文件的类型未知。");
return;
}
Path=InputAddress+"\\*";
OutputAddress=OutputAddress+"\\搜索结果.txt";
if ((fs=fopen(OutputAddress,"w"))==NULL)
{
AfxMessageBox("未能生成有效的搜索结果文件记录。");
return;
}
if ((FileHandle=_findfirst(Path,&ft))!=-1)
{
do
{
m_FileType.GetWindowText(Filetype);
val=::GetFileAttributes(ft.name);
if (val & FILE_ATTRIBUTE_DIRECTORY)/////如果查找的是目录的话SetCurrentDirectory(路径名);
{
Filetype="*."+Filetype;
str=InputAddress+"\\"+ft.name;
SearchFile(Filetype,str,fs,Result);
::SetCurrentDirectory(InputAddress);////设置当前目录为原始目录。
}
else/////是一个文件的话
{
Filetype="."+Filetype;
if (strstr(ft.name,Filetype))
{
fprintf(fs,"查找到的文件在 %s 目录下: %s\n",InputAddress,ft.name);
Result.Add(ft.name);
}
}
} while (_findnext(FileHandle,&ft)!=-1);
_findclose(FileHandle);
}
fclose(fs);
if (Result.GetSize()>0)
{
for (k=0;k<Result.GetSize();++k)
{
m_ResultList.InsertString(k,Result[k]);
}
str.Format("\n总共找到%d个文件",Result.GetSize());
m_ResultList.InsertString(k,str);
}
else
{
AfxMessageBox("在指定的根目录下没有找到指定类型的文件。");
}
}int CFindFileDlg::SearchFile(CString FileType, CString Directory,FILE *&fs,CStringArray &Result)
{/////////该函数需要自身递归
long FileHandle;
int k;
DWORD val;
_finddata_t ft;
CString str,Path,Type;
Type=FileType;
k=Directory.ReverseFind('\\');
if (Directory.Mid(k+1)=="." || Directory.Mid(k+1)=="..")
{
return 0;
}
Path=Directory+"\\*";
if ((FileHandle=_findfirst(Path,&ft))!=-1)
{
do
{
FileType=Type;
val=::GetFileAttributes(ft.name);
str=Directory+"\\"+ft.name;
if (val == FILE_ATTRIBUTE_DIRECTORY)/////如果查找的是目录的话SetCurrentDirectory(路径名);
{
SearchFile(FileType,str,fs,Result);
::SetCurrentDirectory(Path);////设置当前目录为原始目录。
}
else/////是一个文件的话
{
FileType="."+FileType;
if (strstr(ft.name,FileType))
{
fprintf(fs,"查找到的文件在 %s 目录下: %s\n",str,ft.name);
Result.Add(ft.name);
return 1;
}
}
} while (_findnext(FileHandle,&ft)!=-1);
_findclose(FileHandle);
}
return 0;
}
void FindFile(CString strPath)
{
CString strTemp;
CFileFind fileFinder; BOOL bIsFinded = fileFinder.FindFile(strPath+_T("\\*.*"));
while (bIsFinded)
{
bIsFinded = fileFinder.FindNextFile(); if(!fileFinder.IsDots())
{
strTemp = fileFinder.GetFilePath();
if(fileFinder.IsDirectory())
FindFile(strTemp+_T('\\'));
else
{
if(!strcmp(strTemp.Right(4),".doc"))
m_strFilesSrc.Add(strTemp);
}
}
}
fileFinder.Close();
}
记得在头文件中定义:
CStringArray m_strFilesSrc;
{
CString strTemp;
CFileFind fileFinder; BOOL bIsFinded = fileFinder.FindFile(strPath+_T("\\*.*"));
while (bIsFinded)
{
bIsFinded = fileFinder.FindNextFile(); if(!fileFinder.IsDots())
{
strTemp = fileFinder.GetFilePath();
if(fileFinder.IsDirectory())
FindFile(strTemp);
else
{
if(!strcmp(strTemp.Right(4),".doc"))
m_strFilesSrc.Add(strTemp);
}
}
}
fileFinder.Close();
}
谁能修改我的代码,从而实现该功能呢?
我把分都给他。
{
bWorking=CFind.FindFile(arrstr[i],0);
while(bWorking)
{
bWorking=CFind.FindNextFile();
arrstr[i]=CFind.GetFilePath();
m_list.AddString(arrstr[i]);
}
// CFind.Close();
}CString str;
int j,n;
for(j=0;j<19;j++)
{
if((arrstr[j].Right(4)).Find('.')==(-1))
{
for(n=0;n<arrstr[j].GetLength();)
{
if(arrstr[j].GetAt(n)=='\\')
{
arrstr[j].Insert(n,"\\");
n++;n++;
}
else {n++;}
}
arrstr[j]=arrstr[j]+"\\\\*.*";
bWorking=CFind.FindFile(arrstr[j],0);
while(bWorking)
{
bWorking=CFind.FindNextFile();
str=CFind.GetFilePath();
if(str.Right(4)==".mp3"||str.Right(4)==".wma"||str.Right(4)==".ape")
{
m_list.AddString(str); //MessageBox(arrstr[i+1]);
}
}
// CFind.Close();
}
这是我的文件查找系统 部分代码
如果是目录,则就递归,如果是文件,判断后缀
不需要::SetCurrentDirectory(Path);////设置当前目录为原始目录
这个的吧