0

Windows 10 64 BIT

Scenario: copy files in a directory and subdirectory to the destination directory. File Type: Only pdf Issue: When the file name has special characters not able to copy

Tried below code not working

#Get all files and not the directories
$files = Get-ChildItem -Path "c:/source" -Recurse -filter "*.pdf" | Where {$_.PSIsContainer -eq $false}

#Copy items from sources to new destination
foreach ($file in $files)
{
if ($file.Name -match '[^a-zA-Z0-9]')
{
$file.FullName
*$file.FullName | Rename-Item -NewName {$_ -replace '_*(\[.*?\]|\(.*?\))_*' -replace '_+', ' '} $NewName
*Rename-Item -NewName {$_ -replace '_*(\[.*?\]|\(.*?\))_*' -replace '_+', ' '} 
*$NewName = rename-item $file.FullName.Replace('_*(\[*?\]|\(*?\))_*', '')
Rename-Item -Path $file.fullName -NewName {$_ -replace '_*(\[.*?\]|\(.*?\))_*' -replace '_+', ' '}
}
    Copy-Item -Path $file.FullName -Destination "c:/desination\"
}
Nash
  • 99
  • 2
  • 9
  • 5
    the `-LiteralPath` parameter is designed for just exactly that use. [*grin*] – Lee_Dailey Mar 29 '20 at 19:36
  • 3
    As a general tip (@Lee_Dailey's hint may be all you need): "not working" is typically not specific enough to diagnose a problem; please show the specific error you're getting in your questions. – mklement0 Mar 29 '20 at 21:23
  • This is the Error Copy-Item : The operation completed successfully. At line:2 char:3 + %{Copy-Item -Path $_.FullName -Destination "c:\... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Copy-Item], IOException + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand – Nash Mar 30 '20 at 00:42
  • 1
    @Avi, please add additional information _directly to the question_ - don't use comments. Also, please add a sample input path that produces the symptom. – mklement0 Mar 30 '20 at 11:36

0 Answers0