 |
|
 |
Clean and simple. Thanks.
|
|
|
|
 |
|
 |
Just what the doctor ordered, a big 'thank you'.
Regards,
Mohammad Elsheimy
---------------------------
Just Like a Magic
http://JustLikeAMagic.Wordpress.com
|
|
|
|
 |
|
 |
You are Welcome .
Feroz Zahid
|
|
|
|
 |
|
 |
//Delete file
Slightly modified code as:
else
{
sprintf(fileFound,"%s\\%s", folderPath, info.cFileName);
BOOL retVal = DeleteFileA(fileFound);
if(0 == retVal)
{
SetFileAttributesA(fileFound, FILE_ATTRIBUTE_NORMAL);
DeleteFileA(fileFound);
}
}
Regards,
Anand Choubey
|
|
|
|
 |
|
 |
Thanks Anand Choubey for your input.
Regards.
Feroz Zahid
|
|
|
|
 |
|
 |
DeleteDirectory() misses the first file as it calls FindNextFile() without looking at the file returned by FindFirstFile(). Also there should be no need for the inner call to RemoveDirectory() as that's handled by the call at the end of the function.
|
|
|
|
 |
|
 |
Dear Friends,
Using your sample code for deleting a directory along with sub-folders, is very good and working fine. I want a function which will delete all the empty sub-folders in a particular directory. Can anybody help me to achieve this, i just want to delete all the empty sub-folders in a directory.
Thanks
Datta K
|
|
|
|
 |
|
 |
BOOL DeleteDirectory(const TCHAR* sPath)
{
HANDLE hFind; // file handle
WIN32_FIND_DATA FindFileData;
TCHAR DirPath[MAX_PATH];
TCHAR FileName[MAX_PATH];
_tcscpy(DirPath,sPath);
_tcscat(DirPath,_T("\\"));
_tcscpy(FileName,sPath);
_tcscat(FileName,_T("\\*")); // searching all files
hFind = FindFirstFile(FileName, &FindFileData); // find the first file
if( hFind != INVALID_HANDLE_VALUE )
{
do
{
if( IsDots(FindFileData.cFileName) )
continue;
_tcscpy(FileName + _tcslen(DirPath), FindFileData.cFileName);
if((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
// we have found a directory, recurse
if( !DeleteDirectory(FileName) )
break; // directory couldn't be deleted
}
else
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
_chmod(FileName, _S_IWRITE); // change read-only file mode
if( !DeleteFile(FileName) )
break; // file couldn't be deleted
}
}while( FindNextFile(hFind,&FindFileData) );
FindClose(hFind); // closing file handle
}
return RemoveDirectory(sPath); // remove the empty (maybe not) directory
}
|
|
|
|
 |
|
 |
This was just what I was after. Worked perfectly. Mucho Gratiz!
|
|
|
|
 |
|
 |
Hello
I tried your code on a Windows CE device. WinCE does not use drive letters and does not list the special files "." and "..". So your code failes everytime for the first file found, as it does not work with the first file found. I had to rearrange the code to:
hFile = FindFirstFile(pattern, &fdata)
do {
.. some operations on the found file
}while (FindNext(hFile, &fdata))
With this loop also the first file is processed by the operations.
Regards
Josef
|
|
|
|
 |
|
 |
Thanks Josef for pointing out; I'll check out this for Windows CE and will update the code accordingly.
Regards.
Feroz Zahid
|
|
|
|
 |
|
 |
