powershellcmdincrementrevisionalphabet

PowerShell or CMD - Find and Increment alphabet letter of files (revision management)


I need some help to automate rename files in a current directory. The following files that can exits in the directory is for example:

AAA111A.txt

AAA111A.pdf

AAA111A.jpg

BBB222B.jpg

BBB222B.pdf

Where the bold letter stand for the revision of the file. What I want is a PowerShell or batch file where it automatically looks what revision letter it is and then increment that revision letter with the next in the alphabet for all files.

Example:

AAA111A.txt -> AAA111B.txt

BBB222B.pdf -> BBB222C.pdf

etc

The letters and numbers before the revision letter and the extension of the file can vary, so used as a wildcard? It is also possible that the file is named like: AAA111A-01.pdf or AAA111A-blabla.pdf

Hopefully someone can make my life easier for this noobie :).

Thank you in advance!


Solution

  • Building off of Abraham's answer, because the use of a scriptblock with parameter to evaluate the replacement is great and not something I knew about before today. I'd just setup a hashtable ahead of time for the replacement letter(s). I'd replace z with aa to avoid conflicts, assuming that doesn't break the naming convention.

    $Letters=[hashtable]::new() #unconventional hashtable creation to create a hashtable with case sensitive keys
    65..122|?{$_ -lt 90 -or $_ -gt 96}| #this gets up A through Z and a through z, skipping the other characters between the sets
        ForEach-Object{$Letters.add([string][char]$_, [string][char]($_+1))}
    $Letters.'Z'='AA' #manually set rollover case
    $Letters.'z'='aa' #manually set rollover case
    

    Once you get that setup you can basically do what Abraham suggested, just referencing the hashtable to get the replacement string.

    Get-ChildItem C:\Path\To\Files*.txt|
        Rename-item -NewName {
            [regex]::replace($_.Name, '(?<=^[a-zA-Z0-9]+)([a-zA-Z])(?=\W)',{param($old)$Letters[$old.value]})
            }
    

    This has the same failing that Abraham's answer does in that it does not check for file name availability before trying to rename a file. It does solve the case where a file's revision is at Z though. My regex is also a little more flexible for cases where a file is named something like 'AAAA111C-glarb.txt' and will correctly increment it to 'AAAA111D-glarb.txt'.