C语言遍历文件夹将其子文件分类移动到以后缀名为名的文件夹

head.h

#pragma once
#include <Windows.h>
#include <ShlObj.h>
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>
#include <string.h>
#include <iostream>
#include <io.h>
#include <errno.h>
#include <atlbase.h>
#include <string> 
#pragma comment(lib,"User32.lib")
using namespace std;
void filesearch(string path,string srcpath);

case.cpp

#include "head.h"

void filesearch(string path,string srcpath)
{
	struct _finddata_t filefind;
	string curr = path + "\\*.*";							// 修改此处改变搜索条件
	int done = 0, handle;
	if ((handle = _findfirst(curr.c_str(), &filefind)) != -1)
	{
		while (!(done = _findnext(handle, &filefind)))
		{
			if (strcmp(filefind.name, "..") == 0)
				continue;
			if ((_A_SUBDIR == filefind.attrib))				// 是目录
			{
				curr = path + "\\" + filefind.name;
				filesearch(curr,srcpath);							// 递归遍历子目录
			}
			else
			{
				char drive[_MAX_DRIVE];
				char dir[_MAX_DIR];
				char fname[_MAX_FNAME];
				char ext[_MAX_EXT];
				curr = path + "\\" + filefind.name;
				_splitpath_s(curr.c_str(), drive, dir, fname, ext);
				char *ptrext;
				ptrext = ext;
				printf("filename = %s\n", ptrext + 1);
				//定义一个要创建的文件夹的完整路径
				string newfile = srcpath + "\\" + (ptrext+1);
				CreateDirectory(newfile.c_str(),NULL);
				string newfilename = newfile + "\\" + filefind.name;
				MoveFile(curr.c_str(), newfilename.c_str());
			}
		}
		_findclose(handle);
	}
}

main.cpp

#include "head.h"


int main(int argc, LPCWSTR argv[])
{
	BROWSEINFO bi;
	ZeroMemory(&bi, sizeof(BROWSEINFO));
	LPMALLOC pMalloc;
	LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
	//定义用户选择的初始路径
	TCHAR srcpath[MAX_PATH];
	if (pidl != NULL)
	{
		SHGetPathFromIDList(pidl, srcpath);
		printf("你所选择的路径为:%s\n", srcpath);
		char *path = srcpath;
		if (SUCCEEDED(SHGetMalloc(&pMalloc)))//pidl指向的对象用完应该释放
		{
			//在此处添加代码
			filesearch(path,srcpath);


			pMalloc->Free(pidl);
			pMalloc->Release();
		}
	}
	else
	{
		MessageBox(NULL, TEXT("选择为空"), TEXT("Choose"), MB_OK);
	}
	//delete srcpath;//记得用完删除
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值