 |
|
 |
Maybe it could be usefull to change :
while (*p != '\\')
to
while (*p != '\\' && *p != '/')
to be able to process paths with '/'.
Good class tho, very very usefull .
|
|
|
|
 |
|
 |
And i also suggest a i=0; in the code below to avoid a 10 sub-directories limitation, but keeping your 10 times for :
// Try 10 times to create the directory
for (int i=0;i<10;i++)
{
BOOL bOK = CreateDirectory(szFolder, NULL);
DWORD dwLastError = GetLastError();
if (!bOK && dwLastError == ERROR_PATH_NOT_FOUND)
{
while (*p != '\\' && *p != '/')
{
if (p == pStart)
return FALSE;
p--;
}
*p = NULL;
i=0;
}
else if (bOK || (ERROR_ALREADY_EXISTS == dwLastError))
{
if (p == pEnd)
return TRUE;
*p = '\\';
while (*p)
p++;
i=0;
}
else
break;
}
|
|
|
|
 |
|
 |
... Just what i was looking for. You save me some time.
Thanks. ive given you a 5.
|
|
|
|
 |
|
 |
if you have your paths stored in CStrings then what?
I tried GetBuffer but() it still didn't work.
|
|
|
|
 |
|
 |
I don't understand your question.
|
|
|
|
 |
|
 |
I meant the CopyFolder() which uses SHFileOperation() requires LPCTSTR params for the From and To. If my paths were assigned to CString vars how would one pass this to the CopyFolder() so it would work?
At first I tried csPath.GetBuffer() trying to convert CString to LBCTSTR but then realized that it could be passes as char * BUT it needs a NULL termination which I neglected at first.
|
|
|
|
 |
|
 |
You can pass CString object as a parameter that get LPCTSTR since CString has this operator. (There is no need to call .GetBuffer method).
|
|
|
|
 |
|
 |
hi
I tried this code for Pocket PCs with windows CE 5.0 OS. I got ERROR_ACCESS_DENIED error when tried Directory copy.
Can I use SHFileOperation function in WinCE?
Also suggest the format for pFromFolder and pToFolder arguments passed to CopyFolder function.
-mahesh
|
|
|
|
 |
|
 |
I put this line: #include in your file. Work fine.
|
|
|
|
 |
|
 |
not bad, but I didn't like the for loop in CreateFolder.
Try this slight improvment - comments welcome
BOOL CreateFolderFromDirectory(LPCTSTR lpszdirectory)
{
// recursively try to create the directory
if( CreateDirectory(lpszdirectory, NULL) == TRUE )
{
return TRUE;
}
switch( GetLastError() )
{
case ERROR_PATH_NOT_FOUND:
{
// get the parent directory
TCHAR szParent[MAX_PATH];
memset((void*)szParent, 0, sizeof(szParent));
TCHAR* pLast = _tcsrchr(lpszdirectory, _T('\\'));
if(!pLast)
return FALSE;
// no need to account for TCHAR differences here
int dirlen = (pLast - lpszdirectory);
if(dirlen <= 0)
return FALSE;
_tcsncpy(szParent, lpszdirectory, dirlen);
if(CreateFolderFromDirectory(szParent) == TRUE)
{
// if we could create the parent dir, then go ahead and
// try to create the input directory again
return CreateDirectory(lpszdirectory, NULL);
}
break;
}
case ERROR_ALREADY_EXISTS:
return TRUE;
default:
return FALSE;
}
return FALSE;
}
BOOL CreateFolderFromPath(LPCTSTR lpszpath)
{
TCHAR szDirectory[MAX_PATH];
memset((void*)szDirectory, 0, sizeof(szDirectory));
TCHAR* pLast = _tcsrchr(lpszpath, _T('\\'));
if(!pLast)
return FALSE;
// no need to account for TCHAR differences here
int dirlen = (pLast - lpszpath);
if(dirlen <= 0)
return FALSE;
_tcsncpy(szDirectory, lpszpath, dirlen);
return CreateFolderFromDirectory(szDirectory);
}
|
|
|
|
 |
|
 |
The idea was to avoid recursion calls.
|
|
|
|
 |
|
 |
In addition to the potential overflow in GetSpace mentioned in another comment, there are a few issues with your source code that need to be addressed:
UNICODE: Although most parts of the code can be compiled to UNICODE, there are a few glitches. In RemoveFolder the string literals "." and ".." aren't wrapped in respective _T() macros. Same goes for all character literals in CreateFolder and GetSpace.
GetSpace fails on directories starting with a "." (like .x, which is a valid directory name). Less obvious, GetSpace is a naive implementation that ignores quite a few situations that do exist. For further information on this, follow this link[^].
GetSpace and RemoveFolder fail when left with an infinitely recursive directory structure. More information on this can be found here[^].
.f
|
|
|
|
 |
|
 |
Thanks for you comments.
An updated code has been posted regrading to the string literals "." and ".." in RemoveFolder and regarding to directories starting with a "." .
As for the issues discussed in the links that you mentioned, i will check it later on.
Dudi
|
|
|
|
 |
|
 |
Please add a line to your article stating whether your code uses MFC or is purely built on the Win32 API.
|
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Dear Dudi,
maybe you should reconsider some of your methods :
For example the method GetSize returns an just a DWORD.
But DWORD is defined as unsigned long. This means you have just
4 bytes wit a range of values from 0 to 4,294,967,295 for holding
the size of a hole directory???
My smallest hardisk is 10 times bigger, so there are many directories
which contains more than 4GB...
Best regards,
Markus Hlacer
|
|
|
|
 |