#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>
// 检查目录是否存在
bool isDirectoryExist(const std::string &directory)
{
return std::filesystem::is_directory(std::filesystem::path(directory));
}
// 获取文件名和扩展名
void getBasenameAndExtension(const std::string &filename, std::string &basename, std::string &extension)
{
basename = std::filesystem::path(filename).stem().string();
extension = std::filesystem::path(filename).extension().string();
}
// 重命名文件
void renameFile(const std::string &oldname, const std::string &newname)
{
std::filesystem::rename(oldname, newname);
}
int main(int argc, char *argv[])
{
// 检查输入参数
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << "<directory>" << std::endl;
return 1;
}
// 获取目录路径
std::string directory = argv[1];
// 检查目录是否存在
if (!isDirectoryExist(directory))
{
std::cerr << "Invalid directory: " << directory << std::endl;
return 1;
}
// 遍历目录中的所有文件
int count = 0;
for (auto &entry : std::filesystem::directory_iterator(directory))
{
// 获取文件路径
std::string filename = entry.path().string();
// 检查文件是否存在
if (!std::filesystem::is_regular_file(entry))
{
std::cerr << "Invalid file:" << filename << std::endl;
return 1;
}
// 获取文件名和扩展名
std::string basename, extension;
getBasenameAndExtension(filename, basename, extension);
// 构造新文件名
std::string newname = directory + "/" + "meow" + std::to_string(count) + extension;
// 重命名文件
renameFile(filename, newname);
count++;
}
std::cout << "Renamed " << count << " files." << std::endl;
return 0;
return 0;
}