Search code examples
c++cwindowspathname

Program to Create and Move a Pathname with more than 260 characters in Windows


The aim is to code a utility in C++ using the function:

BOOL WINAPI CreateDirectoryW(_In_ LPCTSTR lpPathName, _In_opt_LPSECURITY_ATTRIBUTES lpSecurityAttributes)

The only successful attempt found so far appears to be a in a Perl script, but getting that to work is another question. This script trials the prefix

my $path = '\\\\?\\' 

but it has been observed elsewhere that using "\\?\UNC\" is more reliable. Any code blocks would be welcome.

Edit: Also, as the original question title indicated, the problem is moving the folder to another location (other than a relative path). Can this path be moved with MoveFileEx?


Solution

  • The following is from the MSDN documentation on the CreateDirectory function.

    There is a default string size limit for paths of 248 characters. This limit is related to how the CreateDirectory function parses paths.

    To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\?\" to the path. For more information, see Naming a File.

    Note that in C++, as in Perl, it is necessary to escape the \ characters in the source code unless you use Raw String Literals. Thus it would be "\\?\" in the source code.

    Here is a quick example of how to do it.

    BOOL CreatDirWithVeryLongName()
    {
        BOOL ret = ::CreateDirectoryW(L"\\\\?\\C:\\This is an example directory that has an extreemly long name that is more than 248 characters in length to serve as an example of how to go beyond the normal limit - Note that you will not be able to see it in Windows Explorer due to the fact that it is limited to displaying files with fewer than 260 characters in the name", NULL);
        return ret;
    }