Increment Alphanumeric

I have a stupid and likely simple question that has perplexed me.

 

I need to be able to increment an alphanumeric string by 1 for a script.

I basically need to be able to set a variable to equal “T01” + 1

Any assistance would be greatly appreciated.

 

Hii,

This is the first thing which came up in my mind. Works for you?? You can make the changes as per your requirement.

[pre]

$i = 01
$i++
$str = “T$i”
Write-Host $str

[/pre]

I recommend the padleft method of the string class

1..10 | foreach { "T$(([String]($_)).PadLeft(2,'0'))" }

My difficulty and I should have mentioned this is that the “T01” is being provided to the script as a parm in the script execution command line so it is a var in the script. So I was trying to accomplish this with a little rewriting of the scripts as possible.

So you might have explained a little more detailed what you’re trying to do. Ideally with some examples of the data before and after. If I got it right you could use something like this:

$Result = ‘T01’ -match ‘(\w)(\d+)’
If ($Result){
“{0}{1:0#}” -f $Matches[1], $([INT]$Matches[2] + 1 )
}

Yes I should have added more details :slight_smile:

Your solution was perfect. Thanks a bunch!