I'm giving you some context, I'm trying to move several files using Move-Item from a script that gets the names from a .txt file, it works pretty good for some of them but failing for some of them for example:
- 129318081_08122019142338VPUB_ M'NDINC A^V_^TRlTST COMI'ANY.pdf
- 129498612_08132019145413BTXI_Jul'19 Inv.07.19.943.1 - Test - 3200 - OSSE.pdf
- 129963323_081720191638262E2K_20190817_^SOUTHUIEST [ORPORnTlOH -w. Ill--III--1111--11-111-.pdf
- 130724789_08262019144137TSNF_19100715_ Cooperative Ý—e're putting our en.pdf
I tried using double quotes with interpolation ("$fileName") and even nested just in case "$($fileName)" and seems it's not working. Finally I tried [regex]::Escape("$fileName") thinking something could be escaping the string with no luck, I'm putting the current code below:
$recPath = "E:\Images_Rec\"
$logFile = "E:\rec_out.txt"
$filesToMove = [System.IO.File]::ReadLines($logFile)
if (!(Test-Path $recPath)) {
New-Item -ItemType Directory -Path $recPath
}
foreach ($file in $filesToMove) {
try {
$escapedName = [regex]::Escape("$file")
Move-Item -LiteralPath "$escapedName" -Destination "$($recPath)"
}
catch {
Write-Output "ERROR: Failed to copy $($file)"
}
}
Anyone has faced this kind of issue before? Would be awesome any advice so I can research about it.