Directory Traversal of Windows programming

Source: Internet
Author: User

Using Windows Api,findfirstfile and Filenextfile, the first Windows programming is to traverse all files and directories in the specified directory recursively. Code writes are very bloated and unsightly. Please forgive us!

#include <cstdio> #include <cstring> #include <iostream> #include <windows.h> #define MAXN 100005using namespace Std;void Surf (win32_find_data myData) {cout<<mydata.ftcreationtime.dwlowdatetime< <endl;cout<<myData.ftLastAccessTime.dwLowDateTime<<endl;cout<< mydata.ftlastwritetime.dwlowdatetime<<endl; SYSTEMTIME CTime, Atime, Wtime; FileTimeToSystemTime (&mydata.ftcreationtime, &ctime); FileTimeToSystemTime (&mydata.ftlastaccesstime, &atime); FileTimeToSystemTime (&mydata.ftlastwritetime, &wtime);p rintf (%d%d months%d%d seconds \ n), Ctime.wyear, Ctime.wmonth, Ctime.wday, Ctime.whour, Ctime.wminute, Ctime.wsecond);p rintf ("%d%d"%d "%d"%d%d seconds \ n ", Atime.wyear, ATI Me.wmonth, Atime.wday, Atime.whour, Atime.wminute, Atime.wsecond);p rintf ("%d-%d-%d-day%d-%d-%d-second \ n", Wtime.wyear, Wtime. Wmonth, Wtime.wday, Wtime.whour, Wtime.wminute, Wtime.wsecond); Cout<<endl<<endl;} void Traverse (char *str) {Win32_find_dataMyData; HANDLE hfind = Invalid_handle_value;char Str[max_path] = {0};strcpy (str, str); strcat (str, "/*");//Use wildcards to match hfind = Find Firstfile (str, &mydata), if (Invalid_handle_value = = hfind) return, while (FindNextFile (Hfind, &mydata)) {if ( MYDATA.CFILENAME[0]! = '. ') Do not return the folder when the next {cout<< "=========" <<myData.cFileName<< "==========" <<endl; Surf (myData); (mydata.dwfileattributes = = file_attribute_directory)//infer whether the folder {char Dir[max_path] = {0};snprintf (dir, Max_path, "%s/ %s ", Str, Mydata.cfilename);//Construction Path Traverse (dir);}}} FindClose (hfind);//close handle}int main () {char *str = "./test"; traverse (str);}




My current directory of Test directory has "haha" directory and Test.1, test, 2, test, 33 files, "haha" directory contains "AI" directory and haha.1, haha.2, haha.3 three files. "AI" directory also contains "wo" directory and AI.1, AI.2, ai.3 three files, "wo" directory has wo.1, wo.2, wo.3 three files.


Program Output Result:

=========haha==========
1556376338
2156419021
2156419021
May 31, 2014 10:16 18 seconds
May 31, 2014 10:24 27 seconds
May 31, 2014 10:24 27 seconds


=========ai==========
2147248496
772195107
772195107
May 31, 2014 10:24 27 seconds
May 31, 2014 11:26 34 seconds
May 31, 2014 11:26 34 seconds


=========ai.1==========
2197291359
2197291359
2197291359
May 31, 2014 10:24 32 seconds
May 31, 2014 10:24 32 seconds
May 31, 2014 10:24 32 seconds


=========ai.2==========
2197291359
2328198846
2328198846
May 31, 2014 10:24 32 seconds
May 31, 2014 10:24 45 seconds
May 31, 2014 10:24 45 seconds


=========ai.3==========
2197291359
2387262224
2387262224
May 31, 2014 10:24 32 seconds
May 31, 2014 10:24 51 seconds
May 31, 2014 10:24 51 seconds


=========wo==========
752914004
2835575798
2835575798
May 31, 2014 11:26 33 seconds
May 31, 2014 15:40 33 seconds
May 31, 2014 15:40 33 seconds


=========wo.1==========
806557072
806557072
806557072
May 31, 2014 11:26 38 seconds
May 31, 2014 11:26 38 seconds
May 31, 2014 11:26 38 seconds


=========wo.2==========
870010701
870010701
870010701
May 31, 2014 11:26 44 seconds
May 31, 2014 11:26 44 seconds
May 31, 2014 11:26 44 seconds


=========wo.3==========
870010701
923383754
923383754
May 31, 2014 11:26 44 seconds
May 31, 2014 11:26 50 seconds
May 31, 2014 11:26 50 seconds


=========haha.1==========
1601118898
1601118898
1601118898
May 31, 2014 10:16 22 seconds
May 31, 2014 10:16 22 seconds
May 31, 2014 10:16 22 seconds


=========haha.2==========
1601118898
1678113301
1678113301
May 31, 2014 10:16 22 seconds
May 31, 2014 10:16 30 seconds
May 31, 2014 10:16 30 seconds


=========haha.3==========
1601118898
1742136963
1742136963
May 31, 2014 10:16 22 seconds
May 31, 2014 10:16 36 seconds
May 31, 2014 10:16 36 seconds


=========test.1==========
1835930690
1835930690
1601118898
May 31, 2014 10:23 55 seconds
May 31, 2014 10:23 55 seconds
May 31, 2014 10:16 22 seconds


=========test.2==========
1924505756
1924505756
1678113301
May 31, 2014 10:24 4 seconds
May 31, 2014 10:24 4 seconds
May 31, 2014 10:16 30 seconds


=========test.3==========
1924535758
1924535758
1742136963
May 31, 2014 10:24 4 seconds
May 31, 2014 10:24 4 seconds
May 31, 2014 10:16 36 seconds



Directory traversal of Windows programming

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.