0

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.

Poshox
  • 33
  • 7
  • have you tried using `$File.FullName` to get just the string value of the file name [instead of an object]? – Lee_Dailey Apr 07 '21 at 05:23
  • Add `-WhatIf` parameter to `Move-Item` to see what the command would do. Also, if your file names are very long, older versions of .Net (and thus PowerShell) had issues with lengths over 250. – vonPryz Apr 07 '21 at 05:28
  • @Lee_Dailey tried with that, no luck either – Poshox Apr 07 '21 at 05:33
  • @vonPryz it's throwing this message: Move-Item : Cannot move item because the item at 'E:\\AvidImages30\\123107371_06102019120310VDZA_fsresidentialapsouth_20190610_iNVOICE\ 'e\ -®\.pdf' does not exist. But clearly the file exist, I'm seeing it. – Poshox Apr 07 '21 at 05:40
  • `E:\\AvidImages30\\` contains double backslashes. Remove the regex escape and try again. – vonPryz Apr 07 '21 at 05:55
  • 1
    The `[regex]::Escape` makes no sense at all. Code should work without escaping using `Move-Item -LiteralPath`. You propably have an encoding issue. `ReadLines` with single parameter expects UTF-8 encoding, so make sure `rec_out.txt` is encoded as such. Better use `Get-Content` which tries to detect some encodings based on BOM. – zett42 Apr 07 '21 at 07:58
  • @Poshox - arg! you may be seeing a character encoding issue. you can check that by sending the file name to `Format-Hex` and checking the displayed chars. – Lee_Dailey Apr 07 '21 at 08:03
  • Does this answer your question? [Copy file with square brackets \[ \] in the filename and use \* wildcard](https://stackoverflow.com/questions/21008180/copy-file-with-square-brackets-in-the-filename-and-use-wildcard) – JosefZ Apr 07 '21 at 12:07
  • @zett42 changed to Get-Content and removed the regex escape, same case ): – Poshox Apr 07 '21 at 23:18
  • @JosefZ tried and didn't work either, but thanks ); – Poshox Apr 08 '21 at 04:03

1 Answers1

0

With your Example file names, I replaced just two lines of code, and the files moved. The file with the "[" requires the -LiteralPath statement. I did't research why your code failed, I just changed it to what I thought would work.

$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 {
         dir -LiteralPath  "$file" | Move-Item -Destination $recPath
    }
    catch {
        Write-Output "ERROR: Failed to copy $($file)"
    }
}