鱼C论坛

 找回密码
 立即注册
查看: 1661|回复: 8

[已解决]求助:C语言实现把一个文件夹中多个名字相同的文件分别存放到另一些文件夹内

[复制链接]
发表于 2019-5-14 15:29:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用C或C++实现,在一个文件夹中很多个名字相同的文件(一个是底库文件另一个是现场采集的文件,两两匹配),把他们名字相同的找出来放在同一个文件夹内再存放到一个大的文件夹内
比如:原文件夹内有1_10,2_10,3_10和1_20,2_20,3_20;程序实现后:1_10和1_20在一个文件夹内,2_10和2_20在一个文件夹内,3_10和3_20在一个文件夹内,最后再把他们放在一个大文件夹内;

求助,万分感谢
最佳答案
2019-5-15 12:36:01
或者用这个版本
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <io.h>
  5. #include <windows.h>
  6. using namespace std;

  7. #ifdef UNICODE

  8. #endif

  9. //连接路径
  10. string linkpath(string path, string path2 = "", string path3 = "") {
  11.         if (path2 != "") {
  12.                 if (path.back() != '\\')
  13.                         path.push_back('\\');
  14.                 path += path2;
  15.         }
  16.         if (path3 != "") {
  17.                 if (path.back() != '\\')
  18.                         path.push_back('\\');
  19.                 path += path3;
  20.         }
  21.         return path;
  22. }

  23. //寻找路径中的所有文件和文件夹
  24. pair<vector<string>, vector<string>> findAllFiles(string path) {
  25.         intptr_t handle;
  26.         _finddata_t findinfo;
  27.         pair<vector<string>, vector<string>> ret;
  28.         path = linkpath(path,"*");
  29.         handle = _findfirst(path.c_str(), &findinfo);
  30.         if (handle == -1) return ret;
  31.         do {
  32.                 string tmp = findinfo.name;
  33.                 if (tmp == "." || tmp == "..") continue;
  34.                 if (findinfo.attrib == _A_SUBDIR) {
  35.                         ret.second.push_back(tmp);
  36.                 }
  37.                 else {
  38.                         ret.first.push_back(tmp);
  39.                 }
  40.         } while (!_findnext(handle, &findinfo));

  41.         _findclose(handle);
  42.         return ret;
  43. }

  44. //分割扩展名和文件名
  45. pair<string, string> splitfname(string name) {
  46.         auto tmp = find(name.rbegin(), name.rend(), '.');
  47.         if (tmp == name.rend()) return { name,"" };
  48.         auto tmp2 = tmp.base();
  49.         return { string(name.begin(),tmp2 - 1),string(tmp2,name.end()) };
  50. }

  51. //获取前缀
  52. string prefix(string name, char seprator = '_') {
  53.         name = splitfname(name).first;
  54.         auto tmp = find(name.begin(), name.end(), seprator);
  55.         if (tmp == name.end()) return "";
  56.         return name.substr(0, tmp - name.begin());
  57. }

  58. //主函数
  59. void CollateFiles(string path) {
  60.         auto ret = findAllFiles(path);
  61.         auto& files = ret.first;
  62.         auto& dirs = ret.second;
  63.         int n = 0;
  64.         int m = 0;

  65.         for (string s : files) {
  66.                 string p = prefix(s);
  67.                 if (find(dirs.begin(), dirs.end(), p) == dirs.end()) {
  68.                         CreateDirectoryA(linkpath(path, p).c_str(), nullptr);
  69.                         ++m;
  70.                         dirs.push_back(p);
  71.                 }
  72.                 MoveFileA(linkpath(path, s).c_str(), linkpath(path, p, s).c_str());
  73.                 ++n;
  74.         }
  75.         cout << "整理完毕,共创建" << m << "个文件夹,移动" << n << "个文件" << endl;
  76. }

  77. int main() {
  78.         CollateFiles("D:\\b");
  79.         system("pause");
  80. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-14 17:39:42 | 显示全部楼层

文件操作用得比较少,按楼主的意思写了个windows平台下的

注意:不保证安全性,使用前请首先确认是否符合要求。操作前请备份原文件。本人对造成的损失概不负责!
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <io.h>
  5. #include <windows.h>
  6. using namespace std;

  7. //连接路径
  8. string linkpath(string path, string path2="",string path3="") {
  9.         if (path2 != "") {
  10.                 if (path.back() != '\\')
  11.                         path.push_back('\\');
  12.                 path += path2;
  13.         }
  14.         if (path3 != "") {
  15.                 if (path.back() != '\\')
  16.                         path.push_back('\\');
  17.                 path += path3;
  18.         }
  19.         return path;
  20. }

  21. //寻找路径中的所有文件和文件夹
  22. pair<vector<string>,vector<string>> findAllFiles(string path) {
  23.         intptr_t handle;
  24.         _finddata_t findinfo;
  25.         pair<vector<string>, vector<string>> ret;
  26.         path = linkpath(path, "*");
  27.         handle = _findfirst(path.c_str(), &findinfo);
  28.         if (handle == -1) return ret;
  29.         do {
  30.                 string tmp = findinfo.name;
  31.                 if (tmp == "." || tmp == "..") continue;
  32.                 if (findinfo.attrib == _A_SUBDIR) {
  33.                         ret.second.push_back(tmp);
  34.                 }
  35.                 else {
  36.                         ret.first.push_back(tmp);
  37.                 }
  38.         } while (!_findnext(handle, &findinfo));

  39.         _findclose(handle);
  40.         return ret;
  41. }

  42. //分割扩展名和文件名
  43. pair<string, string> splitfname(string name) {
  44.         auto tmp = find(name.rbegin(), name.rend(), '.');
  45.         if (tmp == name.rend()) return { name,"" };
  46.         auto tmp2 = tmp.base();
  47.         return { string(name.begin(),tmp2-1),string(tmp2,name.end())};
  48. }

  49. //获取前缀
  50. string prefix(string name,char seprator='_') {
  51.         name = splitfname(name).first;
  52.         auto tmp = find(name.begin(), name.end(), seprator);
  53.         if (tmp == name.end()) return "";
  54.         return name.substr(0, tmp - name.begin());
  55. }

  56. //主函数
  57. void CollateFiles(string path){
  58.         auto ret = findAllFiles(path);
  59.         auto& files = ret.first;
  60.         auto& dirs = ret.second;
  61.         int n = 0;
  62.         int m = 0;

  63.         for (string s : files) {
  64.                 string p = prefix(s);
  65.                 if (find(dirs.begin(), dirs.end(), p) == dirs.end()) {
  66.                         CreateDirectory(linkpath(path, p).c_str(), nullptr);
  67.                         ++m;
  68.                         dirs.push_back(p);
  69.                 }
  70.                 MoveFile(linkpath(path, s).c_str(), linkpath(path, p, s).c_str());
  71.                 ++n;
  72.         }
  73.         cout << "整理完毕,共创建" << m << "个文件夹,移动" << n << "个文件" << endl;
  74. }

  75. int main() {
  76.         CollateFiles("D:\\b\");
  77.         system("pause");
  78. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-15 08:20:18 | 显示全部楼层
Croper 发表于 2019-5-14 17:39
文件操作用得比较少,按楼主的意思写了个windows平台下的

注意:不保证安全性,使用前请首先确认是否 ...

非常感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-15 08:27:40 | 显示全部楼层
C:\Users\Administrator\Desktop
这个地方linkpath怎么回事
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-15 10:48:01 | 显示全部楼层
RunTian 发表于 2019-5-15 08:27
这个地方linkpath怎么回事

什么意思?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-15 11:22:15 | 显示全部楼层

1>e:\opencv cood\2\2\2.cpp(52): error C2059: 语法错误:“{”
1>e:\opencv cood\2\2\2.cpp(52): error C2143: 语法错误 : 缺少“;”(在“{”的前面)
1>e:\opencv cood\2\2\2.cpp(52): error C2143: 语法错误 : 缺少“;”(在“}”的前面)
1>e:\opencv cood\2\2\2.cpp(54): error C2059: 语法错误:“{”
1>e:\opencv cood\2\2\2.cpp(54): error C2143: 语法错误 : 缺少“;”(在“{”的前面)
1>e:\opencv cood\2\2\2.cpp(54): error C2143: 语法错误 : 缺少“;”(在“}”的前面)
1>e:\opencv cood\2\2\2.cpp(76): error C2664: “CreateDirectoryW”: 不能将参数 1 从“const char *”转换为“LPCWSTR”
1>          与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
1>e:\opencv cood\2\2\2.cpp(80): error C2664: “MoveFileW”: 不能将参数 1 从“const char *”转换为“LPCWSTR”
1>          与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
1>e:\opencv cood\2\2\2.cpp(87): error C2001: 常量中有换行符
1>e:\opencv cood\2\2\2.cpp(88): error C2146: 语法错误: 缺少“)”(在标识符“system”的前面)
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========


有一些错误,这个怎么解决
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-15 12:01:39 | 显示全部楼层
本帖最后由 Croper 于 2019-5-15 12:03 编辑

把编码格式改成ansi,没做unicode的代码,
然后
  1.         CollateFiles("D:\\b\");
复制代码

改成
  1.         CollateFiles("D:\\b");
复制代码


改成
  1.         CollateFiles("D:\\b\\");
复制代码


貌似这儿发代码会吃掉一个'\'
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-15 12:36:01 | 显示全部楼层    本楼为最佳答案   
或者用这个版本
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <io.h>
  5. #include <windows.h>
  6. using namespace std;

  7. #ifdef UNICODE

  8. #endif

  9. //连接路径
  10. string linkpath(string path, string path2 = "", string path3 = "") {
  11.         if (path2 != "") {
  12.                 if (path.back() != '\\')
  13.                         path.push_back('\\');
  14.                 path += path2;
  15.         }
  16.         if (path3 != "") {
  17.                 if (path.back() != '\\')
  18.                         path.push_back('\\');
  19.                 path += path3;
  20.         }
  21.         return path;
  22. }

  23. //寻找路径中的所有文件和文件夹
  24. pair<vector<string>, vector<string>> findAllFiles(string path) {
  25.         intptr_t handle;
  26.         _finddata_t findinfo;
  27.         pair<vector<string>, vector<string>> ret;
  28.         path = linkpath(path,"*");
  29.         handle = _findfirst(path.c_str(), &findinfo);
  30.         if (handle == -1) return ret;
  31.         do {
  32.                 string tmp = findinfo.name;
  33.                 if (tmp == "." || tmp == "..") continue;
  34.                 if (findinfo.attrib == _A_SUBDIR) {
  35.                         ret.second.push_back(tmp);
  36.                 }
  37.                 else {
  38.                         ret.first.push_back(tmp);
  39.                 }
  40.         } while (!_findnext(handle, &findinfo));

  41.         _findclose(handle);
  42.         return ret;
  43. }

  44. //分割扩展名和文件名
  45. pair<string, string> splitfname(string name) {
  46.         auto tmp = find(name.rbegin(), name.rend(), '.');
  47.         if (tmp == name.rend()) return { name,"" };
  48.         auto tmp2 = tmp.base();
  49.         return { string(name.begin(),tmp2 - 1),string(tmp2,name.end()) };
  50. }

  51. //获取前缀
  52. string prefix(string name, char seprator = '_') {
  53.         name = splitfname(name).first;
  54.         auto tmp = find(name.begin(), name.end(), seprator);
  55.         if (tmp == name.end()) return "";
  56.         return name.substr(0, tmp - name.begin());
  57. }

  58. //主函数
  59. void CollateFiles(string path) {
  60.         auto ret = findAllFiles(path);
  61.         auto& files = ret.first;
  62.         auto& dirs = ret.second;
  63.         int n = 0;
  64.         int m = 0;

  65.         for (string s : files) {
  66.                 string p = prefix(s);
  67.                 if (find(dirs.begin(), dirs.end(), p) == dirs.end()) {
  68.                         CreateDirectoryA(linkpath(path, p).c_str(), nullptr);
  69.                         ++m;
  70.                         dirs.push_back(p);
  71.                 }
  72.                 MoveFileA(linkpath(path, s).c_str(), linkpath(path, p, s).c_str());
  73.                 ++n;
  74.         }
  75.         cout << "整理完毕,共创建" << m << "个文件夹,移动" << n << "个文件" << endl;
  76. }

  77. int main() {
  78.         CollateFiles("D:\\b");
  79.         system("pause");
  80. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-15 13:22:42 | 显示全部楼层
Croper 发表于 2019-5-15 12:36
或者用这个版本

好的,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-3 09:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表