Powershell : File Copy : If File name contains special characters then not able to copy

611 Views Asked by At

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\"
}
0

There are 0 best solutions below