|
|
the article was very usefull to me. thanks
void RecursiveDelete( wchar_t * szPath)
{
int l = wcslen(szPath);
if ((l == 0)||((l-1)>MAX_PATH))
return;
wchar_t path[MAX_PATH]= {NULL};
wchar_t path2[MAX_PATH] = {NULL};
wchar_t path3[MAX_PATH] = {NULL};
wcscpy(path,szPath);
wcscpy(path2,path);
int i = 0;
if( path[l-1]=='\\')
{
do
{
path3[i] = path[i];
i++;
}while(i<(l-1));
wcscat(path,_T("*.*"));
}
else
{
wcscpy(path3,path);
wcscat(path2,_T("\\"));
wcscat(path,_T("\\*.*"));
}
BOOL res = TRUE;
WIN32_FIND_DATA FindFileData;
HANDLE ff = FindFirstFileW(path,&FindFileData);
while((res==TRUE) && (ff!=INVALID_HANDLE_VALUE))
{
wcscpy(path,path2);
wcscat(path,FindFileData.cFileName);
if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
{
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY )
SetFileAttributes(path,FILE_ATTRIBUTE_NORMAL);
DeleteFileW(path);
}
else if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
RecursiveDelete(path); //here we call the same function
RemoveDirectory(path);
}
res = FindNextFileW(ff,&FindFileData);
}
FindClose(ff);
RemoveDirectory(path3);
}
JCK
modified 2-Oct-11 16:55pm.
|
|
|
|
|
Hi All,
I want to develop a GUI in which the user can "Browse" and give path(s) of the file(s)he/she wants to delete. Once he clicks on RUN button all file(s) in the queue should be deleted.
Like wise similar approach is needed for multiple file creation as well. Someone please help me with a suitable VC++ code.
Sai
RockSai
|
|
|
|
|
Hello,
Can anyone tell me why I cannot delete the folder although the file was already empty?.. The files in the folder can be delete succesfully but the folder cannot be delete..
Please help me in this matter..
Thanks...
|
|
|
|
|
The matter after I delete all the files in folder then continue to delete folder without closing the application it's fail to delete it..
But if I close the application the start the application again the do the delete folder operation on the same folder it was success.. So, anyone know about this problem?..
Please...
Thanks...
|
|
|
|
|
Hi
Under VS7, this is a better loop:
while(res)
{
res = ff.FindNextFile();
if (!ff.IsDots() && !ff.IsDirectory())
DeleteFile(ff.GetFilePath());
else if (ff.IsDirectory() && !ff.IsDots())
{
path = ff.GetFilePath();
RecursiveDelete(path);
RemoveDirectory(path);
}
}
for . the ff.IsDirectory() returns true, so an infinite loop will be executed with the original code.
Jozsi
|
|
|
|
|
I used this RemoveDirectory API where if I have the files inside that folder then it willnot delete that folder..
can u please tell me why its doing so?
Santhosh.C.Rao.
|
|
|
|
|
Strangely, when I used this code, I got into an infinite loop. I made one modification and the code worked. I'm not sure why the code posted here didn't work for me when it seems to work for some many others.
I changed the line:
else if (ff.IsDirectory())
to
else if (!ff.IsDots() && ff.IsDirectory())
and my code works.
Any ideas why?
Thanks!
|
|
|
|
|
hi,
when listing a directories contents you get two additional entries:
"." & "..".
"." = current directory (ie: c:\windows\system)
".." = parent directory (ie: c:\windows)
IsDots checks if the current entry is either of those two.
the path c:\windows\system\..\.. is perfectly valid :
c:\windows\system
then up two levels then you are therefore pointing at c:\
Hope this Help,
Andy.
|
|
|
|
|
yup, it is infinite loop. I guess author need to update the codes.
|
|
|
|
|
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
|
|
|
|