powershelllong-path

Create file inside over 260 characters folder via AlphaFS module


I have a folder with over 260 characters. I want to create a sample text file in this directory with AlphaFS module. but I get error like below.

Error message:

Cannot convert argument "bufferSize", with value: "c:\test1\test2\test3\test4\test5\test6\test7\test8\test9\test10\test11\test12\test13\test14\test15\test16\tes
t17\test18\test19\test20\test21\test22\test23\test24\test25\test26\test27\test28\test29\test30\test31\test32\test33\test34\test35\test36\test37\test38\test39\te
st40\test41\test42\test43\test44\test45\test46\test47\test48\test49\test50\test51\test52\test53\test54\test55\test56\test57\test58\test59\test60\", for 
"Create" to type "System.Int32": "Cannot convert value "c:\test1\test2\test3\test4\test5\test6\test7\test8\test9\test10\test11\test12\test13\test14\test15\test1
6\test17\test18\test19\test20\test21\test22\test23\test24\test25\test26\test27\test28\test29\test30\test31\test32\test33\test34\test35\test36\test37\test38\test
39\test40\test41\test42\test43\test44\test45\test46\test47\test48\test49\test50\test51\test52\test53\test54\test55\test56\test57\test58\test59\test60\" to type 
"System.Int32". Error: "Input string was not in a correct format.""
At line:1 char:1
+ [Alphaleonis.Win32.Filesystem.File]::Create('test.txt ',$folderTree)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Script :

1..60 | foreach {$folderTree = 'c:\'} {$folderTree += "test$_\"}
[Alphaleonis.Win32.Filesystem.Directory]::CreateDirectory($folderTree)

[Alphaleonis.Win32.Filesystem.File]::Create('test.txt ',$folderTree)

Solution

  • Your immediate problem is not one of a too long path.

    The problem is that you have a misconception about the arguments to pass to the Create() method:

    Note:


    It looks like the single-parameter (Create(string path)) overload is the right one, where you pass the full path of the file to create:

    [Alphaleonis.Win32.Filesystem.File]::Create((Join-Path $folderTree test.txt))
    

    However, it is then that you may run into the problem with an overly long path, which can be solved in one of two ways:

    See this answer for details.