11

My powershell script:

$dst = 'C:\Temp'

#Get all folders in $dst
$folders = Get-ChildItem $dst | ?{ $_.PSIsContainer }

foreach($folder in $folders)
{
    $cnt = (Get-ChildItem -filter *.txt $folder | Measure-Object).Count

    $base = ($folder.FullName -split " \[.*\]$")[0]
    $newname = $("{0} [{1}]" -f $base,$cnt)

    Write-Host $folder.FullName "->" $newname

    Rename-Item $folder.FullName $newname
}

The problem

On my first run I get this:

PS C:\Temp> C:\Temp\RenameFolders.ps1
C:\Temp\m1 -> C:\Temp\m1 [1]

On my second run I get this:

PS C:\Temp> C:\Temp\RenameFolders.ps1
C:\Temp\m1 [1] -> C:\Temp\m1 [0]
Rename-Item : Cannot rename because item at 'C:\Temp\m1 [1]' does not exist.
At C:\Temp\RenameFolders.ps1:15 char:5
+     Rename-Item $folder.FullName $newname
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand

I know that the problem is in '[' and ']', but I realy can't understand why.

Can someone explain me why is that a problem?

2
  • What does your folder structure look like before/after? What are you expecting to occur exactly?
    – user189198
    Apr 24, 2014 at 12:14
  • as you can see in my example... start name: "C:\Temp\m1", after rename: "C:\Temp\m1 [1]", after second run... start name: "C:\Temp\m1 [1]", rename to "C:\Temp\m1 [0]" <- this step fails
    – Wolfy
    Apr 24, 2014 at 12:23

1 Answer 1

31

If you are running PS 3+ add -LiteralPath switch to your rename:

Rename-Item -LiteralPath $folder.FullName $newname

otherwise use Move-Item

Move-Item -LiteralPath $folder.FullName $newname

Powershell doesn't like square brackets in filenames, more in the following post:

This became an issue with V2 when they added the square brackets to the wildcard character set to support "blobbing".

From get-help about_wildcards:

Windows PowerShell supports the following wildcard characters.

Wildcard Description Example Match No match


  • Matches zero or a* A, ag, Apple banana more characters

    ? Matches exactly ?n an, in, on ran one character in the specified position

    [ ] Matches a range [a-l]ook book, cook, look took of characters

    [ ] Matches specified [bc]ook book, cook hook characters

[ and ] are special characters.

3
  • Thank you so much! -LiteralPath worked perfectly for me. Why? Jan 8, 2016 at 6:00
  • See above article: ...This became an issue with V2 when they added the square brackets to the wildcard character set to support "blobbing"
    – Raf
    Jan 11, 2016 at 17:48
  • Thank you so so much! - Square brackets in filenames had me completely messed up! -LiteralPath fixed it for me! Mar 29, 2019 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.