#include <windows.h> #include <iostream> #include <fstream> #include <locale.h> #include "stdio.h" #include "OperatingIni.h" using namespace std; class ScanDisk //磁盘搜索类 { public: ScanDisk(TCHAR *Expansion,TCHAR *FileName);//构造函数 ~ScanDisk(); TCHAR DriveString[MAX_PATH];// 驱动器列表 TCHAR Driver[MAX_PATH];//驱动器名 TCHAR Expansion[MAX_PATH];//后缀名 TCHAR FileName[MAX_PATH];//构造函数使用生成的文件名 TCHAR Name[MAX_PATH];//还未传送的文件路径 TCHAR ConfigName[MAX_PATH];//要使用的配置文件名 DWORD count;//文件个数 DWORD Transform_count;//已传送文件个数 CIniReader *Reader; CIniWriter *Writer; FILE *fp;//文件指针,创建路径文件 public: TCHAR * GetFirstFile();//得到第一个文件路径 //void ModifyPath(TCHAR *path);//修改路径字符串 void SearchforAllDriver(); //搜索所有驱动器 void GetDriverList();//得到驱动器列表 bool Search(TCHAR *Path,TCHAR *File);//递归搜索 bool TcharMarch(TCHAR *fileName,TCHAR *Extension);//文件后缀名匹配 void SetExpansion(TCHAR *Expansion);//设置新的文件后缀 void SetConfigName(TCHAR *ConfigName);//设置需要操作的配置文件名 void InitOperateIni(TCHAR *ConfigName);//初始化配置信息类 void GetAllExpansion();//得到所有后缀名并且检索目录写入文件 }; ScanDisk::ScanDisk(TCHAR *Expansion,TCHAR *FileName)//初始化工作 { memset(this->DriveString,0,sizeof(this->DriveString)); memset(this->Driver,0,sizeof(this->Driver)); memset(this->Expansion,0,sizeof(this->Expansion)); memset(this->FileName,0,sizeof(this->FileName)); memset(this->Name,0,sizeof(this->Name)); memset(this->ConfigName,0,sizeof(this->ConfigName)); this->count=0;//文件个数 this->Transform_count=0;//已传送文件个数为0 memcpy(this->Expansion,Expansion,wcslen(Expansion)*2); memcpy(this->FileName,FileName,wcslen(FileName)*2); //MessageBox(NULL,this->FileName,NULL,MB_OK); // MessageBox(NULL,this->Expansion,NULL,MB_OK); } ScanDisk::~ScanDisk() { fclose(this->fp); } void ScanDisk::SetExpansion(TCHAR *Expansion) { memset(this->Expansion,0,sizeof(this->Expansion)); memcpy(this->Expansion,Expansion,wcslen(Expansion)*2); } void ScanDisk::SetConfigName(TCHAR *ConfigName) { memset(this->ConfigName,0,sizeof(this->ConfigName)); memcpy(this->ConfigName,ConfigName,wcslen(ConfigName)*2); } void ScanDisk::InitOperateIni(TCHAR *ConfigName) { memset(this->ConfigName,0,sizeof(this->ConfigName)); memcpy(this->ConfigName,ConfigName,wcslen(ConfigName)*2); this->Writer=new CIniWriter(this->ConfigName); this->Reader=new CIniReader(this->ConfigName); (this->Writer)->WriteInteger(L"Setting",L"count",this->count); (this->Writer)->WriteInteger(L"Setting",L"Transform_count",this->Transform_count); } void ScanDisk::GetAllExpansion()//读取配置文件中的每一个后缀名,遍历磁盘写入文件 { TCHAR *expansion=(this->Reader)->ReadString(L"Setting", L"extension", L"");//此处设计不是很好 int length=lstrlen(expansion)+1;//没有斜杠零 int i=0; TCHAR temp[MAX_PATH]={0}; for (int j=0;j<length;j++) { if (((*expansion)!=L',')&&((*expansion)!=L'\0')) { memcpy(&temp[i],expansion,sizeof(TCHAR)); temp[i++]; expansion++; } if (((*expansion)==L',')||((*expansion)==L'\0')) { temp[i]=L'\0'; this->SetExpansion(temp); this->SearchforAllDriver(); if ((*expansion)==L'\0') { break; } expansion++; i=0; memset(temp,0,sizeof(temp)); } } } TCHAR * ScanDisk::GetFirstFile() { DWORD number=(this->Reader)->ReadInteger(L"Setting",L"Transform_count",this->Transform_count);//看看读到第几个文件了 this->fp=_wfopen(this->FileName,L"r"); //读的方式打开 if(!this->fp) { cout<<L"Can not open the .txt file"<<endl; } else { cout<<"the file is opened !"<<endl; } //TCHAR path[MAX_PATH]={0}; for (int i=0;i<=number;i++) { fgetws(this->Name,MAX_PATH,this->fp);// } //fgetws(this->Name,MAX_PATH,this->fp);// this->Name[lstrlen(this->Name)-1]=0;//去掉文件最后的0A wprintf(this->Name); //MessageBox(NULL,this->Name,NULL,MB_OK); this->Transform_count++; (this->Writer)->WriteInteger(L"Setting",L"Transform_count",this->Transform_count); fclose(this->fp); return this->Name; } void ScanDisk::SearchforAllDriver() { memset(this->Driver,0,sizeof(this->Driver)); this->GetDriverList(); int driverCount=0; TCHAR * pDrive= this->DriveString; while( *pDrive ) { pDrive += wcslen( pDrive ) + 1; driverCount++; } // printf("%d\n",driverCount);//总共几个驱动器 pDrive= this->DriveString; this->fp=_wfopen(this->FileName,L"a+"); //追加的方式打开 if(!this->fp) { cout<<L"Can not open the .txt file"<<endl; } else { cout<<"the file is opened !"<<endl; } //for (int i=0;i<driverCount;i++) //{ while( * pDrive ) { memcpy(this->Driver,pDrive,wcslen(this->DriveString)+1);//控制字符长度,和缓冲区 //MessageBox(NULL,this->Driver,NULL,MB_OK); this->Search(this->Driver,this->Expansion); fflush(this->fp); pDrive=pDrive+wcslen(pDrive)+1; } //} (this->Writer)->WriteInteger(L"Setting",L"count",this->count); } void ScanDisk::GetDriverList() { TCHAR DriveString[MAX_PATH]; // 前一个字节为令牌,后面的52字节为驱动器跟相关属性 GetLogicalDriveStrings(sizeof(DriveString), DriveString); memcpy(this->DriveString,DriveString,sizeof(this->DriveString)); } bool ScanDisk::TcharMarch(TCHAR *fileName,TCHAR *Extension)//文件后缀名匹配 { int length_of_ext=wcslen(Extension); int length_of_name=wcslen(fileName); int i=0; while(i<length_of_ext) { if (fileName[i+(length_of_name-length_of_ext)]!=Extension[i]) { return false; } else i++; } return true; } bool ScanDisk::Search(TCHAR *Path,TCHAR *File) { HANDLE hFind; WIN32_FIND_DATA wfd; ZeroMemory(&wfd,sizeof(WIN32_FIND_DATA)); TCHAR PathTemp[MAX_PATH]; memset(PathTemp,0,sizeof(PathTemp)); swprintf(PathTemp,L"%s\\*.*",Path); hFind=FindFirstFile(PathTemp,&wfd); if(INVALID_HANDLE_VALUE==hFind) { //MessageBox(NULL,L"INVALID_HANDLE_VALUE",L"FindFirstFile",MB_OK); return false; } do { if('.'==wfd.cFileName[0]) { continue; } if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { swprintf(PathTemp,L"%s\\%s",Path,wfd.cFileName); //MessageBox(NULL,PathTemp,"Directory",MB_OK); //wprintf(PathTemp); //printf("\n"); Search(PathTemp,File); fflush(this->fp); } else { if (TcharMarch(wfd.cFileName,File)) { swprintf(PathTemp,L"%s\\%s",Path,wfd.cFileName); //MessageBox(NULL,L"Found",PathTemp,MB_OK); //printf(PathTemp); //wprintf(PathTemp); //printf("\n"); /////////////////////////////////////////////////////////////////////////////////// // TCHAR temp[MAX_PATH]; // memcpy(temp," ",sizeof(temp)); // temp[MAX_PATH-2]=L'\0'; // memcpy(temp,PathTemp,lstrlen(PathTemp)*2); ////////////////////////////////////////////////////////////////////////////////// fwprintf(this->fp,L"%s",PathTemp); fwprintf(this->fp,L"\n"); this->count++;//文件个数加1 } } }while(FindNextFile(hFind,&wfd)); FindClose(hFind); return true; }
这个代码必须在unicode工程下使用,支持中文路径,
"OperatingIni.h"//这个头文件,在我的空间代码里