3

I am lost with simple rename-item. Need to change names of folders to "01", "02", "03"... Tried everything but at the end I get this "Item doesn't exist". Sorry for dumb question but I am looking for a solution all day.

PS C:\Users\admin> 
$nr = 1

Dir E:"Data-test" | %{Rename-Item $_ -NewName (‘{0}’ -f $nr++)}

Rename-Item : Cannot rename because item at 'ert' does not exist.
At line:3 char:23
+ Dir E:"Data-test" | %{Rename-Item $_ -NewName (‘{0}’ -f $nr++)}
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
 
Rename-Item : Cannot rename because item at 'ukh' does not exist.
At line:3 char:23
+ Dir E:"Data-test" | %{Rename-Item $_ -NewName (‘{0}’ -f $nr++)}
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
 
Rename-Item : Cannot rename because item at 'yph' does not exist.
At line:3 char:23
+ Dir E:"Data-test" | %{Rename-Item $_ -NewName (‘{0}’ -f $nr++)}
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
mklement0
  • 382,024
  • 64
  • 607
  • 775
Drone_Drone
  • 33
  • 1
  • 3

2 Answers2

3

user14915444's helpful answer has provided the crucial pointer:

The problem is that Windows PowerShell situationally stringifies Get-ChildItem (dir) output objects by file name rather than by full path, necessitating the use of $_.FullName rather than just $_; the problem has been fixed in PowerShell [Core] v6.1+ - see this answer for details.

However, in your case the problem can be avoided altogether, by piping the Get-ChildItem output directly to Rename-Item, using a delay-bind script block, which also speeds up the operation:

[ref] $nr = 1
Get-ChildItem E:Data-test | Rename-Item -NewName { '{0}' -f $nr.Value++ } -WhatIf

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

Note the use of a [ref] variable, which enables incrementing the sequence number across input objects from inside the delay-bind script block. This is necessary, because delay-bind script blocks run in a child scope of the caller's scope - unlike the script blocks passed to ForEach-Object (%) and Where-Object (?).
See GitHub issue #7157 for a discussion of this discrepancy.

mklement0
  • 382,024
  • 64
  • 607
  • 775
2

Your script works fine. (At least the first variation.) You can't run it but once because you rename the file it is working with (i.e. "Data-Test"). I just created a Data-Test file on my E: drive and ran your script without issue once. Then, on second attempt, it couldn't work because it worked the first time.

If you are working with the contents of a directory named "Data-Test," you need to append your $_ with .FullName. (EX: Rename-Item $_.FullName -NewName ...)

See image:
See image

If it still doesn't work, ensure that you have a file/folder named "Data-Test" in the CWD of your E: drive. Placing a backslash in the location will guarantee you are working in the root. (EX: E:\Data-Test)

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Dr. C.
  • 76
  • 6
  • 2
    @user14915444 Indicating the signature is unnecessary since it is only noise, and the web / personal blogs are considered spam (you could put it in your profile but not in the answers) – eyllanesc Dec 30 '20 at 20:59
  • 1
    +1 for the `$_.FullName` tip, but note that the command in the question (there is only one) does _not_ work fine, because it indeed triggers file-_name_-only stringification. The problem of situational name-only stringification afflicts _Windows PowerShell_, but has been fixed in _PowerShell [Core] v6.1+_ - see [this answer](https://stackoverflow.com/a/53400031/45375) for details. – mklement0 Dec 30 '20 at 21:03