Folder traversal in Windows (2)

Source: Internet
Author: User

Modified the compile program to output the running result in a file name named ". txt;
The running result is changed to the tree output mode.

# Include <iostream>
# Include <iomanip>
# Include <fstream>
# Include <cstdio>
# Include <stack>

# Include <windows. h>

Using namespace STD;

/*
* Traverse all files and folders in lpszpath and display the content in sequence.
*/
/*
* If a folder is scanned, it is saved to the dirs queue and the name is displayed,
* If a file is scanned, its name is displayed;
* After the current folder is processed, process the next level folder.
* Column.
*/

Void function (maid, ostream & out)
{
// Start searching;
Stack <tchar *> dirs;
Stack <int> dirdepth;

Tchar * TMP = new tchar [lstrlen (lpszpath) + 1];
Lstrcpy (TMP, lpszpath );

If (TMP [lstrlen (TMP)-1] = '//')
TMP [lstrlen (TMP)-1] = '/0 ';

Tchar szfind [max_path * 2];
Tchar szfile [max_path * 2];
Tchar * curdir;
Int curdepth = 1; // The depth of the current folder

Dirs. Push (TMP );
Dirdepth. Push (curdepth );

For (;! Dirs. Empty ();)
{
Curdir = dirs. Top ();
Curdepth = dirdepth. Top ();
Dirs. Pop ();
Dirdepth. Pop ();

Lstrcpy (szfind, curdir );
Lstrcat (szfind, "// *. *"); // find all files
Win32_find_data WFD;

Handle hfind = findfirstfile (szfind, & WFD );

If (hfind! = Invalid_handle_value) // If you find
{
If (curdepth> 1)
Out <"";
For (INT I = 1; I <curdepth-1; ++ I)
Out <'|' <"";
Out <'+' <curdir <Endl;

Do
{

If (WFD. cfilename [0] = '.')
Continue; // filter the "." ".." Directories

If (WFD. dwfileattributes & file_attribute_directory)
{
Wsprintf (szfile, "% S // % s", curdir, WFD. cfilename );

// Function (szfile); // If a directory is found, go to this directory for Recursion
Tchar * TMP = new tchar [lstrlen (szfile) + 2];
Lstrcpy (TMP, szfile );
Dirs. Push (TMP );
Dirdepth. Push (curdepth + 1 );
}
Else
{
// Output file name
Out <"";
For (INT I = 1; I <curdepth; ++ I)
Out <'|' <"";
Out <WFD. cfilename <Endl;
}
} While (findnextfile (hfind, & WFD ));

} // If

Delete [] curdir;

Findclose (hfind); // close the search handle

} // ()
}

Int main (INT argc, char * argv [])
{
Ofstream fout ("traversal result .txt ");
If (argc <= 1)
{
Cerr <Endl <"folder traversal, enter the path :";
Tchar path [max_path];
Cin> path;
Function (path, fout );
}
Else
{
Function (argv [1], fout );
}

Return 0;
}

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.