silentc
silentc

Reputation: 1

How to increment a number in a string every loop?

I'd like to increment the number after M by 1 in "$wshell.SendKeys('M1 ABCD')" every loop based on user input. The first loop will type "M1 ABCD", 2nd "M2 ABCD", 3rd "M3 ABCD" until the script reaches the requested number of loop from user input.

Here is a small part of the script (Pre-reqs not included)

Param(
    [Parameter(
        Mandatory=$true, 
        Position=0, 
        HelpMessage="how many times do you want to run?")
    ]
    $Number
)
$i = 0
while($i -lt $number){                    #Number of times the loop repeats
    Write-host "Loop run #$($i + 1)"
    [Clicker]::LeftClickAtPoint(744,277)
    $wshell.SendKeys('{DOWN}')
    $wshell.SendKeys('{ENTER}')
    $wshell.SendKeys('{TAB}')
    Sleep -Milliseconds 600
    $wshell.SendKeys('M1 ABCD') 
    $wshell.SendKeys('{DOWN}')
    $wshell.SendKeys('{ENTER}')                                                      
    $i++
}

Upvotes: 0

Views: 330

Answers (2)

Walter Mitty
Walter Mitty

Reputation: 18940

Try this:

Param(
    [Parameter(
        Mandatory=$true, 
        Position=0, 
        HelpMessage="how many times do you want to run?")
    ]
    $Number
)
$i = 1
while($i -le $number){                    #Number of times the loop repeats
    Write-host "Loop run #$($i)"
    [Clicker]::LeftClickAtPoint(744,277)
    $wshell.SendKeys('{DOWN}')
    $wshell.SendKeys('{ENTER}')
    $wshell.SendKeys('{TAB}')
    Sleep -Milliseconds 600
    $wshell.SendKeys("M$i ABCD") 
    $wshell.SendKeys('{DOWN}')
    $wshell.SendKeys('{ENTER}')                                                      
    $i++
}

Note that I've started the loop with 1 rather than 0, and made a few corresponding adjustments. The $i inside the double quotes will be evaluated each time through to loop.

Upvotes: 1

silentc
silentc

Reputation: 1

I came up with a work around, but isn't the most efficient.

I broke down send.keys into multiple sections and added an increment separate from the string.

Param(
    [Parameter(
        Mandatory=$true, 
        Position=0, 
        HelpMessage="how many times do you want to run?")
    ]
    $Number
)
$a = 1
$i = 0
while($i -lt $number){                    #Number of times the loop repeats
    Write-host "Loop run #$($i + 1)"
    [Clicker]::LeftClickAtPoint(744,277)
    $wshell.SendKeys('{DOWN}')
    $wshell.SendKeys('{ENTER}')
    $wshell.SendKeys('{TAB}')
    Sleep -Milliseconds 600
    $wshell.SendKeys('M1')
    $wshell.SendKeys($a)
    $wshell.SendKeys('ABCD') 
    $wshell.SendKeys('{DOWN}')
    $wshell.SendKeys('{ENTER}')                                                      
    $i++
    $a++
}

Upvotes: 0

Related Questions