Archived

This topic is now archived and is closed to further replies.

Hippokrates

Deleting files

Recommended Posts

I am currently writing an archive system for my engine. And to save an archive that already exists and was now modified I have to extract its content first and then stuff it all back again. I am extracting the files to a temporary directory but I don`t know how to get rid of it afterwards. MSDN tells me that RemoveDirectory only works with empty ones. I tried to delete all the files the directory contains using FindFirst/Findnext, but oddly the function deleted the files in my work directory (where the executable is) and not in the temporary directory. THis is what I am doing now:
inline void DeleteDirectory(LPSTR strPath) {
	HANDLE hFile;
	WIN32_FIND_DATA FindData;
	char strBuffer[_MAX_PATH + 4];

	sprintf(strBuffer, "%s\\*.*", strPath);

	hFile = FindFirstFile(strBuffer, &FindData);

	if(hFile != INVALID_HANDLE_VALUE) {
		if(''.'' != FindData.cFileName[0]) {
			DeleteFile(FindData.cFileName);
		}

		while(FindNextFile(hFile, &FindData)) {
			if(''.'' != FindData.cFileName[0]) {
				DeleteFile(FindData.cFileName);
			}
		}
		FindClose(hFile);
	}
	RemoveDirectory(strPath);
}
 
Any suggestions?

Share this post


Link to post
Share on other sites
the findData.cFileName member only contains the name of the file, not the path. So you want to change you loop to something like this:


string fullpath = string(strPath) + "\\";
hFile = FindFirstFile( strBuffer, &findData );
do
{
    if( '.' != strBuffer[0] )
    {
        DeleteFile( (fullpath + findData.cFileName).c_str() );
    }
} while( FindNextFile( hFile, &findData ) );


Edit: Oh, and I'm not sure about your test for the '.' and '..' directories, what if you had a regular file who's name started with '.'?

If I had my way, I'd have all of you shot!


codeka.com - Just click it.

[edited by - Dean Harding on October 3, 2002 4:23:23 PM]

Share this post


Link to post
Share on other sites
Does the directory you''re deleting has sub directories? I assume not from seeing your code because it''s not recursive. Instead of using ''.'' to filter the . and .. directory, use FILE_ATTRIBUTE_DIRECTORY instead.

if (!(FindData.FileAttribute & FILE_ATTRIBUTE_DIRECTORY))
{
// delete file
}

I forgot the member name for attribute, but you got the idea.

Share this post


Link to post
Share on other sites

    
void DeleteDirectory(LPCSTR lpszPath)
{
char szFilter[MAX_PATH];
sprintf(szFilter, "%s\\*", lpszPath);
WIN32_FIND_DATA fd;
HANDLE hFindFile = ::FindFirstFile(szFilter, &fd);
if (hFindFile != INVALID_HANDLE_VALUE)
{
BOOL bDone = FALSE;
while (!bDone)
{
if (fd.cFileName[0] != '.')
{
char szFileName[MAX_PATH];
sprintf(szFileName, "%s\\%s", lpszPath, fd.cFileName);
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
DeleteDirectory(szFileName);
}
else
{
::DeleteFile(szFileName);
}

}
bDone = ! ::FindNextFile(hFindFile, &fd);
}
::FindClose(hFindFile);
}

::RemoveDirectory(lpszPath);
}



Finally found out how to use the source tag.

- Mete


[edited by - Mete on October 3, 2002 4:41:20 PM]

Share this post


Link to post
Share on other sites
Argh! No!
I tried this solution and my programme stopped deleting the wrong files.
But it did not start to delete the right ones.
My temporary files are still there afterwards
This is my code:

inline void DeleteDirectory(LPSTR strPath) {
HANDLE hFile;
WIN32_FIND_DATA FindData;
char strBuffer[_MAX_PATH + 4];

sprintf(strBuffer, "%s\\*.*", strPath);

hFile = FindFirstFile(strBuffer, &FindData);

if(hFile != INVALID_HANDLE_VALUE) {
if(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
sprintf(strBuffer, "%s\\%s", strPath, FindData.cFileName);
DeleteFile(strBuffer);
}

while(FindNextFile(hFile, &FindData)) {
if(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
sprintf(strBuffer, "%s\\%s", strPath, FindData.cFileName);
DeleteFile(strBuffer);
}
}
FindClose(hFile);
}
RemoveDirectory(strPath);
}

I looked at the stuff in debug mode and all the file pathes seemed to be correct...

Share this post


Link to post
Share on other sites
Could it be related to the security attributes?
I create my directory like this:

CreateDirectory(strPath, NULL);

If I want to delete files from that directory and remove it afterwards, do I have to set any attributes ?

Share this post


Link to post
Share on other sites