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