I've made some adjustements to use it on WinCE :
BOOL DeleteDirectory(const TCHAR* sPath) {
WIN32_FIND_DATA ffd;
LARGE_INTEGER filesize;
TCHAR szDir[MAX_PATH];
TCHAR FileName[MAX_PATH];
size_t length_of_arg;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError=0;
StringCchLength(sPath, MAX_PATH, &length_of_arg);
if (length_of_arg > (MAX_PATH - 3))
{
return FALSE;
}
StringCchCopy(szDir, MAX_PATH,sPath);
StringCchCat(szDir, MAX_PATH, TEXT("\\*"));
StringCchCopy(FileName, MAX_PATH,sPath);
StringCchCat(FileName, MAX_PATH,TEXT("\\"));
// Find the first file in the directory.
hFind = FindFirstFile(szDir, &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
return FALSE;
}
// List all the files in the directory with some info about them.
do
{
_tcscat(FileName,ffd.cFileName);
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
DeleteDirectory(FileName);
RemoveDirectory(FileName);
StringCchCopy(FileName, MAX_PATH,sPath);
StringCchCat(FileName, MAX_PATH,TEXT("\\"));
}
else
{
DeleteFile(FileName);
StringCchCopy(FileName, MAX_PATH,sPath);
StringCchCat(FileName, MAX_PATH,TEXT("\\"));
}
}
while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
return dwError;
}
please Visit http://www.paslatek.net/deacutesinstallation-custom-pour-windows-mobile-customaction-for-windows-mobile-20091106-25.aspx[^] for a more complete sample (in french but code is universal)
|
|
|
|
 |
|
 |
Thanks for your addition, paslecode.
Regards.
Feroz Zahid
|
|
|
|
 |
|
 |
Code doesn't work, if the initial folder is empty.
Changed it to:
...
hFind = FindFirstFile(szDir, &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
if (ERROR_NO_MORE_FILES == GetLastError())
{
return RemoveDirectory(sPath);
}
}
...
|
|
|
|
 |
|
 |
This has been checked earlier.
hFind = FindFirstFile(DirPath,&FindFileData); // find the first file
if(hFind == INVALID_HANDLE_VALUE) return FALSE;
Regards.Feroz Zahid
|
|
|
|
 |
|
 |
I noticed that you were mixing Windows API with standard C library calls. To make this use Windows API exclusively change _tcscpy and _tcscat to lstrcpy and lstrcat respectively, _tcscmp to lstrcmp, and _chmod() to SetFileAttributes(FileName,FILE_ATTRIBUTE_NORMAL). After that you will not need the CRT (C Runtime Library).
Again, Thanks.
|
|
|
|
 |
|
 |
Thanks m. bergman for your input.
You're right the described function can be modified the stated way for not using CRT functions.
|
|
|
|
 |
|
 |
IMHO it should fail if you get passed C: or another root folder. Or at least throw in an assert to check for such instances.
Todd Smith
|
|
|
|
 |
|
 |
Here' what MSDN says about it: you can't pass root directory names to the FindFirstFile() as C: or D: but you can use C:\\* for searching all files on the root folder but offcourse it won't delete C:, it will just delete all files and folders in C:. And that's what I've done.
Anyway thanks for you input.
Feroz Zahid
|
|
|
|
 |
|
 |
If the directory (or any subdirectories) contains any read only files, your method will fail to delete the directory. If you wish to handle read only files, add the following before calling "DeleteFile()":
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
_chmod(FileName, _S_IWRITE);
Kacee
|
|
|
|
 |
|
 |
Thanks Kacee Giger for a very good suggestion.
I'll update the code for read-only files.
Feroz Zahid
|
|
|
|
 |
|
 |
Code updated for read-only files.
Feroz Zahid
|
|
|
|
 |
|
 |
for file operations like deleting, renaming, creating etc, windows has provided n no of apis
SHFileOperation(...) will do what you are trying to do.
-prakash
|
|
|
|
 |
|
 |
Could we atleast give Reconization to Coder for this Api instead of crtising him. i know there are number of api for deleting file/directory excluding the ShfileOperation as it good to see some body is doing something different......
I know you are person who like to do thing thing diffrently,but i am sorry if any quote ny me hurts you.
-----------------------------
"I Think this Will Help"
-----------------------------
Alok Gupta
visit me at http://www.thisisalok.tk
|
|
|
|
 |
|
 |
How is it doing different ? shfileoperation does the samething what this article intends to do!
Does it hurt me? no, i am open for any logical and good debate.
-prakash
|
|
|
|
 |