Josh
Josh

Reputation: 6282

C++ Win32 API Delete file with progress bar

Using the windows api, is there any way to delete a large file (lets say 1gb+), and monitor the progress somehow? I'm sure its possible but I have no idea where to start..

EDIT: Should have been more specific, I want to move the file to the recycle bin, and show a progress bar similar to explores, though I might want the progress bar in a console or something so I don't want an exact replica.

EDIT 2: Yeaah guess it is instant, should have tested before I asked the question. Anyway to just close this question?

Upvotes: 2

Views: 2237

Answers (2)

Bradley Grainger
Bradley Grainger

Reputation: 28207

Use SHFileOperation with the FO_DELETE func and FOF_ALLOWUNDO flag to move a file to the Recycle Bin. Progress will automatically be shown unless you also specify FOF_SILENT.

SHFILEOPSTRUCT fileop = { 0 };
fileop.hwnd = hwndMain; /* your window */
fileop.wFunc = FO_DELETE;
fileop.pFrom = szFilePathToDelete;
fileop.fFlags = FOF_ALLOWUNDO /* | FOF_NOCONFIRMATION to recycle without prompting */;
int error = SHFileOperation(&fileop);

Update: As noted in the question edit, progress won't be shown for a single file, but it will be shown if recycling an entire directory. This also doesn't let you override the UI (e.g., to display progress in a console window).

Upvotes: 6

jcomeau_ictx
jcomeau_ictx

Reputation: 38492

Perhaps you could progressively truncate it using SetEndOfFile? http://msdn.microsoft.com/en-us/library/aa365531(v=vs.85).aspx. Then removing the inode (or whatever it's called in Windows-land) could be quickly done.

[update]Just tested; the other guys are right. Removing the inode (delete) is instantaneous on a 1gb file.

Upvotes: -1

Related Questions