c++实现磁盘扫描.docx
- 文档编号:8064952
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:18
- 大小:1.47MB
c++实现磁盘扫描.docx
《c++实现磁盘扫描.docx》由会员分享,可在线阅读,更多相关《c++实现磁盘扫描.docx(18页珍藏版)》请在冰豆网上搜索。
c++实现磁盘扫描
windows下,c/c++实现磁盘扫描,结合配置文件,读取特定后缀文件目录代码
[cpp]viewplaincopy
print?
1.#include
2.#include
3.#include
4.#include
5.#include "stdio.h"
6.#include "OperatingIni.h"
7.
8.using namespace std;
9.
10.class ScanDisk //磁盘搜索类
11.{
12.public:
13.
14. ScanDisk(TCHAR *Expansion,TCHAR *FileName);//构造函数
15. ~ScanDisk();
16.
17. TCHAR DriveString[MAX_PATH];// 驱动器列表
18. TCHAR Driver[MAX_PATH];//驱动器名
19. TCHAR Expansion[MAX_PATH];//后缀名
20. TCHAR FileName[MAX_PATH];//构造函数使用生成的文件名
21. TCHAR Name[MAX_PATH];//还未传送的文件路径
22. TCHAR ConfigName[MAX_PATH];//要使用的配置文件名
23. DWORD count;//文件个数
24. DWORD Transform_count;//已传送文件个数
25.
26. CIniReader *Reader;
27. CIniWriter *Writer;
28. FILE *fp;//文件指针,创建路径文件
29.public:
30.
31.TCHAR * GetFirstFile();//得到第一个文件路径
32.//void ModifyPath(TCHAR *path);//修改路径字符串
33.void SearchforAllDriver(); //搜索所有驱动器
34.void GetDriverList();//得到驱动器列表
35.bool Search(TCHAR *Path,TCHAR *File);//递归搜索
36.bool TcharMarch(TCHAR *fileName,TCHAR *Extension);//文件后缀名匹配
37.void SetExpansion(TCHAR *Expansion);//设置新的文件后缀
38.void SetConfigName(TCHAR *ConfigName);//设置需要操作的配置文件名
39.void InitOperateIni(TCHAR *ConfigName);//初始化配置信息类
40.void GetAllExpansion();//得到所有后缀名并且检索目录写入文件
41.};
42.
43.ScanDisk:
:
ScanDisk(TCHAR *Expansion,TCHAR *FileName)//初始化工作
44.{
45. memset(this->DriveString,0,sizeof(this->DriveString));
46. memset(this->Driver,0,sizeof(this->Driver));
47. memset(this->Expansion,0,sizeof(this->Expansion));
48. memset(this->FileName,0,sizeof(this->FileName));
49. memset(this->Name,0,sizeof(this->Name));
50. memset(this->ConfigName,0,sizeof(this->ConfigName));
51. this->count=0;//文件个数
52. this->Transform_count=0;//已传送文件个数为0
53.
54. memcpy(this->Expansion,Expansion,wcslen(Expansion)*2);
55. memcpy(this->FileName,FileName,wcslen(FileName)*2);
56. //MessageBox(NULL,this->FileName,NULL,MB_OK);
57.// MessageBox(NULL,this->Expansion,NULL,MB_OK);
58.
59.}
60.
61.ScanDisk:
:
~ScanDisk()
62.{
63. fclose(this->fp);
64.
65.}
66.
67.void ScanDisk:
:
SetExpansion(TCHAR *Expansion)
68.{
69. memset(this->Expansion,0,sizeof(this->Expansion));
70. memcpy(this->Expansion,Expansion,wcslen(Expansion)*2);
71.}
72.
73.void ScanDisk:
:
SetConfigName(TCHAR *ConfigName)
74.{
75. memset(this->ConfigName,0,sizeof(this->ConfigName));
76. memcpy(this->ConfigName,ConfigName,wcslen(ConfigName)*2);
77.}
78.
79.void ScanDisk:
:
InitOperateIni(TCHAR *ConfigName)
80.{
81. memset(this->ConfigName,0,sizeof(this->ConfigName));
82. memcpy(this->ConfigName,ConfigName,wcslen(ConfigName)*2);
83. this->Writer=new CIniWriter(this->ConfigName);
84. this->Reader=new CIniReader(this->ConfigName);
85. (this->Writer)->WriteInteger(L"Setting",L"count",this->count);
86. (this->Writer)->WriteInteger(L"Setting",L"Transform_count",this->Transform_count);
87.}
88.
89.void ScanDisk:
:
GetAllExpansion()//读取配置文件中的每一个后缀名,遍历磁盘写入文件
90.{
91. TCHAR *expansion=(this->Reader)->ReadString(L"Setting", L"extension", L"");//此处设计不是很好
92. int length=lstrlen(expansion)+1;//没有斜杠零
93. int i=0;
94. TCHAR temp[MAX_PATH]={0};
95. for (int j=0;j 96. { 97. 98. if (((*expansion)! =L',')&&((*expansion)! =L'\0')) 99. { 100. memcpy(&temp[i],expansion,sizeof(TCHAR)); 101. temp[i++]; 102. expansion++; 103. } 104. 105. if (((*expansion)==L',')||((*expansion)==L'\0')) 106. { 107. 108. temp[i]=L'\0'; 109. this->SetExpansion(temp); 110. this->SearchforAllDriver(); 111. if ((*expansion)==L'\0') 112. { 113. break; 114. } 115. expansion++; 116. i=0; 117. memset(temp,0,sizeof(temp)); 118. 119. } 120. 121. } 122. 123. 124. 125. 126.} 127. 128. 129.TCHAR * ScanDisk: : GetFirstFile() 130.{ 131. DWORD number=(this->Reader)->ReadInteger(L"Setting",L"Transform_count",this->Transform_count);//看看读到第几个文件了 132. this->fp=_wfopen(this->FileName,L"r"); //读的方式打开 133. if(! this->fp) 134. { 135. cout< 136. } 137. else 138. { 139. cout<<"the file is opened ! "< 140. } 141. //TCHAR path[MAX_PATH]={0}; 142. for (int i=0;i<=number;i++) 143. { 144. fgetws(this->Name,MAX_PATH,this->fp);// 145. } 146. //fgetws(this->Name,MAX_PATH,this->fp);// 147. this->Name[lstrlen(this->Name)-1]=0;//去掉文件最后的0A 148. wprintf(this->Name); 149. //MessageBox(NULL,this->Name,NULL,MB_OK); 150. this->Transform_count++; 151. (this->Writer)->WriteInteger(L"Setting",L"Transform_count",this->Transform_count); 152. fclose(this->fp); 153. 154. return this->Name; 155. 156.} 157. 158.void ScanDisk: : SearchforAllDriver() 159.{ 160. 161. memset(this->Driver,0,sizeof(this->Driver)); 162. 163. this->GetDriverList(); 164. int driverCount=0; 165. TCHAR * pDrive= this->DriveString; 166. while( *pDrive ) 167. { 168. pDrive += wcslen( pDrive ) + 1; 169. driverCount++; 170. 171. } 172.// printf("%d\n",driverCount);//总共几个驱动器 173. pDrive= this->DriveString; 174. 175. this->fp=_wfopen(this->FileName,L"a+"); //追加的方式打开 176. if(! this->fp) 177. { 178. cout< 179. } 180. else 181. { 182. cout<<"the file is opened ! "< 183. } 184. //for (int i=0;i 185. //{ 186. while( * pDrive ) 187. { 188. memcpy(this->Driver,pDrive,wcslen(this->DriveString)+1);//控制字符长度,和缓冲区 189. //MessageBox(NULL,this->Driver,NULL,MB_OK); 190. this->Search(this->Driver,this->Expansion); 191. fflush(this->fp); 192. pDrive=pDrive+wcslen(pDrive)+1; 193. } 194. //} 195. (this->Writer)->WriteInteger(L"Setting",L"count",this->count); 196. 197.} 198. 199.void ScanDisk: : GetDriverList() 200.{ 201. TCHAR DriveString[MAX_PATH]; 202. // 前一个字节为令牌,后面的52字节为驱动器跟相关属性 203. GetLogicalDriveStrings(sizeof(DriveString), DriveString); 204. memcpy(this->DriveString,DriveString,sizeof(this->DriveString)); 205.} 206. 207. 208.bool ScanDisk: : TcharMarch(TCHAR *fileName,TCHAR *Extension)//文件后缀名匹配 209.{ 210. int length_of_ext=wcslen(Extension); 211. int length_of_name=wcslen(fileName); 212. int i=0; 213. while(i 214. { 215. if (fileName[i+(length_of_name-length_of_ext)]! =Extension[i]) 216. { 217. return false; 218. } 219. else 220. i++; 221. } 222. return true; 223. 224.} 225. 226.bool ScanDisk: : Search(TCHAR *Path,TCHAR *File) 227.{ 228. HANDLE hFind; 229. WIN32_FIND_DATA wfd; 230. 231. ZeroMemory(&wfd,sizeof(WIN32_FIND_DATA)); 232. TCHAR PathTemp[MAX_PATH]; 233. memset(PathTemp,0,sizeof(PathTemp)); 234. 235. swprintf(PathTemp,L"%s\\*.*",Path); 236. hFind=FindFirstFile(PathTemp,&wfd); 237. 238. if(INVALID_HANDLE_VALUE==hFind) 239. { 240. //MessageBox(NULL,L"INVALID_HANDLE_VALUE",L"FindFirstFile",MB_OK); 241. return false; 242. } 243. 244. do 245. { 246. if('.'==wfd.cFileName[0]) 247. { 248. continue; 249. } 250. 251. if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 252. { 253. swprintf(PathTemp,L"%s\\%s",Path,wfd.cFileName); 254. //MessageBox(NULL,PathTemp,"Directory",MB_OK); 255. //wprintf(PathTemp); 256. //printf("\n"); 257. Search(PathTemp,File); 258. fflush(this->fp); 259. } 260. else 261. { 262. 263. if (TcharMarch(wfd.cFileName,File)) 264. { 265. 266. swprintf(PathTemp,L"%s\\%s",Path,wfd.cFileName); 267. //MessageBox(NULL,L"Found",PathTemp,MB_OK); 268. //printf(PathTemp); 269. //wprintf(PathTemp); 270. //printf("\n"); 271./////////////////////////////////////////////////////////////////////////////////// 272.// TCHAR temp[MAX_PATH]; 273.// memcpy(temp," ",sizeof(temp)); 274.// temp[MAX_PATH-2]=L'\0'; 275.// memcpy(temp,PathTemp,lstrlen(PathTemp)*2); 276.////////////////////////////////////////////////////////////////////////////////// 277. fwprintf(this->fp,L"%s",PathTemp); 278. 279. fwprintf(this->fp,L"\n"); 280. this->count++;//文件个数加1 281. 282. } 283. } 284. 285. }while(FindNextFile(hFind,&wfd)); 286. 287. FindClose(hFind); 288. 289. return true; 290. 291.} 这个代码必须在unicode工程下使用,支
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 实现 磁盘 扫描