|
how can i delete file or folder after reboot...or I want to brut force delete it ..even it is in use ...how can i achive that
|
|
|
|
|
Hi,
I was trying to compile this code but couldn't ,wondering what about definition of RemoveDirectory() method?
thanks in advance.
|
|
|
|
|
It's declared in winbase.h . You should be #including windows.h . Perhaps your project settings are incorrect?
/ravi
Let's put "civil" back in "civilization"
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
sorry for stupid question.
|
|
|
|
|
Probably a good idea to check for read only files:
if (logfile.IsReadOnly())
continue;
Also any suggestions on how to tell if the file is open by another app ?
|
|
|
|
|
RecursiveDelete() has a bug, it will attempt to delete the folders '.' and '..'! Below is the corrected code plus it does what it says it does...
RemoveDirectoryRecursive(X) will remove the contents of folder X and remove folder X itself! Oh... and I've tested it !
void RemoveDirectoryRecursive(CString szPath)
{
// Removes the folder szPath after removing all its
// contents including its sub-folders and their contents
//
// The following block {..} limits the scope of ff
{
CFileFind ff;
CString path = szPath;
if(path.Right(1) != "\\")
path += "\\";
path += "*.*";
BOOL res = ff.FindFile(path);
while(res)
{
res = ff.FindNextFile();
if (ff.IsDots())
continue;
if (ff.IsDirectory())
{
path = ff.GetFilePath();
RemoveDirectoryRecursive(path);
RemoveDirectory(path);
}
else
DeleteFile(ff.GetFilePath());
}
}
// Remove szPath itself...
// ...Now everything under folder szPath has been removed
// and since ff is now out of scope so it no longer has
// open a handle to folder szPath
RemoveDirectory(szPath);
}
Andy G.
Andy G.
|
|
|
|
|
great, but
...
if (ff.IsDirectory())
{
path = ff.GetFilePath();
RemoveDirectoryRecursive(path);
//RemoveDirectory(path);
}
...
after your recursion into the subdirectory you don't need to delete it a second time
mm
|
|
|
|
|
And don't forget do ff.Close() before RemoveDirectory or you will have this error under WinNT/2000:
"The process cannot access the file because it is being used by another process."
<fess>
|
|
|
|
|
how not?
RemoveDirectoryRecursive(path)
does actually the DeleteFile on the sub dir (and looks for other possible subdirs).
And when you Delete all files in it and there are no more subdirs you get out of it, then you have to remove the directory (the empty one, now) with RemoveDirectory and not with DeleteFile which removes files...
|
|
|
|
|
While I m trying to delete profile folder of a system...
I m not able to delete complete profile folder even with this code...
When I debug this code.. I found in GetLastError(), error no. for Access denied..
So please any one have any idea about this issue...???
|
|
|
|
|
This is from the API documentation for SHFileOperation:
"When used to delete a file, SHFileOperation will attempt to place the deleted file in the Recycle Bin. If you wish to delete a file and guarantee that it will not be placed in the Recycle Bin, use DeleteFile."
The message is clear...if you do NOT want your deleted files to go to the Recycle Bin use the RecursiveDelete and NOT the shell operation method.
Hope this is useful to someone.
Thanks for the RecursiveDelete function i was on the brink of writing it when I found it!
Andy G.
|
|
|
|
|
No way man. If you dont add FOF_ALLOWUNDO to your flags, it will automatically delete it. It wont put it into recycle bin!
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix
|
|
|
|
|
Thanks Jonathan,it's useful code
Mazy
"So,so you think you can tell,
Heaven from Hell,
Blue skies from pain,...
How I wish,how I wish you were here." Wish You Were Here-Pink Floyd-1975
|
|
|
|
|
Of course no. Finding all files and deleting them is not the easiest way.
SHFILEOPSTRUCT op;
op.hwnd = AfxGetApp()->GetMainWnd()->m_hWnd;
op.pFrom = "c:\\yourfolder\\*.*";
op.pTo = "";
op.wFunc = FO_DELETE;
op.fFlags = FOF_SILENT | FOF_ALLOWUNDO;
SHFileOperation(&op)
This is better, faster and easier...
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix
|
|
|
|
|
maybe but can you delete all the subfolder with this ?
But thanks for the critics
|
|
|
|
|
Yes. Of course
If you only want to delete the files, not the subfolders, you have to add FOF_FILESONLY to your flag. But as I said, this deletes everything in the folder, including sub-folders.
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix
|
|
|
|
|
It doesn't work in WinCE . And code in the article does.
|
|
|
|
|
There may be some errors in my code. Try it on your own. I just write it. It may contain errors. But I am always using this in my apps. As I said, it is faster, more reliable and easier
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix
|
|
|
|
|
Mustafa Demirhan wrote:
op.pFrom = "c:\\yourfolder\\*.*";
I have not tested this, but I would suggest to write the pattern like '*', not '*.*'.
With the later, survivance of the Dos days, you risk to miss the files without extensions (like Makefile) or without name (like .unixhidden).
Now, I can be wrong
I was a bit skeptical about the portability of your snippet, but beside the mentioned CE problem, it seems to work on all versions of Win32. Wow.
Thank you, I added this to my collection of tips and tricks.
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
|
|
|
|
|
Philippe Lhoste wrote:
I have not tested this, but I would suggest to write the pattern like '*', not '*.*'.
With the later, survivance of the Dos days, you risk to miss the files without extensions (like Makefile) or without name (like .unixhidden).
...and I added this to my collection of tips and tricks Thank you.
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix
|
|
|
|
|
I'm just trying this SHFileOperation snippet on a mapped network drive. It dosen't seem to work in this situation.
D'Oh
|
|
|
|
|
This piece of code is great. However, I find problem using it in COM DLL.
I try to use it in a COM DLL that I'm working on. But, I do not know what to put for op.hwnd? I tried to set op.hwnd = NULL, but SHFileOperation(&op) return me an int 87, and the operation failed. There was no explaination on return values in MSDN.
What should I put in op.hwnd? Any idea?
|
|
|
|
|
Put your window's handle. If you dont have any windows, I have no idea. In fact, I thought NULL should work.
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix
|
|
|
|
|
Put the Desktop Window Handle by calling GetDesktopWindow()
I try and this is not the problem! I am not able to make work this piece of code!
Eric
Eric Forget
Windows Software Developer
|
|
|
|
|
The drawback with this is that you have no callback facility to allow the operation to be cancelled from within your app, especially if you said no progress dialog or do operation silently.
The original article allows that as you have more control to modify the code and therefore, I think, is more flexible and could be a more preferable solution depending on your requirements.
|
|
|
|