|
将当前目录下所有形如 "甲-乙.*" 的文件重命名为 "乙-甲.*"
甲-乙.* 正则式表示为 (.*)-(.*)\\.(.*)
乙-甲.* 正则式表示为 $2-$1.$3
命令如下:用正则式批量更名 ("(.*)-(.*)\\.(.*)", "$2-$1.$3")
甲-乙.* 就可以重命名为 乙-甲.*
只要变换相应的正则式文本,就可以在当前目录下,批量重命名文件
- <火山程序 类型 = "通常" 版本 = 1 />
- 包 火山.程序
- 类 启动类 <公开 基础类 = 程序类>
- {
- 方法 启动方法 <公开 类型 = 整数>
- {
- // 将当前目录下所有形如 "甲-乙.*" 的文件重命名为 "乙-甲.*"
- 用正则式批量更名 ("(.*)-(.*)\\.(.*)", "$2-$1.$3")
- 返回 (0)
- }
- 方法 用正则式批量更名 <公开 折叠 折叠2 @禁止流程检查 = 真 @视窗.外部头文件 = "<filesystem>\r\n<iostream>\r\n<string>\r\n<regex>"
- @视窗.附加编译参数 = "cpp: /std:c++17">
- 参数 正则式甲 <类型 = 文本型>
- 参数 正则式乙 <类型 = 文本型>
- {
- @ const wchar_t* pattern = @<正则式甲>.GetText();
- @ const wchar_t* replacement = @<正则式乙>.GetText();
- @ std::wregex regexPattern(pattern);
- @ std::wsmatch match;
- @ std::wstring newName;
- @ for (const auto& entry : std::filesystem::directory_iterator(L".")) {
- @ if (entry.is_regular_file()) {
- @ std::wstring filename = entry.path().filename().wstring();
- @ if (std::regex_match(filename, match, regexPattern)) {
- @ newName = std::regex_replace(filename, regexPattern, replacement);
- @ std::filesystem::rename(entry.path(), newName);
- @ std::wcout << L"Renamed: " << filename << L" -> " << newName << std::endl;
- @ }
- @ }
- @ }
- }
- }
复制代码
|
|