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;
}

转载于:https://my.oschina.net/sincoder/blog/96001

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值