简体   繁体   中英

C++ | How can i delete files in a folder located in (C:\Program Files (x86))

So here is my issue, i would like to delete folders that are located in 'C:\Program Files (x86)\Steam\userdata'

so to be clear in userdata there is multiple folder, and i want to clear userdata(remove everything in userdata) but i dont want to delete userdata itself.

i tried multiple things but so far none worked, i was only able to delete.txt like:

#include <iostream>
#include <string>
#include <cstdio>


int main() {


   remove("C:\\Program Files (x86)\\Steam\\userdata\\test.txt");
    

    system("pause");
}

I thinks, You need Permission for doing file operations in C Drive, Just run your program with adminstrator rights!

You could call std::filesystem::remove_all , but this also removes the supplied directory. Because of the requirement that the parent directory remain, you'll have to use a loop:

#include <filesystem>

int main() {
    for(std::filesystem::recursive_directory_iterator iter{std::filesystem::path{"C:/Program Files (x86)/Steam/userdata/"}};
        std::filesystem::recursive_directory_iterator{};
        ++iter)
    {
        std::filesystem::remove(*iter);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM