1

I want to move files to a sub folder when I am done with them.

The below code moves a test file called test.dav to /archive just fine. The files I get are however weirdly named, and I can't change that. They are generated by a system I do not control.

Examples of those file names being:

  • 23.51.54-23.52.19[M][0@0][0].dav
  • 23.52.32-23.52.55[M][0@0][0].dav

I am using the following script to do some processing on them and then to move.

Get-ChildItem "C:\NVR\CameraDrop\dump" -Filter *.dav | ForEach-Object {

    #Doing processing here#

    try { 
        Move-Item -Path "$($_.FullName)" -Destination "C:\NVR\CameraDrop\dump\archive\$($_.Name)" -force
        "Successfully moved" }
    catch { 
        "Error moving $_.FullName" 
    }
}

The problem is the above code and specifically Move-Item fails to move real files but does work on normally named files. It still reports success though ;)

Powershell is not my forte, but I thought it might be because I need to escape the paths since they have all those nasty characters in them but that did not help either.

Help please :)

JensB
  • 6,663
  • 2
  • 55
  • 94
  • 5
    You may have better luck with `Move-Item -LiteralPath` as this will ignore special characters such as brackets. – jfrmilner Jul 28 '22 at 22:23
  • 3
    You can also pipe it to Move-Item – Doug Maurer Jul 28 '22 at 22:57
  • The `-Path` parameter of provider (file-handling) cmdlets (which the first _positional_ argument implicitly binds to), expects _wildcard_ expressions. Because PowerShell's wildcard language also supports expressions such as `[a-z]`, paths meant to be _literal_ ones are misinterpreted if they happen to contain `[` chars. To pass a literal path as such, use the `-LiteralPath` parameter explicitly. Alternatively, pass a file-information object such as produced by `Get-ChildItem` _via the pipeline_ to a provider cmdlet, which also binds to `-LiteralPath`. – mklement0 Jul 29 '22 at 01:13

1 Answers1

2

Move-Item with -Path parameter consider for example the filename 23.51.54-23.52.19[M][0@0][0].dav anything between left bracket and right bracket characters ([ and ]) is interpreted as matching a single character, so a substring [0@0] is '0' or '@' instead of the literal string [0@0], So to solve that use -LiteralPath parameter instead of -Path

XMehdi01
  • 5,538
  • 2
  • 10
  • 34