1

I have following powershell to move files from server to local. I keep getting path not found. The path is long like 280 characters + the file name is about 30 to 70 characters. What to do?

$destDir = "C:\test\Files\"
$csv = import-csv 'C:\test\FilesWithPath.csv'

#For each line in the CSV
$csv | % {                                                           

            robocopy $_.SourceFilePath $destDir $_.SourceFileName 
        }
torres
  • 1,283
  • 8
  • 21
  • 30
  • You exceed the maximum path limit of 256 characters, is there any way you can move the single file or simplify the path? – Marcus May 24 '13 at 14:13
  • Well, Idea is to move these files (from long server path) to local so that I can use another script to move these file from local drive to SharePoint. – torres May 24 '13 at 14:15
  • Use robocopy: http://social.technet.microsoft.com/wiki/contents/articles/12179.net-powershell-path-too-long-exception-and-a-net-powershell-robocopy-clone.aspx – CB. May 24 '13 at 14:17
  • why did you edit your question to actually be an answer? Without the original question the Q&A format is lost. The answer is already below. Revert your question to the original non-working version. – x0n May 24 '13 at 20:42
  • [Find long file path](http://gallery.technet.microsoft.com/scriptcenter/Function-to-get-file-and-475aeb3a) but the same idea applies with moving data. – E.V.I.L. May 24 '13 at 20:44

3 Answers3

1

My suggestion is this: Use this to find all the files that have a path longer than 256 chars - How do I find files with a path length greater than 260 characters in Windows?

Save to CSV and write another script to CD to the location and copy/move using only the filename and target path.

Community
  • 1
  • 1
Marcus
  • 8,230
  • 11
  • 61
  • 88
1

Credit goes to C.B.

Here is the code that works with LONG file path with Robocopy.

$destDir = "C:\test\Files\"
$csv = import-csv 'C:\test\FilesWithPath.csv'

#For each line in the CSV
$csv | % {                                                           

            robocopy $_.SourceFilePath $destDir $_.SourceFileName 
        }
torres
  • 1,283
  • 8
  • 21
  • 30
0

One possible way to deal with this issue is to subst the parent directory to a drive letter and copy the file from there:

$csv | % {
  $i = $_.SourceFilePath.LastIndexOf('\')
  $d = $_.SourceFilePath.SubString(0, $i)
  $f = $_.SourceFilePath.SubString($i+1)

  subst T: $d
  Copy-Item "T:\$f" $destDir
  subst T: /d
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328