#include "windows.h"
#include <stdio.h>void SearchFiles(char * Path) 

HANDLE FindHandle; 
WIN32_FIND_DATA FindResult; 
//char lp[30]="";

if (SetCurrentDirectory(Path)) 
{
//GetCurrentDirectory(30,lp);
//printf("current path:%s\n",lp);

// 当前目录搜索 
if ((FindHandle = FindFirstFile("*", &FindResult)) != INVALID_HANDLE_VALUE) 
{    //---如果找到了就返回了
do 
{                        
printf("%s !\n",FindResult.cFileName); 

while (FindNextFile(FindHandle, &FindResult) && FindHandle  != INVALID_HANDLE_VALUE); 

FindClose(FindHandle); 

// ----没找到,则在子目录下找---
if((FindHandle = FindFirstFile("*", &FindResult)) != INVALID_HANDLE_VALUE) 

do{
if(FindResult.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 

char * DirName; 
DirName = _strupr(_strdup(FindResult.cFileName)); 
if(FindResult.cFileName[0] != '.')//Skip loops with "." and ".."  

printf("Dir:%s\n",FindResult.cFileName );
SearchFiles(FindResult.cFileName); 

free(DirName); 

}while(FindNextFile(FindHandle, &FindResult) && FindHandle  != INVALID_HANDLE_VALUE);
FindClose(FindHandle); 
} //----------End if结束子目录搜索
} //---当前目录设置
} ///////////////////////////////////////////// 
// Search fixed and network drives to infect 
///////////////////////////////////////////// 
DWORD WINAPI SearchDrives() 

BYTE CurrentDrive[] = "A:\\"; 
DWORD DriveType; 
int i;   for (i=0; i<26; i++) 
  {  
      CurrentDrive[0] = 'A' + i; 
      DriveType = GetDriveType((const char *)CurrentDrive);  //---------得到驱动器---
      
      if ((DriveType == DRIVE_FIXED)) //|| (DriveType == DRIVE_REMOTE) 
      { 
        SearchFiles((char *)CurrentDrive); 
      } //--end if
  } //---end for
  return 1; 
} int main(int argc, char* argv[])
{
SearchDrives();
return 0;
}

