Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell Rename-Item fail to rename

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?

like image 255
Wolfy Avatar asked Apr 24 '14 12:04

Wolfy


People also ask

Why I Cannot rename my files?

When a file or folder is still open, Windows doesn't allow users to rename it. Therefore, you must ensure that no files or folders are open and no apps are running in the background while you're renaming. To do that, simply click on the same file again to open it, and it will take you to the already opened tab.

How do I rename an item in PowerShell?

To rename and move an item, use Move-Item . You can't use wildcard characters in the value of the NewName parameter. To specify a name for multiple files, use the Replace operator in a regular expression.

How do I rename a file in PowerShell script?

Rename-Item This can be done with a file, directory, or registry key. Important parameters are -path and -newname. The -path parameter specifies the location of the object you are looking to rename. Use -newname to specify the new name of the object.

How do I rename a directory in PowerShell?

Rename-Item cmdlet is used to rename a folder by passing the path of the folder to be renamed and target name.


1 Answers

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.

like image 119
Raf Avatar answered Jan 01 '23 11:01

Raf