Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behavior with brackets in path

There is a strange behavior with PowerShell when there are square brackets in a path. For instance, if you are in the folder:

C:\Some Movie [2011]

which contains an mkv file and you type:

ls *.mkv

nothing is returned! I think the problem lies with the fact that PowerShell tries to do something like:

Get-ChildItem 'C:\Some Movie [2011]\*.mkv'

which fails because [2011] is considered a wildcard. I was able to retrive all mkv from such a folder with the following command:

Get-ChildItem -LiteralPath 'C:\Some Movie [2011]' -Include *.mkv

but when I try to feed those results in a Rename-Item command it fails.

Get-ChildItem -LiteralPath 'C:\Some Movie [2011]' -Include *.mkv | Rename-Item -NewName "movie.mkv"

The same operations in a folder without brackets runs without problems. Any ideas?

like image 830
lalibi Avatar asked Dec 26 '11 14:12

lalibi


1 Answers

See my comment on your question (above). this will work if as you expect in case there's only one mkv file in that folder. Rename-Item doesn't support LiterlPath (fixed in v3), you can resort to .NET. I also recommend (when you filter for just one extension) to use -Filter instead of Include, it performs faster.

Get-ChildItem -LiteralPath 'D:\Some Movie [2011]' -Filter *.mkv | Foreach-Object{
    $NewName = Join-Path -Path $_.DirectoryName -ChildPath 'movie.mkv'
    [System.IO.File]::Move($_.FullName,$NewName)
}
like image 194
Shay Levy Avatar answered Nov 16 '22 03:11

Shay Levy