C++删除某个目录下的所有MP4
时间: 2023-10-15 14:03:58 浏览: 235
你可以使用C++中的文件操作和字符串处理函数来实现删除某个目录下的所有MP4文件。具体步骤如下:
1. 使用C++中的文件操作函数打开目标目录,获取其中的所有文件名。
2. 使用C++中的字符串处理函数判断每个文件名是否以“.mp4”结尾,如果是则记录该文件名。
3. 遍历记录下来的所有MP4文件名,使用C++中的文件操作函数逐个删除它们。
以下是示例代码:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
std::string dir_path = "your/directory/path";
std::vector<std::string> mp4_files;
for (const auto& entry : fs::directory_iterator(dir_path))
{
std::string file_name = entry.path().filename().string();
if (file_name.size() >= 4 && file_name.substr(file_name.size() - 4) == ".mp4")
{
mp4_files.push_back(file_name);
}
}
for (const auto& file_name : mp4_files)
{
fs::remove(fs::path(dir_path) / file_name);
}
return 0;
}
```
这段代码可以遍历指定目录下的所有文件,筛选出所有MP4文件,然后逐个删除它们。
阅读全文
相关推荐














