1

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");
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Lazy Toxic
  • 27
  • 4
  • What error did you get? – user207421 May 12 '21 at 04:34
  • Spawning a shell to call remove is a terrible way to do it. Since you're on Windows you can use the [DeleteFile](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-deletefile) function – Irelia May 12 '21 at 04:42
  • Also make sure you have privilege to delete that file. – Irelia May 12 '21 at 04:43
  • Use [`std::filesystem::remove_all`](https://en.cppreference.com/w/cpp/filesystem/remove) instead and invoke program with administrator rights. – brc-dd May 12 '21 at 04:44
  • @brc-dd `remove_all` also removes the directory. OP wants it to not be deleted. – Casey May 12 '21 at 04:59
  • "C:\Program Files (x86)" is protected by UAC. Related: [https://stackoverflow.com/questions/16617004](https://stackoverflow.com/questions/16617004) – drescherjm May 12 '21 at 05:00
  • @Casey then maybe create it again after deleting? [std::filesystem::create_directory](https://en.cppreference.com/w/cpp/filesystem/create_directory). Moreover I also suspect that steam will create it again if it didn't find it. Also one possible cause that the OP is unable to delete the directory is that the files are being used by steam or its service. – brc-dd May 12 '21 at 06:01
  • And why are you putting user files there? They should be in $HOME/AppData/... – user207421 May 12 '21 at 06:31
  • 1
    @user207421 There is nothing like `$HOME` in Windows (unless you manually create suitable environment variable). Although there is `%appdata%`. Moreover it is the choice of Steam developers not the OP. – brc-dd May 12 '21 at 09:45
  • 1
    I believe there is a %USERPROFILE% environment variable but I do agree that the location of the files is a Steam choice. – drescherjm May 12 '21 at 12:10
  • You're using `system` anyway. Why don't you just to `CD ` then `DEL *`? – kesarling He-Him May 12 '21 at 15:08

2 Answers2

1

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

Vapour Dev
  • 90
  • 8
  • Well i tried and it did not work either, the thing is i would like to remove all the file in "userdata". and doing : remove("C:\\Program Files (x86)\\Steam\\userdata"); will remove the folder userdata wich i dont want :/ – Lazy Toxic May 12 '21 at 04:42
  • 1
    Go to folder userdata and remove each file via loop – Vapour Dev May 12 '21 at 04:45
0

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);
    }
}
Casey
  • 10,297
  • 11
  • 59
  • 88
  • OP, take a look at this answer. This is the only platform independent solution. System calls are really not a good idea! – kesarling He-Him May 12 '21 at 15:09
  • Nice call but even when i include it doesnt recognize 'std::filesystem' and 'iter'. Any idea ? – Lazy Toxic May 12 '21 at 22:57
  • @LazyToxic You make no mention of what compiler, version, and language setting you are using. If C++17 mode (or higher) is not turned on nor supported, it won't work. – Casey May 13 '21 at 03:07