解决方案 »

  1.   

    int Search_Directory(char* szDirName, char* cSpecFile="*.*")
    {
    //表示文件(或目录)的信息 
    struct _finddata_t filestruct;
    int iCounts=0;
    //进入要查找的路径(也可为某一具体的目录) 
    if (_chdir(szDirName) != 0 ) return iCounts; //如果目录不存在,则返回0
    //char cFileName[MAX_PATH]=""; //定义一个文件全名的变量
    //开始查找工作, 找到当前目录下的第一个实体(文件或子目录), 
    //″*″表示查找任何的文件或子目录, filestruct为查找结果 
    long handle = _findfirst(cSpecFile, &filestruct);
    //如果handle为-1, 表示当前目录为空, 则结束查找而返回
    if ( handle == -1 ) return iCounts;
    //检查找到的第一个实体是否是一个目录(filestruct.name为其名称) 
    if( ::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY ) 
    {
    if( filestruct.name[0] != '.') //如果当前为目录,则检查当前名称是否为"."
    iCounts += Search_Directory(szDirName); //调用递归函数
    }
    else //查找到了文件
    {
    iCounts ++;
    //strcpy(cFileName, szDirName);
    //strcat(cFileName, "\\");
    //strcat(cFileName, filestruct.name);
    //AfxMessageBox(cFileName);
    }
    //循环,继续对当前目录中的下一个子目录或文件进行与上面同样的查找 
    while(!(_findnext(handle, &filestruct))) 

    if ( ::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY )

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

    iCounts += Search_Directory(szDirName); 


    else //查找到了文件
    {
    iCounts ++;
    //strcpy(cFileName, szDirName);
    //strcat(cFileName, "\\");
    //strcat(cFileName, filestruct.name);
    //AfxMessageBox(cFileName);
    }
    }
    _findclose(handle); //关闭文件句柄
    _chdir(".."); //返回上级目录 
    return iCounts; //最后结束整个查找工作,返回文件总数
    }
      

  2.   

    先把((FindHandle = FindFirstFile("*", &FindResult)) 
    改为
    ((FindHandle = FindFirstFile("*.*", &FindResult))
    试试看
      

  3.   

    先把我下面的看清楚
    void SearchDatFiles(char* Dir)
      //, AList: TStringList)
    {
      WIN32_FIND_DATA wfd;
      HANDLE hFind;
    //  int TimeLen;
      char tmp[MAX_PATH];  for(int i=0;i<MAX_PATH;i++){
      tmp[i]=0;
      }
      strcat(tmp, Dir);
      strcat(tmp, "*.dat");
      wfd.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
      if ((hFind = FindFirstFile(tmp, &wfd)) != INVALID_HANDLE_VALUE)
        do
        {
          tmp[0] = '\0';
          strcat(tmp, Dir);
          strcat(tmp, wfd.cFileName);
          //if (CheckMPEGFile(tmp, TimeLen)){
      CString filename;
      filename.Format("%s\n",tmp);
      TRACE("filename=%s\n",filename.Left(100));   //}
            ; 
        } while (FindNextFile(hFind, &wfd));
      FindClose(hFind);
    }
      

  4.   

    如何遍历目录及其子目录(自己参考着改改吧)
    void FindDirFiles(CString csDirPath, ThreadInfo *pThreadInfo)
    {
       WIN32_FIND_DATA wfd;
       HANDLE hFind;
       CString csText = _T("");   // Check if the last char is a back-slash
       // (If not, put it there)
       if (csDirPath.Right(1) != "\\")
          csDirPath += _T("\\");   // set the variable and add an astrix for 
       // the beginning of the directory search.
       csText = csDirPath + _T("*");   // Iterate through dirs
       hFind = FindFirstFile(csText, &wfd);
       if (hFind != INVALID_HANDLE_VALUE) {
          do {
             
             // Check if "." or "..", if not...
             // Check if its a directory.
             if ((strcmp(wfd.cFileName,_T("."))) && (strcmp(wfd.cFileName,_T(".."))) && 
                (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {            CString csDirIn = _T("");            // Set to the directory found.
                csDirIn = csDirPath + wfd.cFileName;            // Call this function again.
                FindDirFiles(csDirIn, pThreadInfo);
             }
          } while (FindNextFile(hFind, &wfd));
          
          // This is a MUST
          FindClose(hFind);
       }   // Iterate through files
       //
       // set the variable and add an astrix-dot-astrix "*.*"
       // for the beginning of the file search.
       csText = csDirPath + _T("*.*");
       hFind = FindFirstFile(csText, &wfd);
       if (hFind != INVALID_HANDLE_VALUE) {
          do {
             
             // If NOT a directory...
             if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {            CString csIn = _T("");
                CString csOut = _T("");            // Set to the file found.
                csIn = csDirPath + wfd.cFileName;
                
                if (!pThreadInfo->bEncrypt) {
                   csOut = csIn.Left(csIn.GetLength() - 4);
                }
                else {
                   csOut = csIn + ".enf";
                }            // Now, add the filename into the Array.
                m_csFileArrayIn.Add(csIn);
                m_csFileArrayOut.Add(csOut);
             }
          } while (FindNextFile(hFind, &wfd));      // This is a MUST
          FindClose(hFind);   }

      

  5.   

    你先看看我的:
    //函数描述:函数名Fget_filename 
    //功能:取指定目录下的所有文件,
    //参数描述:fdirectoryPath为指定路径,
    // filename_array存放取得的文件名信息的数组,
    // length数组实际存放项的长度
    //返回值描述:返回值1表示正常,-1表示出错
    short Fget_filename( char* fdirectoryPath,char* filename_array[MAX1], int& length )
    {
    WIN32_FIND_DATA data; //is FindFirstFile Parameters
    HANDLE hFind;
    int nCount=0; // 
    char filename[256];
    memset( filename, 0, 256 );
    strcpy(filename, fdirectoryPath );
    strcat(filename, "\\*.*"); //查找当前目录下的所有文件
    hFind = FindFirstFile(filename, &data);
    while( hFind != INVALID_HANDLE_VALUE )
    {
    strcpy( filename_array[nCount], data.cFileName );
    nCount++; if( !FindNextFile(hFind, &data) )
    {
    CloseHandle( hFind );
    hFind = INVALID_HANDLE_VALUE;
    }
    } length=nCount;
    return 1;
    }
    //函数描述:函数名 Fresource_driveinfo
    //功能:取驱动器信息
    //参数描述: driveInfo存放取得的驱动器统计信息数组,
    // length为数组实际存放项的
    //返回值描述:返回值1表示正常,-1表示出错
    short Fresource_driveinfo( Fdrive_info driveInfo[MAX1],int& length )
    {
    Fdrive_info DriveInfo;
    memset( &DriveInfo, 0, sizeof( Fdrive_info ) );
    DWORD dwBuffer=256;
    char szBuffer[256];
    memset( szBuffer, 0, dwBuffer );
    int nSize = GetLogicalDriveStrings( dwBuffer, szBuffer );
    if( nSize == 0 )
    {
    return -1;
    } char szDrivers[32][5];
    memset( szDrivers, 0, 160 );
    char szTemp[4];
    memset( szTemp, 0, 4 );
    int j=0;
    int nLength=0;   // 有几个盘符
    for( int i=0; i<nSize; i++ )
    {
    if( szBuffer[i] == '\0' )
    {
    szTemp[j]='\0';
    strcpy( szDrivers[nLength], szTemp ); UINT nDriver = GetDriveType( szDrivers[nLength] ); 
    // 得到驱动器的信息
    switch( nDriver )
    {
    case DRIVE_REMOVABLE:  // 是否是软驱
    break;
    case DRIVE_CDROM:
    break;
    case DRIVE_FIXED:
    strcpy( DriveInfo.drive_type, "硬盘" );
    strcat( DriveInfo.drive_type, "DRIVE_FIXED" ); //GetVolumeInformation();
    //DriveInfo.Partitiontype = ;
    //strcpy( DriveInfo.drive_id, szDrivers[nLength] );
    //DriveInfo.drive_size = ;
    //GetDiskFreeSpace( szDrivers );
    break; case DRIVE_REMOTE:
    break;
    case DRIVE_RAMDISK:
    break;
    }
    nLength++;
    j=0;
    continue;
    }
    szTemp[j]=szBuffer[i];
    j++;
    } length = nLength;
    return 1;
    }
      

  6.   

    mfc的
    FindAllFiles(CString csFolderName)
    {
      CFileFind f;
      BOOL bFind=f.FindFile(csFolderName+"\\*.*");
      while(bFind)
      {
        bFind = f.FindNextFile();
        if(f.IsDots()) continue;
        if(f.IsDirectory())
        {
          FindAllFiles(csFolderName+"\\"+f.GetFileName());
        }
    // ::SetFileAttributes(csFolderName+"\\"+f.GetFileName
    //(),FILE_ATTRIBUTE_NORMAL);
        TRACE(_T("%s\r\n"),f.GetFileName());
      }
    }
      

  7.   

    哈哈,这么多。
    主要这两天脑子很混乱,所以写的程序思维也比较紊乱。
    to :YP2002CN(老婆我不敢了,老婆我愛你) 
        你的野蛮老婆也太厉害了,不过还是野蛮的好,因为流行呀。to : wltsui(你跳,我也跳!) 
       跳什么?扑通,不见了。to: wj59(wj59)  ,RiverHill() , jennyvenus(JennyVenus) 
       谢谢了。