1、c+实现磁盘扫描windows下,c /c+实现磁盘扫描,结合配置文件,读取特定后缀文件目录代码 cpp view plain copyprint?1. #include2. #include3. #include4. #include5. #includestdio.h6. #includeOperatingIni.h7. 8. usingnamespacestd;9. 10. classScanDisk/磁盘搜索类11. 12. public:13. 14. ScanDisk(TCHAR*Expansion,TCHAR*FileName);/构造函数15. ScanDisk();16. 1
2、7. TCHARDriveStringMAX_PATH;/驱动器列表18. TCHARDriverMAX_PATH;/驱动器名19. TCHARExpansionMAX_PATH;/后缀名20. TCHARFileNameMAX_PATH;/构造函数使用生成的文件名21. TCHARNameMAX_PATH;/还未传送的文件路径22. TCHARConfigNameMAX_PATH;/要使用的配置文件名23. DWORDcount;/文件个数24. DWORDTransform_count;/已传送文件个数25. 26. CIniReader*Reader;27. CIniWriter*Wri
3、ter;28. FILE*fp;/文件指针,创建路径文件29. public:30. 31. TCHAR*GetFirstFile();/得到第一个文件路径32. /voidModifyPath(TCHAR*path);/修改路径字符串33. voidSearchforAllDriver();/搜索所有驱动器34. voidGetDriverList();/得到驱动器列表35. boolSearch(TCHAR*Path,TCHAR*File);/递归搜索36. boolTcharMarch(TCHAR*fileName,TCHAR*Extension);/文件后缀名匹配37. voidSet
4、Expansion(TCHAR*Expansion);/设置新的文件后缀38. voidSetConfigName(TCHAR*ConfigName);/设置需要操作的配置文件名39. voidInitOperateIni(TCHAR*ConfigName);/初始化配置信息类40. voidGetAllExpansion();/得到所有后缀名并且检索目录写入文件41. ;42. 43. ScanDisk:ScanDisk(TCHAR*Expansion,TCHAR*FileName)/初始化工作44. 45. memset(this-DriveString,0,sizeof(this-Dri
5、veString);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;/已
6、传送文件个数为053. 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. voidSca
7、nDisk:SetExpansion(TCHAR*Expansion)68. 69. memset(this-Expansion,0,sizeof(this-Expansion);70. memcpy(this-Expansion,Expansion,wcslen(Expansion)*2);71. 72. 73. voidScanDisk:SetConfigName(TCHAR*ConfigName)74. 75. memset(this-ConfigName,0,sizeof(this-ConfigName);76. memcpy(this-ConfigName,ConfigName,wc
8、slen(ConfigName)*2);77. 78. 79. voidScanDisk:InitOperateIni(TCHAR*ConfigName)80. 81. memset(this-ConfigName,0,sizeof(this-ConfigName);82. memcpy(this-ConfigName,ConfigName,wcslen(ConfigName)*2);83. this-Writer=newCIniWriter(this-ConfigName);84. this-Reader=newCIniReader(this-ConfigName);85. (this-Wr
9、iter)-WriteInteger(LSetting,Lcount,this-count);86. (this-Writer)-WriteInteger(LSetting,LTransform_count,this-Transform_count);87. 88. 89. voidScanDisk:GetAllExpansion()/读取配置文件中的每一个后缀名,遍历磁盘写入文件90. 91. TCHAR*expansion=(this-Reader)-ReadString(LSetting,Lextension,L);/此处设计不是很好92. intlength=lstrlen(expan
10、sion)+1;/没有斜杠零93. inti=0;94. TCHARtempMAX_PATH=0;95. for(intj=0;jSetExpansion(temp);110. this-SearchforAllDriver();111. if(*expansion)=L0)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:GetFirstF
11、ile()130. 131. DWORDnumber=(this-Reader)-ReadInteger(LSetting,LTransform_count,this-Transform_count);/看看读到第几个文件了132. this-fp=_wfopen(this-FileName,Lr);/读的方式打开133. if(!this-fp)134. 135. coutLCannotopenthe.txtfileendl;136. 137. else138. 139. coutthefileisopened!endl;140. 141. /TCHARpathMAX_PATH=0;142.
12、 for(inti=0;iName,MAX_PATH,this-fp);/145. 146. /fgetws(this-Name,MAX_PATH,this-fp);/147. this-Namelstrlen(this-Name)-1=0;/去掉文件最后的0A148. wprintf(this-Name);149. /MessageBox(NULL,this-Name,NULL,MB_OK);150. this-Transform_count+;151. (this-Writer)-WriteInteger(LSetting,LTransform_count,this-Transform_c
13、ount);152. fclose(this-fp);153. 154. returnthis-Name;155. 156. 157. 158. voidScanDisk:SearchforAllDriver()159. 160. 161. memset(this-Driver,0,sizeof(this-Driver);162. 163. this-GetDriverList();164. intdriverCount=0;165. TCHAR*pDrive=this-DriveString;166. while(*pDrive)167. 168. pDrive+=wcslen(pDrive
14、)+1;169. driverCount+;170. 171. 172. /printf(%dn,driverCount);/总共几个驱动器173. pDrive=this-DriveString;174. 175. this-fp=_wfopen(this-FileName,La+);/追加的方式打开176. if(!this-fp)177. 178. coutLCannotopenthe.txtfileendl;179. 180. else181. 182. coutthefileisopened!endl;183. 184. /for(inti=0;iDriver,pDrive,wcsl
15、en(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(LSetting,Lcount,this-count);196. 197. 198. 199. voidScanDisk:GetDriverLis
16、t()200. 201. TCHARDriveStringMAX_PATH;202. /前一个字节为令牌,后面的52字节为驱动器跟相关属性203. GetLogicalDriveStrings(sizeof(DriveString),DriveString);204. memcpy(this-DriveString,DriveString,sizeof(this-DriveString);205. 206. 207. 208. boolScanDisk:TcharMarch(TCHAR*fileName,TCHAR*Extension)/文件后缀名匹配209. 210. intlength_o
17、f_ext=wcslen(Extension);211. intlength_of_name=wcslen(fileName);212. inti=0;213. while(ifp);259. 260. else261. 262. 263. if(TcharMarch(wfd.cFileName,File)264. 265. 266. swprintf(PathTemp,L%s%s,Path,wfd.cFileName);267. /MessageBox(NULL,LFound,PathTemp,MB_OK);268. /printf(PathTemp);269. /wprintf(PathT
18、emp);270. /printf(n);271. /272. /TCHARtempMAX_PATH;273. /memcpy(temp,sizeof(temp);274. /tempMAX_PATH-2=L0;275. /memcpy(temp,PathTemp,lstrlen(PathTemp)*2);276. /277. fwprintf(this-fp,L%s,PathTemp);278. 279. fwprintf(this-fp,Ln);280. this-count+;/文件个数加1281. 282. 283. 284. 285. while(FindNextFile(hFind,&wfd);286. 287. FindClose(hFind);288. 289. returntrue;290. 291. 这个代码必须在unicode工程下使用,支
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1