I am busy with a windows powershell script to batch rename a bunch of folders with IDs, which works perfectly fine. My question is how do i add validation to ensure all the IDs are unique when all my files get renamed. Here is the code:
param(
[Parameter(Mandatory=$true)]
[int]$idx
)
Get-ChildItem *.sql | Foreach-Object {
$iPrefix = ('{0:d5}' -f $idx)
$path = (Split-Path -Path($_))
$filename = (Split-Path -Path($_) -Leaf) -replace "\[|\]",""
#%{ write-host $path}
%{ write-host $filename}
if(!$filename.StartsWith("script","CurrentCultureIgnoreCase"))
{
#%{ write-host "Script$iPrefix - $filename"}
Rename-Item -LiteralPath(($_)) -NewName("Script$iPrefix - $filename")
++$idx
%{ write-host "Renamed: " + $filename}
}
}
Here is a screenshot of what i want to avoid:

As you can see Script02185 is repeated twice, because the script was ran at two different times. How do i ensure that the numbers will remain unique?