0

This is a sample code i have written to check if i am able to create a folder with name length greater than MAX_PATH -

wstring s = L"D:\\Test";
wstring s2 = L"\\?\D:\\datafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffr700000000000000datafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffr700000000000000datafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffr700000000000000datafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffr700000000000000datafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffr700000000000000";

int ret = CreateDirectoryEx(s.c_str(), s2.c_str(), NULL);

int error = GetLastError();

It did not work, the returned error is ERROR_PATH_NOT_FOUND. Can anyone please tell me whats the problem in the code?

Note: "D:\Test" folder is an existing folder. I am using Windows 7.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Rasheduzzaman Sourov
  • 1,375
  • 2
  • 15
  • 36
  • look for [this](https://stackoverflow.com/a/265782/6401656) - *Individual components of a filename (i.e. each subdirectory along the path, and the final filename) are limited to 255 characters* – RbMm Jul 06 '17 at 07:35
  • @RbMm Please check the edit. – Rasheduzzaman Sourov Jul 06 '17 at 07:58
  • Please don't ask new questions in an edit. This one is done. You should upvote answers by xMRi and RbRm and accept that of xMRi. Don't edit the question to change the subject matter that has already been addressed. – David Heffernan Jul 06 '17 at 07:59
  • As for the contents of your edit, almost certainly the parent directory in your destination did not exist. Which is what the error told you. You need to look at the error codes. – David Heffernan Jul 06 '17 at 08:00

3 Answers3

5

need not confuse Maximum file name length (path component) and Maximum path length - see Limits

the Maximum file name length is <= 255 Unicode characters for all file systems

and Maximum path length 32,760 Unicode characters with each path component no more than 255 characters


initial error was by using L"\\?\" prefix - really it must be L"\\\\?\\" because c/c++ translate "\\" to \ - but this already only language specific error.

if fix it - must be error ERROR_INVALID_NAME (converted from NTSTATUS STATUS_OBJECT_NAME_INVALID ) because path component which you use more than 255 characters

RbMm
  • 31,280
  • 3
  • 35
  • 56
  • 1
    That would not result in `ERROR_PATH_NOT_FOUND` surely. So once the escaping is fixed, that's the next problem I guess. – David Heffernan Jul 06 '17 at 07:48
  • @DavidHeffernan - yes, I miss at begin that \\?\ used instead \\\\?\\. error must be `ERROR_INVALID_NAME` (or NTSTATUS `STATUS_OBJECT_NAME_INVALID`) if fix tis typo -anyway name exceeds 255 characters – RbMm Jul 06 '17 at 08:03
  • If you look at the edit history, and comments to the other answer, then this is exactly what happens. First of all he tried with incorrect escaping, and got `ERROR_PATH_NOT_FOUND`. Then he fixed that and saw `ERROR_INVALID_NAME`. Then he fixed that with multiple directories all with length less than 255 and got back to `ERROR_PATH_NOT_FOUND` because the directories did not exist. – David Heffernan Jul 06 '17 at 08:05
  • I just thought this answer was more helpful to me. The first error was trivial and i fixed that after posting this question. After this answer i realize the actual problem in the code and my approach to create long directory. – Rasheduzzaman Sourov Jul 06 '17 at 08:13
  • 2
    `CreateDirectory/Ex()` does not create missing intermediate folders. If you try to create `D:\x\y\z` and `x` or `y` do not exist, the function fails. The documentation says so: "*`ERROR_PATH_NOT_FOUND` One or more intermediate directories do not exist. **This function only creates the final directory in the path**.*" So, you would have to create `D:\x` first, then create `D:\x\y`, then create `D:\x\y\z`, ignoring any `ERROR_ALREADY_EXISTS` errors along the way. Or, do what the documentation says: "*To create all intermediate directories on the path, use the `SHCreateDirectoryEx` function*". – Remy Lebeau Jul 07 '17 at 00:38
3

Because the syntax is simply wrong. You have to escape the backslash. So the prefix should be L"\\\\?\\".

wstring s2 = L"\\\\?\\D:\\dataff...";
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
xMRi
  • 14,982
  • 3
  • 26
  • 59
  • I did that actually. I was getting `ERROR_INVALID_NAME` error if i was using "\\\\?\\". – Rasheduzzaman Sourov Jul 06 '17 at 07:53
  • @RasheduzzamanSourov Trial and error is no way to program is it. You know that \ is an escape character in a string. Are you hoping that the compiler will forget this fact? You know that `L"\\\\?\\"` is correct, so if you have a problem, it must be elsewhere. And in this case that elsewhere is explained by RbRm's answer. This answer is the correct one. It answers the question you asked. It explains why you encounter `ERROR_PATH_NOT_FOUND`. The `ERROR_INVALID_NAME` is a subsequent issue. – David Heffernan Jul 06 '17 at 07:56
  • Alternatively use C++11 raw string literal so you don't need to escape: `LR"(\\?\D:\dataff.....)"` – zett42 Jul 06 '17 at 08:20
-1

Because the path sizes are limited (to 160 caracters I think on W7 but not sure)

user6106573
  • 193
  • 2
  • 11