C++编程:
1.采用windows api;
2.将.\label文件夹下的所有txt文件挨个存放到容器中;
3.读取一个.\video文件夹下的一级子目录路径,将容器中的一个文件放入,读取下一个.\video文件夹下的一级子目录路径,将容器中的下一个文件放入,依次类推;
#include <iostream>
#include <windows.h>
#include <vector>
#include <string>
#include <fstream>
std::vector<std::wstring> GetAllTxtFiles(const std::wstring& folderPath)
{
std::vector<std::wstring> txtFiles;
std::wstring searchPath = folderPath + L"\\*.txt";
WIN32_FIND_DATAW fileData;
HANDLE hFind = FindFirstFileW(searchPath.c_str(), &fileData);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
if ((fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
std::wstring txtFileName = fileData.cFileName;
txtFiles.push_back(txtFileName);
}
} while (FindNextFileW(hFind, &fileData));
FindClose(hFind);
}
return txtFiles;
}
bool MoveFileToFolder(const std::wstring& filePath, const std::wstring& destinationFolderPath)
{
std::wstring destinationPath = destinationFolderPath + L"\\" + filePath;
if (!MoveFileW(filePath.c_str(), destinationPath.c_str()))
{
std::wcout << L"Failed to move file: " << filePath << std::endl;
return false;
}
else
{
std::wcout << L"Moved file: " << filePath << L" to: " << destinationPath << std::endl;
return true;
}
}
std::wstring ReadFileContent(const std::wstring& filePath)
{
std::wifstream file(filePath);
if (!file)
{
std::wcerr << L"Failed to open file: " << filePath << std::endl;
return L"";
}
std::wstring content;
std::wstring line;
while (std::getline(file, line))
{
content += line + L"\n";
}
file.close();
return content;
}
int main()
{
std::wstring labelFolder = L".\\label"; // label文件夹路径
std::wstring videoFolder = L".\\video"; // video文件夹路径
// 获取所有txt文件
std::vector<std::wstring> txtFiles = GetAllTxtFiles(labelFolder);
// 获取video文件夹下的一级子目录路径
std::wstring searchPath = videoFolder + L"\\*";
WIN32_FIND_DATAW fileData;
HANDLE hFind = FindFirstFileW(searchPath.c_str(), &fileData);
if (hFind != INVALID_HANDLE_VALUE)
{
size_t txtIndex = 0;
do
{
if ((fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
{
std::wstring subFolderName = fileData.cFileName;
if (subFolderName != L"." && subFolderName != L"..")
{
// 读取txt文件并将其放入当前子目录
if (txtIndex < txtFiles.size())
{
std::wstring txtFile = txtFiles[txtIndex];
++txtIndex;
std::wstring txtFilePath = labelFolder + L"\\" + txtFile;
std::wstring destinationFolderPath = videoFolder + L"\\" + subFolderName;
// 创建目标子目录
CreateDirectoryW(destinationFolderPath.c_str(), nullptr);
// 读取txt文件内容
std::wstring txtContent = ReadFileContent(txtFilePath);
// 将txt文件内容保存到目标子目录中
std::wstring destinationFilePath = destinationFolderPath + L"\\" + txtFile;
std::wofstream destinationFile(destinationFilePath);
if (destinationFile)
{
destinationFile << txtContent;
destinationFile.close();
std::wcout << L"Moved file with content: " << txtFilePath << L" to: " << destinationFilePath << std::endl;
}
else
{
std::wcerr << L"Failed to create file: " << destinationFilePath << std::endl;
}
}
else
{
std::wcout << L"No more txt files to move." << std::endl;
break;
}
}
}
} while (FindNextFileW(hFind, &fileData));
FindClose(hFind);
}
return 0;
}