It wouldn't be hard to do something like:
BOOL
MakeNewNumberedFolder(PCWSTR pcwszBaseName, PDWORD pdwFirstFree)
{
PWSTR pwszTemp = NULL;
DWORD dwChars = 0, dwCount = 2;
*pdwFirstFree = 0xFFFFFFFF;
if (CreateDirectory(pcwszBaseName)) {
return TRUE;
}
dwChars = lstrlenW(pcwszBaseName) + 3 + 10 + 1; // space, (, ), 10
possible digits in 32 bit number
pwszTemp = (PWSTR)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) *
(dwChars + 1));
if (!pwszTemp)
return FALSE;
for (dwCount = 2; dwCount < 0xFFFFFFFF; dwCount++) {
wsprintf(pwszTemp, L"%ls (%d)", pcwszBaseName, dwCount);
if (CreateDirectory(pwszTemp)) {
*pdwFirstFree = dwCount;
HeapFree(GetProcessHeap(), 0, (PVOID)pwszTemp);
return TRUE;
}
else if (GetLastError() != ERROR_ALREADY_EXISTS)
break;
}
HeapFree(GetProcessHeap(), 0, (PVOID)pwszTemp);
return FALSE;
Quote:}
So you call this with the directory you want to create, and it tries to
create the directory outright. If it succeeded, then it returns TRUE and
sets the number to -1 (DWORD style.) That means it created the directory
you wanted automatically. Otherwise, it allocates enough to hold the
directory path plus the formatting (my numbers might be off, you'll want to
do some testing), and starts wsprintf'ing the directory you want plus the
serial number into the buffer, then trying to mkdir it. If the error was
other than 'it's already there', it stops - otherwise, it goes and
incrememnts the counter and tries again.
As the .sig says, no guarantees - but this should work.
--
Jon Wiswall - Microsoft
This posting is provided AS IS with no warranties, and confers no rights.
Quote:> Does anyone know a Win32 API function which can create a
> new folder with the default name "New Folder", or if it
> already exists "New Folder (x)" (where x=2, 3, 4, ...)
> I only know of such a function for Shell Shortcuts, namely
> SHGetNewLinkInfo..... This function automatically provides
> names for (existing) shortcuts like this:
> Shortcut to Command.com.lnk
> Shortcut (2) to Command.com.lnk
> Shortcut (3) to Command.com.lnk
> etc.
> Chris Wolkenfelt
> Software Engineer
> Heidenhain Numeric B.V.
> Eindhoven, The Netherlands