I'm trying to make a feature in my program to delete all files in the Windows temporary folder "(C:\Users\Owner\AppData\Local\Temp)", how would I do this?
CodePudding user response:
Call the Win32 API GetTempPath()
function to get the user's %TEMP%
folder path, then you can either:
call
FindFirstFile()
/FindNextFile()
in a loop, callingDeleteFile()
on each iteration.call
SHFileOperation()
, specifyingFO_DELETE
with a*.*
wildcard.use a loop to discover the files, calling
IFileOperation::DeleteItem()
to mark each one for deletion, and then callIFileOperation::PerformOperations()
to actually delete them.
If you want a pure C solution, and are using C 17 or later, then you can use std::filesystem::temp_directory_path()
, using std::filesystem::directory_iterator
and std::filesystem::remove()
in a loop, or just std::filesystem::remove_all()
by itself.