问题
I am new to VBScript and AutoIt. I have been trying to make changes to a VBScript (text file) using an AutoIt script by taking input from the user in form of comma-seperated commands and then writing it to a .vbs
file. I tried to do this by storing the strings in array and then writing it using a While
loop.
Example:
For an input of: ALPHA,BETA,GAMMA
I am expecting an output as follows starting from line "140" in the text file
WshShell.appactivate "telnet 10.250.124.85"
WScript.Sleep 1999
WshShell.SendKeys"ALPHA"
WshShell.appactivate "telnet 10.250.124.85"
WScript.Sleep 1999
WshShell.SendKeys"BETA"
WshShell.appactivate "telnet 10.250.124.85"
WScript.Sleep 1999
WshShell.SendKeys"GAMMA"
Instead, i am getting such output:
WshShell.appactivate "telnet 10.250.124.85"
WScript.Sleep 1999
WshShell.SendKeys""
WshShell.appactivate "telnet 10.250.124.85"
WScript.Sleep 1999
WshShell.SendKeys""
WshShell.appactivate "telnet 10.250.124.85"
WScript.Sleep 1999
WshShell.SendKeys""
I.e. the SendKeys quotes are empty due to some mistake. The code I have used is as follows:
$fileadd="C:\Users\rmehta\Downloads\zyxw\Memorycheck.vbs"
$commandnewstring= InputBox("Command Settings","Please enter the commands seperated by commas","")
If @error=0 Then
Global $count,$usestring,$output
$usestring=$commandnewstring
$count= UBound(StringSplit($commandnewstring, ",",""))-1
Global $a[$count],$line
$line=140
$count= $count-1
$output= StringSplit($usestring,",","")
While $count >= 0
$a[$count]= $output
_FileWriteToLine($fileadd,$line,"WshShell.appactivate" & Chr( 34 ) & "telnet 10.250.124.85" & Chr( 34 ),1)
_FileWriteToLine($fileadd,$line+1,"WScript.Sleep" & " 1999",1)
_FileWriteToLine($fileadd,$line+2,"WshShell.SendKeys" & Chr( 34 ) & $a[$count] & Chr( 34 ),1)
$count=$count-1
$line=$line+3
WEnd
Else
MsgBox(0,"You clicked on Cancel",2)
EndIf
I have put in a lot of thought but couldn't get the answer.
回答1:
Ok, here's a working solution:
#include <File.au3>
$fileadd = "C:\Users\rmehta\Downloads\zyxw\Memorycheck.vbs"
$commands = InputBox("Command Settings", "Please enter the commands seperated by commas","")
If @error == 0 Then
Global $count
$commands = StringSplit($commands, ",", 2)
$count = UBound($commands)
Global $a[$count], $line
$line = 140
$index = 0
While $index < $count
$command = $commands[$index]
_FileWriteToLine($fileadd, $line, "WshShell.appactivate" & Chr(34) & "telnet 10.250.124.85" & Chr(34), 1)
_FileWriteToLine($fileadd, $line + 1, "WScript.Sleep 1999", 1)
_FileWriteToLine($fileadd, $line + 2, "WshShell.SendKeys" & Chr(34) & $command & Chr(34), 1)
$line += 3
$index += 1
WEnd
Else
MsgBox(0, "You clicked on Cancel", 2)
EndIf
I encourage you to study the differences between your first try and this solution. Learn from the formatting and the usage of spacing as well as from the comparison with ==
, not with =
. Or the usage of the +=
operator...
来源:https://stackoverflow.com/questions/18722318/error-getting-the-required-output-in-sendkeys-using-arrays