#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <stack>
#include <windows.h>
using
namespace
std;
void
function(
LPCTSTR
lpszPath,ostream & out)
{
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;
Dirs.push(tmp);
DirDepth.push(curdepth);
for
(;!Dirs.empty();)
{
curdir=Dirs.top();
curdepth=DirDepth.top();
Dirs.pop();
DirDepth.pop();
lstrcpy(szFind,curdir);
lstrcat(szFind,
"\\*.*"
);
WIN32_FIND_DATA wfd;
HANDLE
hFind = FindFirstFile(szFind, &wfd);
if
(hFind != INVALID_HANDLE_VALUE)
{
if
(curdepth >1) out<<
" "
;
for
(
int
i=1;i<curdepth-1;++i) out<<
'|'
<<
" "
;
out<<
'+'
<<curdir<<endl;
do
{
if
(wfd.cFileName[0] ==
'.'
)
continue
;
if
(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
wsprintf(szFile,
"%s\\%s"
, curdir, wfd.cFileName);
TCHAR
* tmp=
new
TCHAR
[lstrlen(szFile)+2];
lstrcpy(tmp,szFile);
Dirs.push(tmp);
DirDepth.push(curdepth+1);
}
else
{
out<<
" "
;
for
(
int
i=1;i<curdepth;++i)
out<<
'|'
<<
" "
;
out<<wfd.cFileName<<endl;
}
}
while
(FindNextFile(hFind, &wfd));
}
delete
[] curdir;
curdir = NULL;
FindClose(hFind);
}
}
int
main(
int
argc,
char
*argv[])
{
ofstream fout(
"遍历结果.txt"
);
if
(argc<=1)
{
cerr<<endl<<
"文件夹遍历,请输入路径:"
;
TCHAR
path[MAX_PATH];
cin>>path;
function(path,fout);
}
else
{
function(argv[1],fout);
}
return
0;
}