Disk Partitions Information:
Drive: C:\ (Local Drive) - Total: 119 GB
Drive: D:\ (Local Drive) - Total: 341 GB
Drive: G:\ (Local Drive) - Total: 15 GB
Pick a path: C:\
"C:\\$Recycle.Bin"
"C:\\$Recycle.Bin\\S-1-5-18"
Error: Unable to iterate through the path C:\ (filesystem error: cannot increment recursive directory iterator: Invalid argument)
Permissions for "C:\\":
rwxrwxrwx
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
what(): filesystem error: cannot set permissions: Permission denied [C:\]
E para o DI só obtive o erro "Não foi possível iterar pelo caminho C:\ (erro no sistema de arquivos: não é possível incrementar o iterador de diretório recursivo: argumento inválido)"
Meu objetivo é imprimir todos os arquivos em um diretório. Quando recebo esse erro, penso que seja algo sobre permissões ou algo assim, então tento forçar todas as permissões, porque é o que eu costumava fazer em Python. No entanto, sinto que em C++ não funciona.
void check_permissions(const fs::path& path) {
fs::perms p = fs::status(path).permissions();
std::cout << "Permissions for " << path << ":\n";
std::cout << ((p & fs::perms::owner_read) != fs::perms::none ? "r" : "-")
<< ((p & fs::perms::owner_write) != fs::perms::none ? "w" : "-")
<< ((p & fs::perms::owner_exec) != fs::perms::none ? "x" : "-")
<< ((p & fs::perms::group_read) != fs::perms::none ? "r" : "-")
<< ((p & fs::perms::group_write) != fs::perms::none ? "w" : "-")
<< ((p & fs::perms::group_exec) != fs::perms::none ? "x" : "-")
<< ((p & fs::perms::others_read) != fs::perms::none ? "r" : "-")
<< ((p & fs::perms::others_write) != fs::perms::none ? "w" : "-")
<< ((p & fs::perms::others_exec) != fs::perms::none ? "x" : "-")
<< '\n';
}
void set_permissions(const fs::path& path, fs::perms permissions) {
fs::permissions(path, permissions);
}
E abaixo está a função que imprime todos os arquivos:
void list_files_recursively(const std::string& path) {
try {
for (fs::directory_entry entry : fs::recursive_directory_iterator(path)) {
try {
std::cout << entry.path() << std::endl;
} catch (const fs::filesystem_error& e) {
std::cerr << "Error accessing: " << entry.path() << " (" << e.what() << ")\n";
}
}
} catch (const fs::filesystem_error& e) {
std::cerr << "Error: Unable to iterate through the path " << path << " (" << e.what() << ")\n";
}
}
Com o Google Drive G:, funciona sem nenhum problema, e também tentei pular os arquivos que não tive acesso, não funciona.
exemplo mínimo reproduzível
#include <iostream>
#include <filesystem>
#include <windows.h>
namespace fs = std::filesystem;
void print_files(const std::string& path) {
try {
for (const auto& entry : fs::recursive_directory_iterator(path, fs::directory_options::skip_permission_denied)) {
try {
std::cout << entry.path() << std::endl;
} catch (const fs::filesystem_error& e) {
std::cerr << "Error accessing: " << entry.path() << " (" << e.what() << ")\n";
}
}
} catch (const fs::filesystem_error& e) {
std::cerr << "Error: Unable to iterate through the path " << path << " (" << e.what() << ")\n";
}
}
int main() {
std::string path;
std::cout << "Pick a path: ";
std::cin >> path;
print_files(path);
return 0;
}
Retorna o mesmo erro.
Com a outra página do StackOverflow eu escrevi isso. Não funciona com C:\\
mas funciona com pastas específicas mas não com C:\\
.
void print_files(const std::string& path) {
try {
for (const auto& entry : fs::recursive_directory_iterator(path, fs::directory_options::skip_permission_denied)) {
try {
// Skip system and hidden files
if ((entry.status().permissions() & fs::perms::others_read) == fs::perms::none) {
continue;
}
std::cout << "Path: " << entry.path()
<< " | Type: " << (entry.is_directory() ? "Directory" : "File")
<< " | Size: " << (entry.is_regular_file() ? std::to_string(entry.file_size()) + " bytes" : "N/A")
<< std::endl;
} catch (const fs::filesystem_error& e) {
std::cerr << "Error accessing: " << entry.path() << " (" << e.what() << ")\n";
}
}
} catch (const fs::filesystem_error& e) {
std::cerr << "Error: Unable to iterate through the path " << path << " (" << e.what() << ")\n";
}
}
int main() {
std::string path;
std::cout << "Pick a path: ";
std::cin >> path;
if (!fs::exists(path)) {
std::cerr << "Error: Path does not exist.\n";
return 1;
}
print_files(path);
return 0;
}
// BUT THIS STILL DOESN'T WORK WITH C:\
Como @PaulMcKenzie disse, esse erro é facilmente evitado executando o Visual Studio Code como administrador
Para executá-lo como administrador, você pode clicar duas vezes no Visual Studio Code e rolar para baixo nas opções até encontrar Executar como administrador .
Ou, para executá-lo como administrador o tempo todo, você pode :
A partir de então, ele será executado como administrador sempre que você abrir o aplicativo.