Re: How to delete the folder and its files using VC++



See below...


On 4 Jan 2006 22:09:35 -0800, "Deva" <niruban.n@xxxxxxxxx> wrote:

>hi
>this nirub i want the information for deleting the folder and its
>files...
>here i attached my code deleting file but i cant delete the folder..
>if any of one give me idea for that....
****.
You can't delete the folder if any program, including the current one, has this folder
selected as its working directory.
****
>
>
>WIN32_FIND_DATA FindFileData;
> HANDLE hHandle;
> int complt;
> char FileName[100];
*****
Why would you think that 100 made sense? MAX_PATH would make sense (263), but 100 is
erroneous.

Why are you using obsolete concepts like 'char'. You should be using TCHAR
****
> strcpy(FileName,"");
*****
Why are you using non-Unicode compliant, non-buffer-overflow-safe antiques like strcpy?
Use strsafe.h and use StringCchCopy. This is particularly important because you have
erroneously chosen a FileName buffer that is far too small to hold a legal file name.

For that matter, why are you not using CString?
****
> char rpath[]="C:\\Sam";
> char Path[]="C:\\Sam\\";
****
OUCH! Why do you presume that c:\ is relevant? The number of times I've had to fix code
because the programmer did not recognize the existence of any drive other that c: is too
frequent to count.
****
> SetCurrentDirectory(Path);
****
Why are you using SetCurrentDirectory? This is almost always a Really Bad Idea, because
it changes the directory for all threads in the process. It also forces the directory to
be the working directory, thus making sure you can't delete the directory. Avoid
SetCurrentDirectory.
****
> hHandle=FindFirstFile("*.*",&FindFileData);
****
Just think of how much more reliable this could be if you wrote
CString FileName;
CString Path = _T("C:\\Sam"); // except you would fix the C: problem as described
above!

Why are you using Hungarian notation REDUNDANTLY? Gack! Doesn't anyone ever THINK any
longer?
CString findpath = Path + _T("\\*.*");
HANDLE h = ::FindFirstFile(findpath, &FindFileData);
>
>
****
The code below is needlessly complicated, such as introducing a gratuitous variable
'complt' which serves no purpose other than to take up intellectual space. In addition,
it does not check that ehe DeleteFile actually worked, and doesn't handle the case where
subdirectories are found.

if(h == INVALID_HANDLE_VALUE)
{ /* failed */
DWORD err = ::GetLastError();
...convert err to string using FormatMessage
AfxMessageBox(...useful string here telling EXACT error!...);
} /* failed */
else
{ /* worked */
do
{ /* deletion loop */
CString Filename;
FileName = Path;
FileName += _T("\\");
FileName += FindFileData.cFileName;
if(FindFileData.dwAttributes & FILE_ATTRIBUTE_DIRECTORY)
{ /* found directory */
.. deal with recursive deletion or report an error
} /* found directory */
else
{ /* found file */
if(!::DeleteFile(FileName))
{ /* deletion failed */
DWORD err = ::GetLastError();
...convert err to string using FormatMessage...
....report reason deletion failed...
} /* deletion failed */
} /* found file */
} /* deletion loop */
while(FindNextFile(h, &FindFileData);
::FindClose(h);
} /* worked */
******
> if(hHandle == INVALID_HANDLE_VALUE)
> MessageBox("Path NotFound");
> else
> {
> while(complt)
> {
> strcpy(FileName,Path);
> strcat(FileName,FindFileData.cFileName);
> DeleteFile(FileName);
> complt=FindNextFile(hHandle,&FindFileData);
> }
> }
> FindClose(hHandle);
*****
This seems odd. Why are you deleting the directory then re-creating it? Having just
deleted all the files in it, what purpose is served by deleting it and re-creating it?
Note that if there was a directory, or a file which could not be deleted, you could not
RemoveDirectory; in addition, because you erroneously did a SetCurrentDirectory, you are
absolutely guaranteed that you could not delete the directory.

You should not treat ints as BOOL values. Read the documentation. It is fairly
important. Note that RemoveDirectory is defined as
BOOL RemoveDirectory(LPCTSTR lpPathName);

I don't see where this allows you to declare an int variable to hold the result, or to
test an int as if it were a boolean.
****
> int d=RemoveDirectory(rpath);
> if(d)
> MessageBox("Deleted");
> else
> MessageBox("Sorry i cant");
> int i=CreateDirectory(rpath,NULL);
> if(i)
> MessageBox("Created");
> else
> MessageBox("Sorry i cant");
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Deleting files
    ... To elaborate a bit more on the subject, on a FAT file system, deleting ... delete files on CE other then DeleteFile(). ... I am trying to create an app that limits the number of files in a folder ...
    (microsoft.public.windowsce.embedded)
  • Re: Rolling back folders delete using VBA
    ... created at run time) with same sub folder structure. ... if we come accross an error while deleting a file from original location. ... Dim oSubFolder As Object ... Dim oFile As Object ...
    (microsoft.public.excel.programming)
  • Re: "delete file" takes forever to finish
    ... Folder contents ... test for that is Nirsoft's free Shell Integration Viewer. ... deleting a file may require the corresponding thumbnail or index entry ...
    (microsoft.public.windowsxp.general)
  • Re: library database folders dont exist
    ... folder as opposed to deleting the file. ... directory and that the media player would recreate the files and work, ... problem i have is that the directory doesnt even exist. ...
    (microsoft.public.windowsmedia.player)
  • Re: Property tags - what happens if... (Rename of user defined fie
    ... If a user property is added only to individual items then deleting it from each item where it exists will completely remove it. ... If it was also added to the folder then even deleting every item in the folder won't remove it from ... That would have to be done using a different API such as Extended MAPI or CDO 1.21 or Redemption to get at the folder fields. ... you and store how successful you were back so that it examines you more often ...
    (microsoft.public.outlook.program_vba)