aeszyl 发表于 2024-12-24 10:08:48

c++代码嵌入,实现文件批量重命名

本帖最后由 aeszyl 于 2024-12-24 16:02 编辑

  c++代码嵌入是火山中文编程的灵魂,这几天一直研究这方面的知识,先用AI(我用的是:文心快码)根据要求输出c++代码,再转成嵌入式代码。下面代码中的正则表达式,稍做修改,可以实现各种文件批量重命名功能。
<火山程序 类型 = "通常" 版本 = 1 />

包 火山.程序

类 启动类 <公开 基础类 = 窗口程序类>
{
    方法 启动方法 <公开 类型 = 整数>
    {
      文件批量更名 ()
      返回 (1)
    }

    方法 文件批量更名 <公开 静态 @视窗.外部头文件 = "<iostream>\r\n<filesystem>\r\n<string>\r\n<regex>"
            @视窗.附加编译参数 = "cpp: /std:c++17">
    {
      // 文件目录=当前目录
      @ std::filesystem::path currentDir = std::filesystem::current_path();
      // 遍历当前目录中的所有文件
      @ for (const auto& entry : std::filesystem::directory_iterator(currentDir)) {
      @ if (std::filesystem::is_regular_file(entry.status())) {
      @ std::string filename = entry.path().filename().string();
      // 使用正则表达式匹配文件名(名称1-名称2.*)
      @ std::regex pattern(R"(([^-]+)-([^-]+)(\..+))");
      @ std::smatch match;
      @ if (std::regex_match(filename, match, pattern)) {
      @ std::string name1 = match.str();
      @ std::string name2 = match.str();
      @ std::string name3 = match.str();
      // 构建新文件名(名称2-名称1.*)
      @ std::string newName = name2 + "-" + name1 +name3;
      @ std::cout << "Renaming " << filename << " to " << newName << std::endl;
      // 批量重命名文件
      @ std::filesystem::rename(entry.path(), entry.path().parent_path() / newName);
      @ }
      @ }
      @ }
    }
}



rinipp 发表于 2024-12-24 12:05:27

C++20 需要稍微修改下

Xelloss0618 发表于 2024-12-24 12:16:56

你这个代码也只需要用到C++17。
并不是火山不兼容C++20,而是C++20的改动太大,以前的很多代码不符合C++20的规范,不允许编译。
现在火山核心库和第三方模块就有大量的代码不兼容C++20,所以会导致大量报错。
页: [1]
查看完整版本: c++代码嵌入,实现文件批量重命名