如何在每个循环中增加字符串中的数字



我想在"$wshell。发送密钥(‘M1 ABCD’(";每个循环都基于用户输入。第一个循环将键入"0";M1 ABCD";,第二个";M2 ABCD";,第三个";M3 ABCD";直到脚本从用户输入达到所请求的循环数。

以下是脚本的一小部分(不包括预请求(

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++
}

试试这个:

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++
}

请注意,我已经用1而不是0开始了循环,并进行了一些相应的调整。双引号中的$i将在每次循环中进行评估。

我想出了一个变通办法,但不是最有效的。

我将send.keys分解为多个部分,并添加了一个独立于字符串的增量。

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++
}

最新更新