1

I'm trying to log some files that are unable to transfer when I'm recursively copying over one folder to a different location. When I right click, copy, and paste the folder, I get an error message "The file name you specified is not valid or too long". on some of the files.

Then I tried to use PowerShell to log the file names that are unable to transfer, but I get the message "Copy-Item : The filename, directory name, or volume label syntax is incorrect."

This is the PowerShell script that I am using:

$skippedFiles = @()

$items = Get-ChildItem -Path $sourcePath -Recurse

foreach ($item in $items) {
     $destinationItemPath = Join-Path $destinationPath $item.FullName.Substring($sourcePath.Length + 1)
     if (Test-Path $destinationItemPath) {
          $skippedFiles += "Destination file already exists: $($item.FullName)"
          } elseif ($item.Name.Length -gt 255) {
               $skippedFiles += "File name too long: $($item.FullName)"
          } else {
               Copy-Item -Path $item.FullName -Destination $destinationItemPath -ErrorAction SilentlyContinue
          }
}

$skippedFiles | Out-File -FilePath $logFilePath

1 Answers1

0

This does not exactly answer your question, but shows ways to work around long filenames.

  • If copying to a file system that supports long filenames, such as NTFS, enable long filenames/paths: In Regedit, navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem] and create or edit Value DWORD LongPathsEnabled, setting it to 1.
  • If the file system to which files are being copied does not support long filenames, such as FAT, copy using the 8.3 format.. E.G., for file ALongFilename.txt, use xcopy /N *.txt .\destfldr.

See also this answer on long file path. In particular, Robocopy, part of Windows OS, will log any errors.