InBlock.gif#include <iostream>
InBlock.gif#include <windows.h>
InBlock.gif#include < string>
InBlock.gif#include <tchar.h>
InBlock.gif#include <fstream>
InBlock.gif using namespace std;
InBlock.gif
InBlock.gif#ifdef _UNICODE
InBlock.gif#define TCHAR wchar
InBlock.gif# else
InBlock.gif#define TCHAR char
InBlock.gif#endif
InBlock.gif
InBlock.gif //file stream    
InBlock.giffstream fStream;
InBlock.gif
InBlock.gif //count the total file number in the directory    
InBlock.gif static int fileCount=0;
InBlock.gif
InBlock.gif void file_scan( char* root)
InBlock.gif{
InBlock.gif    
InBlock.gif   char filePath[MAX_PATH];
InBlock.gif         char tmpPath[MAX_PATH];
InBlock.gif        
InBlock.gif  memset(filePath,0,MAX_PATH);
InBlock.gif        memset(tmpPath,0,MAX_PATH);
InBlock.gif
InBlock.gif    
InBlock.gif        WIN32_FIND_DATA fd;
InBlock.gif        memset(&fd,0, sizeof(WIN32_FIND_DATA));
InBlock.gif
InBlock.gif        HANDLE hSearch;
InBlock.gif    
InBlock.gif        strcpy(filePath, root);
InBlock.gif    
InBlock.gif        BOOL bSearchFinished = FALSE;
InBlock.gif    
InBlock.gif         if( filePath[strlen(filePath) -1] != '\\' )
InBlock.gif        {
InBlock.gif    strcat(filePath, "\\");
InBlock.gif  }
InBlock.gif    
InBlock.gif        strcat(filePath, "*");
InBlock.gif        hSearch = FindFirstFile(filePath, &fd);
InBlock.gif    
InBlock.gif        
InBlock.gif   // the file is a dir , its name should not be '.','..', and not begin with '$'
InBlock.gif         if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
InBlock.gif    && (fd.cFileName[0]!='.') &&(fd.cFileName[0]!='$') )            
InBlock.gif        {
InBlock.gif    strcpy(tmpPath, root);
InBlock.gif    strcat(tmpPath, fd.cFileName);
InBlock.gif
InBlock.gif     //recursion
InBlock.gif    file_scan(tmpPath);
InBlock.gif        }
InBlock.gif    
InBlock.gif   // normal file
InBlock.gif         else if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..")&& fd.cFileName[0]!='$')
InBlock.gif        {
InBlock.gif    cout<<root<<'\\'<<fd.cFileName<<endl;    
InBlock.gif    
InBlock.gif    fStream.write(root,strlen(root));
InBlock.gif    fStream.write("\\",1);
InBlock.gif    fStream.write(fd.cFileName,strlen(fd.cFileName));
InBlock.gif    fStream.write( "\r\n",2);
InBlock.gif
InBlock.gif    ++fileCount;
InBlock.gif        }
InBlock.gif    
InBlock.gif    
InBlock.gif         while( !bSearchFinished )
InBlock.gif        {
InBlock.gif     if( FindNextFile(hSearch, &fd) )
InBlock.gif    {
InBlock.gif       if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
InBlock.gif        && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )            
InBlock.gif      {
InBlock.gif        strcpy(tmpPath, root);
InBlock.gif        memset(tmpPath+strlen(tmpPath),0,MAX_PATH-strlen(tmpPath));
InBlock.gif         if(tmpPath[strlen(tmpPath)]!='\\')
InBlock.gif        {
InBlock.gif          tmpPath[strlen(tmpPath)]='\\';
InBlock.gif          tmpPath[strlen(tmpPath)+1]='\0';
InBlock.gif        }
InBlock.gif        
InBlock.gif        strcat(tmpPath, fd.cFileName);
InBlock.gif        file_scan(tmpPath);
InBlock.gif      }
InBlock.gif        
InBlock.gif       else if(strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..")&& fd.cFileName[0]!='$')
InBlock.gif      {
InBlock.gif        cout<<root<<'\\'<<fd.cFileName<<endl;
InBlock.gif
InBlock.gif        fStream.write(root,strlen(root));
InBlock.gif        fStream.write("\\",1);
InBlock.gif        fStream.write(fd.cFileName,strlen(fd.cFileName));
InBlock.gif        fStream.write( "\r\n",2);
InBlock.gif
InBlock.gif        ++fileCount;
InBlock.gif      }
InBlock.gif    }
InBlock.gif    
InBlock.gif     else
InBlock.gif    {
InBlock.gif       if( GetLastError() == ERROR_NO_MORE_FILES )                     //Normal Finished                        
InBlock.gif        bSearchFinished = TRUE;
InBlock.gif       else
InBlock.gif        bSearchFinished = TRUE;         //Terminate Search
InBlock.gif    }
InBlock.gif        }
InBlock.gif        FindClose(hSearch);
InBlock.gif}
InBlock.gif
InBlock.gif
InBlock.gif int main( int argc, char** argv)
InBlock.gif{
InBlock.gif   char dir[MAX_PATH];
InBlock.gif   char txtfile[MAX_PATH];
InBlock.gif  cout<< "please enter the directory to be scanned    !"<<endl;
InBlock.gif  cin>>dir;
InBlock.gif  cout<<endl<<endl;
InBlock.gif
InBlock.gif  cout<< "Please input the .txt file name (include path) to record your search result !"<<endl;
InBlock.gif  cin>>txtfile;
InBlock.gif  cout<<endl<<endl;
InBlock.gif    
InBlock.gif  fStream.open(txtfile,ios:: in | ios:: out);
InBlock.gif   if(!fStream)
InBlock.gif  {
InBlock.gif    cout<< "Can not open the .txt file"<<endl;
InBlock.gif     return 0;
InBlock.gif  }
InBlock.gif  cout<< "the file is opened !"<<endl;
InBlock.gif
InBlock.gif    
InBlock.gif  file_scan(dir);    
InBlock.gif
InBlock.gif  fStream.close();
InBlock.gif
InBlock.gif  cout<<endl<<endl;
InBlock.gif  cout<< "the total file number in "<<dir<< " : "<<fileCount<<endl;
InBlock.gif
InBlock.gif   return 0;
InBlock.gif}
因为要获得ftp服务器的所有资源信息,所以需要遍历磁盘文件。在网上看了不少代码,但都不能够完全遍历,今天写了一个,可以完全遍历磁盘的文件。
代码如下。
 
首先输入要搜索的目录:比如 D:\
然后输入生成的文本文档名(要存在)  E:\result.txt
然后回车开始运行。