1. 程式人生 > >windows API遞迴遍歷資料夾下所有檔案

windows API遞迴遍歷資料夾下所有檔案

1.網上有些程式碼有問題,改進如下

#include <stdio.h>
#include<windows.h>
#include<iostream>
#include<string>
using namespace std;
int count = 0;
void find(char * lpPath)
{
	char save_path[200];
	char szFile[MAX_PATH] = {0};
	char szFind[MAX_PATH];
	char root[MAX_PATH];

	WIN32_FIND_DATA FindFileData;
	strcpy(szFind,lpPath);

	strcat(szFind,"*.*");
	HANDLE hFind=::FindFirstFile(szFind,&FindFileData);


	if(INVALID_HANDLE_VALUE == hFind) return;
	while(TRUE)
	{
		if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			if(FindFileData.cFileName[0]!='.')
			{
				strcpy(szFile,lpPath);
				//strcat(szFile,"//");
				strcat(szFile,FindFileData.cFileName);
				strcat(szFile,"//");
				find(szFile);
			}
		}
		else
		{
			
			strcpy(root, lpPath);
			strcat(root,FindFileData.cFileName);
			cout << root << endl;
		}
	
	
		if(!FindNextFile(hFind,&FindFileData)) break;
	}
	FindClose(hFind);
}
void main()
{


	find("..//2003//");


}