Search code examples
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)

thanks,


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:

    • You're argument types do not precisely match any of the Create() overloads, and the one that PowerShell's method-overload resolution algorithm picks is Create(string path, int bufferSize), which predictably fails.

    • Specifically, PowerShell tries to coerce your $folderTree - a [string] - to the parameter type, [int], and the resulting error message is the same as if you tried [int]::Parse('foo')

    Note:

    • Generally, it's best to match the parameter types of .NET methods you're calling exactly, as this avoids ambiguity related to automatic type conversions and therefore as to which particular overload is selected and, perhaps more importantly, doing so is future-proof, as it eliminates the possibility of PowerShell in the future selecting a different overload, which can happen if and when new overloads are added to the API being called.

    • To see all overloads of a given method, simply invoke it without parentheses:

      # Prints all overloads, due not using (...)
      [Alphaleonis.Win32.Filesystem.File]::Create
      

    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:

    • Either: Prepend the long-path opt-in prefix, \\?\ to the file path:

      [Alphaleonis.Win32.Filesystem.File]::Create((Join-Path "\\?\$folderTree" test.txt))
      
    • Or: Enable long-path support system-wide.

    See this answer for details.