windows 遍历子目录所有文件

  放到这 以后用 直接 copy 了。。

#include <Windows.h>
#include <strsafe.h>

int clean_backslash2(char  *str)
{
	char *p;
	while(*str)
	{
		if(*str == '\\' && *(str+1) == '\\')
		{
			p = ++str;
			while(*str)
			{
				*p++ = *(str+1);
				++str;
			}
			return 1;
		}
		++str;
	}
	return 0;
}

int clean_backslash(char  *str)
{
	while(clean_backslash2(str));
}


void callback(const char *file)
{
	printf("%s \n",file);
}

int ListSubDir(const char *dir)
{
	WIN32_FIND_DATA ffd;
	LARGE_INTEGER filesize;
	char *find_string = NULL,*find_string2 = NULL;
	size_t length_of_arg;
	HANDLE hFind = INVALID_HANDLE_VALUE;
	DWORD dwError=0;
	char filename[MAX_PATH * 2];
	unsigned char md5_buff[33];
	int i;
	char md5_string[64];
	unsigned long size;
	char query_string[8192];
	char *p = NULL;

	find_string = (char *)malloc(8192);//这个长度应该够了 。。
	*find_string = 0;
	StringCchCopy(find_string, MAX_PATH, dir);
	StringCchCat(find_string, MAX_PATH, TEXT("\\*"));

	clean_backslash(find_string);

	// Find the first file in the directory.

	hFind = FindFirstFile(find_string, &ffd);
	if (INVALID_HANDLE_VALUE == hFind) 
	{
		goto out;
	} 
	do
	{
		if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			if(strcmp(ffd.cFileName,".") == 0 || 0==strcmp(ffd.cFileName,".."))
				continue;
			find_string2 = (char *)malloc(8192);
			*find_string2 = 0;
			StringCchCopy(find_string2, MAX_PATH, dir);
			StringCchCat(find_string2, MAX_PATH, "\\");
			StringCchCat(find_string2, MAX_PATH, ffd.cFileName);
			ListSubDir(find_string2);
			free(find_string2);
		}
		else
		{
			filesize.LowPart = ffd.nFileSizeLow;
			filesize.HighPart = ffd.nFileSizeHigh;
			//printf("file : %s\\%s \n",dir,ffd.cFileName);

			filename[0] = 0;

			strcpy(filename,dir);
			strcat(filename,"\\");
			strcat(filename,ffd.cFileName);
			
			callback(filename);
		}
	}
	while (FindNextFile(hFind, &ffd) != 0);

	dwError = GetLastError();
	if (dwError != ERROR_NO_MORE_FILES) 
	{

	}
out:
	if(hFind != INVALID_HANDLE_VALUE)
		FindClose(hFind);
	if(find_string)
		free(find_string);
	return dwError;
}



int main(int argc ,char **argv)
{
	if(argc < 2)
	{
		return;
	}
	ListSubDir(argv[1]);
	return 0;
}

你可能感兴趣的:(windows 遍历子目录所有文件)