How to create a folder with the name "New Folder (x)"

How to create a folder with the name "New Folder (x)"

Post by Chris Wolkenfel » Tue, 11 Dec 2001 19:36:06



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

 
 
 

How to create a folder with the name "New Folder (x)"

Post by Jon Wiswall [MS » Fri, 14 Dec 2001 16:10:41


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


 
 
 

How to create a folder with the name "New Folder (x)"

Post by coriorda » Fri, 14 Dec 2001 20:46:31


Heres some code I used to make my own "naming collision" functionality.

Hope it helps.

regards,
C.

// begin code

WIN32_FIND_DATA FindFileData;
 HANDLE hFind;
 CString szFindFile, szTempFolder;
 int nFileIdx = 1;

 if(0xFFFFFFFF == GetFileAttributes(szDestFolder)) // does the path exist
  return FALSE;

 if('\\' != szDestFolder.GetAt(szDestFolder.GetLength() -1)) // add '\' to
the end of the path if needed
  szDestFolder += _T("\\");

// New Folder
 szFindFile.LoadString(IDS_FOLDERNAME_DEFAULT);

 while(1)
 {
  if(nFileIdx > 1)

   CString szFileIdx;
   szFileIdx.Format(_T("%d"),nFileIdx);
   // New Folder %1
   szFindFile.LoadString(IDS_FOLDERNAME_DEFAULT_NUMBERED);
   szFindFile.Replace(_T("%1"), szFileIdx);
  }

  szTempFolder = szDestFolder;
  szTempFolder += szFindFile;

  hFind = FindFirstFile(szTempFolder, &FindFileData);

  if(hFind == INVALID_HANDLE_VALUE) // break on the first non-existent
folder
   break;

  if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  {
   if(szFindFile == FindFileData.cFileName)
    nFileIdx++;
  }
 }

 m_strDefaultFolderName = szFindFile;
// end code


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

 
 
 

1. Quark 4.0 creating "Can't find required volume or folder" error on OS8

Bad craziness!

On two Mac G3 systems in my office (running OS8), coworkers are getting
this error from the System when they try to save an open document in Quark
4.0:

Can't find required volume or folder (-35)

This makes saving impossible.

The document they are trying to save is merely on the desktop, not on any
mounted servers. I know it's an OS error (-35), but is it Quark being evil
or the system?

After doing a "Find File" on the Quark document in question, it appeared in
the Find window where it had been saved all along. After double clicking it
in the Find window, the system activated the Quark document (which had been
open and unsavable during the whole ordeal) then allowed a normal save.

What gives?

Thanks,

*************************************************************************
"Every heart has its secret sorrows which the world knows not
and oftentimes we call a man cold when he is only sad."
                                                     -- Henry Wadsworth
Longfellow

-- The opinions of Joe Murphy
do not reflect those of Hispanex, Inc.
or its subsidiaries --

2. Postscript on HPDJ520 HELP!

3. creating "folder" desktop

4. Dixons & Link: Something to Watch

5. a Unicode folder name causes "pragma once" to be ignored

6. Marshalling IAdviseSink::OnDataChange() - eating memory problem

7. SendTo "new compressed folder" crashes (winXP)

8. Microsoft's server configuration...

9. !!!!!"""""IMPORTANTE"""""!!!!!

10. K7s5a """"ich krieg die KRIESE"""""

11. Pagekeeper Pro. "Smart Folders"

12. Adobe folder in "my files"

13. Changing default "Save As" folder with Acrobat PDF Writer