The specified path, file name, or both are too long.

I try to copy three kind of files to a remote location. I've tested it on my own computer and on a server and everything works fine BUT on the computer I actually should execute this script.

I already checked the ExecutionPolicy and it's Ok. The write-host gives mean empty screen. Do get it where the problem lies. Any idea guys?

Here is the error message:

PS C:\Windows\system32> C:\NG_auto_project\Scripts\Get_TXTv3.ps1
C:\NG_auto_project\nextgene_to_import\PA9046_NA_CPKRAS_SN1-193_20131023132639\PA9046_NA_CPKRAS_SN1-193_20131023132639_StatInfo.txt C:\NG_auto_project\nextgene_to_import\AC7449_14A00270-2_ODCL_PRO-98_20140314204739_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\META11A-M2_13A00427-1_OD
DX_SN1-274_20140211193345_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9083_13A01435_ODDX_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9
084_13A01436_ODDX_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9085_13A01441_ODDX_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9154_13A0
1494-1_ODCL_PRO-81_20131128193235_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9165_13A01507-1_ODCL_PRO-81_20131128194449_Mutation_Report.txt C:\NG_aut
o_project\nextgene_to_import\PA9262_13A01673-1_ODCL_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9263_13A01674-1_ODCL_Mutation_Report.txt C:\NG_auto_pr
oject\nextgene_to_import\PA9264_13A01675-1_ODCL_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9266_13A01677-1_ODCL_Mutation_Report.txt C:\NG_auto_projec
t\nextgene_to_import\PA9267_13A01678-1_ODCL_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9268_13A01679-1_ODCL_Mutation_Report.txt C:\NG_auto_project\ne
xtgene_to_import\PA9270_13A01681-1_ODCL_PRO-85_20140108200354_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9362_13A01736-1_ODDX_SN1-241_20140103211849_
Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9363_13A01737-1_ODDX_SN1-241_20140103211901_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA94
27_14A00008-1_ODCL_PRO-89_20140122214642_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9523_14A00105-1_ODDX_SN1-269_20140205150832_Mutation_Report.txt C
:\NG_auto_project\nextgene_to_import\PA9542_14A00184-1_ODDX_SN1-274_20140211193353_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9560_14A00221-1_ODDX_SN
1-276_20140214193028_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9607_14A00271-1_ODCL_PRO-98_20140314204203_Mutation_Report.txt C:\NG_auto_project\nex
tgene_to_import\PA9637_14A00418-1_ODDX_SN1-288_20140304205119_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9638_14A00419-1_ODDX_SN1-288_20140304205140_
Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\PA9670_14A00423-1_ODCL_PRO-98_20140314203402_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\SN214
_BC43_Mutation_Report.txt C:\NG_auto_project\nextgene_to_import\test_Mutation_Report9.txt

Exception calling "Put" with "2" argument(s): "The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters,
and the directory name must be less than 248 characters."
At C:\NG_auto_project\Scripts\Get_TXTv3.ps1:37 char:1
+ $sftp.Put($file, "/files/txt")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : PathTooLongException

The actual script is as following:

#Add the SFTP snap-in
Add-PSSnapin -Name Ktools.PowerShell.SFTP

$FilePatha = "C:\NG_auto_project\nextgene_to_import\*\*"
$FileNameStat = "*_StatInfo.txt"
$Outputa = Get-ChildItem -Path $FilePatha -Filter $FileNameStat | select -ExpandProperty FullName

$FilePathb = "C:\NG_auto_project\nextgene_to_import\"
$FileNameMut1 = "*_Mutation_Report.txt"
$Outputb = Get-ChildItem -Path $FilePathb -Filter $FileNameMut1 | select -ExpandProperty FullName

$Main = "*_Mutation_Report"
$Num = ""
$Ext = ".txt"
for ($Num=0; $Num -lt 50; $Num++)
    {$c=$Main + $Num + $Ext
    $d = Get-ChildItem -Path $FilePathb -Filter $c | select -ExpandProperty FullName
    $Outputd = $d

#Define SFTP remote Variables
$sftpHost = ""
$userName = "xxxxxx"
$userPassword = "xxxxxx"
$output = "$Outputa","$Outputb","$Outputd"
Write-Host $output | Format-Table Name, Length

#Open the SFTP connection
$sftp = Open-SFTPServer -serverAddress $sftpHost -userName $userName -userPassword $userPassword

#Upload a list of files to another folder on the SFTP server
foreach($file in $output)
$sftp.Put($file, "/files/txt")

#Close the SFTP connection

  • Yep, that's a problem.  And when it was bugged by the community on, MSFT came back with a "that's by design" reply.

    You might want to take a look at

  • I can understand there is a limit by file which is 248 in Windows. The fact is that if I take the longest path in the list, I get a path with 122 characters.

    I do not know if I have the scripts interpret things as I want. The scripts is supposed to get a list of files, specific files, and copy them to a remote location.

    I made a search with longpathtool (thanks by the way for this link) and it did not find any filepath longer than 248 characters.

  • Couple of issues.  You are mixing up FileInfo and String objects.  PowerShell moves objects through a pipeline -- not redirected output, so Write-Host $x | Format-Table just does not "compute". 

    In line 37, $file in $sftp.Put($file,"/files/txt") is a collection of FileInfo objects, as coded produce a space separated (very long) list of files.

    It looks like you are mixing up VB with PS.  Note that "select -ExpandProperty FullName" does not change the FileInfo object into a string object, it metamorphasized (spelling?) it.  And I am guessing $sftp Put method is looking for string or strings.

    Instead of

       $Outputa = Get-ChildItem -Path $FilePatha -Filter $FileNameStat | select -ExpandProperty FullName

    I suggest try:

       $Outputa = (Get-ChildItem -Path $FilePatha -Filter $FileNameStat).FullName


    Also, in your for loop, a PowerShell way would be more like:

      $Outputd = 0..49 | ForEach{ (Get-ChildItem -Path $FilePathb -Filter "*_Mutation_Report$($_).txt").FullName }


  • As pointed out by rshen the problem is in code.

    Stated by the error, it is showing you that all your paths are converging in to one giant string. I haven't gone through your code very closely to find out why, but that is your problem.

    I will point out that in your for loop, you are constantly overwriting the outputd variable so I am not sure what you are trying to accomplish there.

  • If File Path is too long then I would like to suggest you, try Long Path Tool program to resolve this issue.