![]() |
#2
apull2022-05-23 21:54
|

#include <iostream>
#include <filesystem>
#include <algorithm>
using namespace std;
bool ReduceFileName(const std::filesystem::path& path)
{
//auto make_name_name = [](const std::wstring& filename)
//{
// size_t a = filename.find(L':');
// if (a != std::wstring::npos)
// {
// a = filename.find_first_not_of(L"\t\n\v\f\r ", a + 1);
// size_t b = filename.find_first_of(L"((", a);
// b = filename.find_last_not_of(L"\t\n\v\f\r ", b - 1);
// return filename.substr(a, b + 1 - a);
// }
// return filename;
//};
//using namespace std::filesystem;
//try
//{
// for (auto& entry : directory_iterator(path))
// {
// if (entry.is_regular_file()) // 只处理常规文件
// {
// auto ext = entry.path().extension().generic_wstring(); // 扩展名
// std::for_each(begin(ext), end(ext), [](auto& c) {if (c >= L'A'&&c <= L'Z') c += L'a' - L'A'; }); // 扩展名转化成小写
// if (ext == L".mp4")
// {
// const auto& old_name = entry.path().stem().generic_wstring();
// const auto& new_name = make_name_name(old_name);
// if (old_name != new_name)
// {
// wcout << old_name << L" -> " << new_name;
// auto new_path = entry.path().parent_path() / (new_name + L".mp4");
// if (exists(new_path)) // 新文件名已经存在
// {
// wcout << L" --- [failed] 文件已存在";
// }
// else
// {
// std::error_code ec;
// rename(entry, new_path, ec);
// if (ec)
// cout << " --- [failed] " << ec.message();
// }
// wcout << L'\n';
// }
// }
// }
// }
//}
//catch (const std::exception& e)
//{
// cout << "[failed] " << e.what() << '\n';
// return false;
//}
return true;
}
int main(void)
{
wcout.imbue(std::locale(std::locale(), "", std::locale::ctype));
ReduceFileName(u8R"(C:\Users\Administrator\Desktop\[WPF]JJDown\Download\)"); // 使用 Unicode 可避免GBK认字不全
}
#include <filesystem>
#include <algorithm>
using namespace std;
bool ReduceFileName(const std::filesystem::path& path)
{
//auto make_name_name = [](const std::wstring& filename)
//{
// size_t a = filename.find(L':');
// if (a != std::wstring::npos)
// {
// a = filename.find_first_not_of(L"\t\n\v\f\r ", a + 1);
// size_t b = filename.find_first_of(L"((", a);
// b = filename.find_last_not_of(L"\t\n\v\f\r ", b - 1);
// return filename.substr(a, b + 1 - a);
// }
// return filename;
//};
//using namespace std::filesystem;
//try
//{
// for (auto& entry : directory_iterator(path))
// {
// if (entry.is_regular_file()) // 只处理常规文件
// {
// auto ext = entry.path().extension().generic_wstring(); // 扩展名
// std::for_each(begin(ext), end(ext), [](auto& c) {if (c >= L'A'&&c <= L'Z') c += L'a' - L'A'; }); // 扩展名转化成小写
// if (ext == L".mp4")
// {
// const auto& old_name = entry.path().stem().generic_wstring();
// const auto& new_name = make_name_name(old_name);
// if (old_name != new_name)
// {
// wcout << old_name << L" -> " << new_name;
// auto new_path = entry.path().parent_path() / (new_name + L".mp4");
// if (exists(new_path)) // 新文件名已经存在
// {
// wcout << L" --- [failed] 文件已存在";
// }
// else
// {
// std::error_code ec;
// rename(entry, new_path, ec);
// if (ec)
// cout << " --- [failed] " << ec.message();
// }
// wcout << L'\n';
// }
// }
// }
// }
//}
//catch (const std::exception& e)
//{
// cout << "[failed] " << e.what() << '\n';
// return false;
//}
return true;
}
int main(void)
{
wcout.imbue(std::locale(std::locale(), "", std::locale::ctype));
ReduceFileName(u8R"(C:\Users\Administrator\Desktop\[WPF]JJDown\Download\)"); // 使用 Unicode 可避免GBK认字不全
}
1>------ 已启动生成: 项目: test3, 配置: Debug Win32 ------
1> test3.cpp
1>c:\users\administrator\desktop\test3\test3\test3.cpp(6): error C3083: “filesystem”:“::”左侧的符号必须是一种类型
1>c:\users\administrator\desktop\test3\test3\test3.cpp(6): error C2039: “path”: 不是“std”的成员
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\filesystem(26): note: 参见“std”的声明
1>c:\users\administrator\desktop\test3\test3\test3.cpp(6): error C2143: 语法错误: 缺少“,”(在“&”的前面)
1>c:\users\administrator\desktop\test3\test3\test3.cpp(68): error C2664: “bool ReduceFileName(const int)”: 无法将参数 1 从“const char [53]”转换为“const int”
1> c:\users\administrator\desktop\test3\test3\test3.cpp(68): note: 没有使该转换得以执行的上下文
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========