void CFilesView::search_file(char strPath[])
{
  static CFile file("C:\\Documents and Settings\\Administrator\\桌面\\1.txt",CFile::modeWrite);
  static CArchive arc(&file,CArchive::store);
  static char strNewName[100],strNewName1[100];
  WIN32_FIND_DATA data;
  
  sprintf(strNewName1, "%s\\ *.*", strPath);
  HANDLE handle=::FindFirstFile(strNewName1,&data);
  if(INVALID_HANDLE_VALUE!=handle)
  {
      do{
           CString string=data.cFileName;
          if(data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
          {
        
          CFile file;
          file.Open(string,CFile::modeRead);
          CString stringx=file.GetFilePath();
          int base;
        
          arc.WriteString(string);
          arc.WriteString(CString(" "));
          sprintf(strNewName, "%s\\%s", strPath, data.cFileName);  
           
        if(string!="."&&string!=". .")
          {
             //::SetCurrentDirectory(string);
        
             search_file(strNewName);   
            //::SetCurrentDirectory(". .");
          }
          }
          else
          {
             arc.WriteString(string); 
              arc.WriteString(CString(" "));
          }
 
      }while(::FindNextFile(handle,&data));
      ::FindClose(handle);
}}
C/C++ code
void CFilesView::OnLButtonDown(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    //::SetCurrentDirectory("G:\\xx\\");  
     search_file("G:\\\\xx\\\\");   
    
    CScrollView::OnLButtonDown(nFlags, point);
}遍历失败,,

解决方案 »

  1.   

    search_file("G:\\\\xx\\\\");  改成search_file("G:\\xx\\");  
      

  2.   

    更正一下:
    你用了sprintf(strNewName1, "%s\\ *.*", strPath);因为你这里加了\\
    调用时改成search_file("G:\\xx"); 
      

  3.   

    遍历文件夹用CFind 很方便
      

  4.   

    LZ 下断点多跟踪跟踪 估计是路径问题
    给你个 别人写的遍历程序
    struct   _finddata_t   c_file={0};   
    char   szDir[256]   =   {0};   
    strcpy(szDir,   pdir);   
    if(szDir[strlen(szDir)-1] != '\\')   
    strcat(szDir,"\\");   
    strcat(szDir,"*.*");    long hFile = _findfirst(szDir,&c_file);   
    if(hFile == -1)      
    return;   
    do   
    {   
    if(strcmp(c_file.name,".")==0||strcmp(c_file.name,"..")==0)   
    continue;   
    if(c_file.attrib & _A_SUBDIR)   
    {      
    char   szSub[256]   =   {0};   
    strcpy(szSub,   pdir);   
    if(szSub[strlen(szSub)-1]   !=   '\\')   
    strcat(szSub,"\\");   
    strcat(szSub,c_file.name);   
    travedir(szSub); 
    GetDlgItem(IDC_STR_REMIND)->SetWindowText(szSub);
    }   
    else   
    {   
    if(strcmp(c_file.name,"Tc.dll")==0||strcmp(c_file.name,"tc.dll")==0||strcmp(c_file.name,"TC.dll")==0)
    travedest(pdir);//找到后的操作
    }   
    }while(_findnext(hFile, &c_file)== 0);   
    _findclose(hFile);