Click to See Complete Forum and Search --> : removing directory
rocky
May 17th, 1999, 11:58 AM
Hello!
Does anybody know how to remove a directory with files in it?
"RemoveDirectory("<dirname>")" works only when the directory is empty...
please help me.
thanks in advance
scp
May 17th, 1999, 01:23 PM
Delete all the files in the directory, before removedirectory is called...
Santhosh
Jason Brooks
May 17th, 1999, 01:33 PM
Or perhaps a more constructive comment would be
DELTREE <directory>
Jason
http://www.netcomuk.co.uk/~jbrooks
scp
May 17th, 1999, 03:34 PM
Can we call Deltree from VC++ program? If so, can you please tell me the syntax?
Jason Brooks
May 17th, 1999, 03:41 PM
Sorry,
You can use either of the following:-
system( "deltree c:\\temp\\test" );
or WinExec("deltree c:\\temp\\test" );
Jason
http://www.netcomuk.co.uk/~jbrooks
BrianOG
May 18th, 1999, 02:41 AM
Here is a function that will remove a directory (including ALL files and ALL subdirectories// NukeDirecrory - what RemoveDirectory should be, removes ALL files & sub-dirs within
// strDir, and then removes strDir
void NukeDirectory(const CString &strDir)
{ // dont do anything if no string
if ( !strDir.GetLength() ) return;
// if last 2 chars are :\ dont do anything (this would be c:\ or d:\ or something similar)
if ( strDir == "\\" || strDir.Right(2) == ":\\" ) return;
// do a findfirst/findnext on all files in the directory
WIN32_FIND_DATA findData;
CString _strDir = strDir;
if ( '\\' != _strDir.Right(1) )
_strDir += "\\";
HANDLE hFile = FindFirstFile(_strDir + "*.*", &findData);
if ( INVALID_HANDLE_VALUE == hFile )
return;
do
{ // if we get a directory, and its not "." or ".." recursively call NukeDirectory
if ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
if ( strcmp(findData.cFileName, ".") && strcmp(findData.cFileName, "..") )
NukeDirectory(_strDir + findData.cFileName);
}
else
{ // or if its a file, remove it
SetFileAttributes(_strDir + findData.cFileName, FILE_ATTRIBUTE_NORMAL);
DeleteFile(_strDir + findData.cFileName);
}
} while ( FindNextFile(hFile, &findData) ); // next item
// close the findfirst handle
FindClose(hFile);
// remove the directory
RemoveDirectory(strDir);
}
Be *VERY* careful when using this function, I once wiped my 2Gig D: drive with it (but have since added the check for '\' and '?:\')
Hope this helps.
John_Reese
June 10th, 1999, 03:59 AM
Hey. I'm having probelm with the a remove directory function. I'm using your NukeDirectory code, and it works great on Win9x. But it's leaving two folders of the last active directory in NT4. Do you have any idea what could be causing this? I've checked and double checked to make sure the current directory is not interfering. I'm not sure what else could cause such behavior. Any ideas or help will be greatly appreciated...
BrianOG
June 10th, 1999, 04:16 AM
I have seen this before, but usually the directory cannot be deleted by anything.
For eaxmple, but a breakpoint just after the call to NukeDirectory and when your prog stops, try deleting the dir with dos/windows, and see if it can be deleted. It is usually something to do with processes using the directory at the time.
John_Reese
June 10th, 1999, 04:28 AM
You are correct. Windows performs exactly the same way as the function. Which adds quite a new twist since I must delete these folders. But at least now I know the problem...Thanks, and if you have any ideas for a workaround, they are more than welcome...
BrianOG
June 10th, 1999, 05:01 AM
Well usually, and reboot and then call NukeDirectory again does the trick :-)
AfxMessageBox("Please reboot your computer and run this program again.... ;-(", MB_ICON_AH_NO);
no, afraid I have no serious solution... other than double check your application to see if it could possible have some sort of handle on the dir (also, check to see if you can del the dir via windows after your applic closes... this would hint at the fact that it is your applic that is preventing the deletion).
Brian
John_Reese
June 10th, 1999, 05:12 AM
Ok, thanks for the help...Gives me somewhere to work from...
Jason Teagle
June 10th, 1999, 06:36 AM
If another program had a file from these directories open, or Windows Explorer itself was displaying the contents of those directories, or Windows itself was using a DLL or similar from that directory, that would cause the problem. You can't delete directories while files from them are still open.
One way round it is, when you detect a directory which cannot be removed, you check the individual files by trying to open them with exclusive access. If they are in use (the open fails), you can politely ask the user to release them.
John_Reese
June 10th, 1999, 08:25 AM
Thanks for the response, but I'm afraid that angle is covered. The software is really user-friendly, so they don't actually have the ability to interfere with these file processes. It's all done behind the scenes for them, both accessing and releasing. But more importantly, the NukeDirectory function deletes all the files successfully. It only leaves the project directory folder and the \Sch (schematics) folders. Something is accessing the \sch, but I haven't been able to determine what. It shouldn't be the software, but anything is always possible. I appreciate the response...Please keep ideas coming if you (or anyone else reading this) have any...
John Simmons
Jason Teagle
June 10th, 1999, 08:49 AM
OK. I see you have thought about files being open already. What about the directory itself? If, for example, Windows Explorer is displaying the contents of that directory in the right-hand pane, you will probably not be able to delete it - although as it is similar to files being displayed, I'm sure you've covered this also.
My only other thought is that the NukeDirectory() function is still somehow hogging the directory (because it grabbed it to delete its contents)? I find it hard to understand how it can successfully release and delete other directories and not these particular two, but it's a possibility. Perhaps you should put breakpoints on the places where the \sch directory could be accessed, and make sure it is being released correctly.
Something has just occurred to me - if you have used _chdir to set the current directory to this \sch directory, that could well be what's preventing it from being deleted. I would recommend getting the current working directory by using _getcwd() at the start of the program, and then restoring to this before trying to nuke the directories.
John_Reese
June 10th, 1999, 09:39 AM
We've pretty much come to the same conclusion here: That either 1.) The Delete function isn't letting the directory go or 2.) Some other part of our software or NT itself is accessing the directory when it's not supposed to. Now we just have to find which is the case and how to fix it. I'm leaning towards 2., mainly because the recursive process for NukeDirectory should work. My other reason for thinking it is either a software or NT problem is that the function works flawlessly on Win9x. However, all possibilities are still open as far as I'm concerned.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.