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