鱼C论坛

 找回密码
 立即注册
查看: 1263|回复: 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
或者用这个版本
#include <iostream>
#include <vector>
#include <string>
#include <io.h>
#include <windows.h>
using namespace std;

#ifdef UNICODE

#endif

//连接路径
string linkpath(string path, string path2 = "", string path3 = "") {
        if (path2 != "") {
                if (path.back() != '\\')
                        path.push_back('\\');
                path += path2;
        }
        if (path3 != "") {
                if (path.back() != '\\')
                        path.push_back('\\');
                path += path3;
        }
        return path;
}

//寻找路径中的所有文件和文件夹
pair<vector<string>, vector<string>> findAllFiles(string path) {
        intptr_t handle;
        _finddata_t findinfo;
        pair<vector<string>, vector<string>> ret;
        path = linkpath(path,"*");
        handle = _findfirst(path.c_str(), &findinfo);
        if (handle == -1) return ret;
        do {
                string tmp = findinfo.name;
                if (tmp == "." || tmp == "..") continue;
                if (findinfo.attrib == _A_SUBDIR) {
                        ret.second.push_back(tmp);
                }
                else {
                        ret.first.push_back(tmp);
                }
        } while (!_findnext(handle, &findinfo));

        _findclose(handle);
        return ret;
}

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

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

//主函数
void CollateFiles(string path) {
        auto ret = findAllFiles(path);
        auto& files = ret.first;
        auto& dirs = ret.second;
        int n = 0;
        int m = 0;

        for (string s : files) {
                string p = prefix(s);
                if (find(dirs.begin(), dirs.end(), p) == dirs.end()) {
                        CreateDirectoryA(linkpath(path, p).c_str(), nullptr);
                        ++m;
                        dirs.push_back(p);
                }
                MoveFileA(linkpath(path, s).c_str(), linkpath(path, p, s).c_str());
                ++n;
        }
        cout << "整理完毕,共创建" << m << "个文件夹,移动" << n << "个文件" << endl;
}

int main() {
        CollateFiles("D:\\b");
        system("pause");
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

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

//连接路径
string linkpath(string path, string path2="",string path3="") {
        if (path2 != "") {
                if (path.back() != '\\') 
                        path.push_back('\\');
                path += path2;
        }
        if (path3 != "") {
                if (path.back() != '\\')
                        path.push_back('\\');
                path += path3;
        }
        return path;
}

//寻找路径中的所有文件和文件夹
pair<vector<string>,vector<string>> findAllFiles(string path) {
        intptr_t handle;
        _finddata_t findinfo;
        pair<vector<string>, vector<string>> ret;
        path = linkpath(path, "*");
        handle = _findfirst(path.c_str(), &findinfo);
        if (handle == -1) return ret;
        do {
                string tmp = findinfo.name;
                if (tmp == "." || tmp == "..") continue;
                if (findinfo.attrib == _A_SUBDIR) {
                        ret.second.push_back(tmp);
                }
                else {
                        ret.first.push_back(tmp);
                }
        } while (!_findnext(handle, &findinfo));

        _findclose(handle);
        return ret;
}

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

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

//主函数
void CollateFiles(string path){
        auto ret = findAllFiles(path);
        auto& files = ret.first;
        auto& dirs = ret.second;
        int n = 0;
        int m = 0;

        for (string s : files) {
                string p = prefix(s);
                if (find(dirs.begin(), dirs.end(), p) == dirs.end()) {
                        CreateDirectory(linkpath(path, p).c_str(), nullptr);
                        ++m;
                        dirs.push_back(p);
                }
                MoveFile(linkpath(path, s).c_str(), linkpath(path, p, s).c_str());
                ++n;
        }
        cout << "整理完毕,共创建" << m << "个文件夹,移动" << n << "个文件" << endl;
}

int main() {
        CollateFiles("D:\\b\");
        system("pause");
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

非常感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-15 08:27:40 | 显示全部楼层
C:\Users\Administrator\Desktop
这个地方linkpath怎么回事
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

什么意思?
想知道小甲鱼最近在做啥?请访问 -> 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 个 ==========


有一些错误,这个怎么解决
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

改成
        CollateFiles("D:\\b\\");

貌似这儿发代码会吃掉一个'\'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

#ifdef UNICODE

#endif

//连接路径
string linkpath(string path, string path2 = "", string path3 = "") {
        if (path2 != "") {
                if (path.back() != '\\')
                        path.push_back('\\');
                path += path2;
        }
        if (path3 != "") {
                if (path.back() != '\\')
                        path.push_back('\\');
                path += path3;
        }
        return path;
}

//寻找路径中的所有文件和文件夹
pair<vector<string>, vector<string>> findAllFiles(string path) {
        intptr_t handle;
        _finddata_t findinfo;
        pair<vector<string>, vector<string>> ret;
        path = linkpath(path,"*");
        handle = _findfirst(path.c_str(), &findinfo);
        if (handle == -1) return ret;
        do {
                string tmp = findinfo.name;
                if (tmp == "." || tmp == "..") continue;
                if (findinfo.attrib == _A_SUBDIR) {
                        ret.second.push_back(tmp);
                }
                else {
                        ret.first.push_back(tmp);
                }
        } while (!_findnext(handle, &findinfo));

        _findclose(handle);
        return ret;
}

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

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

//主函数
void CollateFiles(string path) {
        auto ret = findAllFiles(path);
        auto& files = ret.first;
        auto& dirs = ret.second;
        int n = 0;
        int m = 0;

        for (string s : files) {
                string p = prefix(s);
                if (find(dirs.begin(), dirs.end(), p) == dirs.end()) {
                        CreateDirectoryA(linkpath(path, p).c_str(), nullptr);
                        ++m;
                        dirs.push_back(p);
                }
                MoveFileA(linkpath(path, s).c_str(), linkpath(path, p, s).c_str());
                ++n;
        }
        cout << "整理完毕,共创建" << m << "个文件夹,移动" << n << "个文件" << endl;
}

int main() {
        CollateFiles("D:\\b");
        system("pause");
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

好的,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 19:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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