for a personal project I want to prefix folder and file names with an index. The goal is to have every childitem name remain the same except for the prefix.
My current Script looks as follows:
$vaultDir = $args[0]
$path = Get-ChildItem -Path $vaultDir -Depth 0
ForEach($childObject in $path){
$index = '{0:d3}' -f $path.IndexOf($childObject)
$currentFolderName = $childObject.ToString() | out-string | select-string -pattern '[^\\]+$'
$newName=($index.ToString()+'-'+$currentFolderName.Matches.value)
$childObject.FullName |rename-Item -newname {$_.Name -replace '.*', $newName} -whatif
}
The $newName is set correctly. Now I want to iterate through the childitems and rename them.
But honestly I do not get how the rename-item function works.
The -whatif result seems to work(the test folders are named asdfg and tyui):
What if: Performing the operation "Rename Directory" on target "Item: C:\Users\mon0\Documents\Script\kekw\asdfg Destination: C:\Users\mon0\Documents\Script\kekw\000-asdfg
".
What if: Performing the operation "Rename Directory" on target "Item: C:\Users\mon0\Documents\Script\kekw\tyui Destination: C:\Users\mon0\Documents\Script\kekw\001-tyui
".
but when I remove it I get the following error message:
Line |
19 | … ject.FullName |rename-Item -newname {$_.Name -replace '.*', $newName}
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The filename, directory name, or volume label syntax is incorrect.
Rename-Item: C:\Users\mon0\Documents\Script\Batch-Rename.ps1:19:28
Line |
19 | … ject.FullName |rename-Item -newname {$_.Name -replace '.*', $newName}
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The filename, directory name, or volume label syntax is incorrect.
Any ideas ?