This brief intro is a neither complete nor exhaustive guide to the CommandLine module.
We’ll open a CMD session, issue a ‘ping’ command, and retrieve the results for later use. We’ll also remove some extraneous information from the results with a loop. We’ll use the results both as a Text variable and as a List variable.
Steps:
- Open a CMD session with ‘CommandLine.Open’.
- Issue a command (here, ‘ping’) with ‘CommandLine.Write’.
- Wait five seconds for the ping to complete.
- Read the output of the command into a TEXT variable with ‘CommandLine.Read’.
- Close the CMD session, as we’re finished with it, with ‘CommandLine.Close’.
- Create a list from our TEXT variable, with ‘Text.Split’.
- After modification, remove extraneous information with a loop and ‘Variables.RemoveItemFromList’.
- Display our results with ‘Display.ShowMessage’.
- Convert our new, modified list back into a TEXT variable with ‘Text.JoinWithDelimiter’.
- Write the new TEXT variable to console with ‘Console.Write’.
Screenshot of script before modification:
And the code:
# Start a command line session
CommandLine.Open Directory:'' \
Session=> Session \
# Issue a command
CommandLine.Write Session: Session \
Command: "ping www.autohotkey.com" \
SendEnter:True
# This command will take awhile, so we'll wait
wait 5
# Read the commandline output (STDOUT, in theory)
CommandLine.Read Session: Session \
CmdOutput=> CmdOutput \
# We're done with the session, wo we'll close it.
CommandLine.Close Session: Session
# Turn the output into a list
Text.Split Text: CmdOutput \
StandardDelimiter:Text.StandardDelimiter.NewLine \
DelimiterTimes:1 \
Result=> TextList
# Sometimes we get not only STDOUT back, but the command prompt itself,
# plus the command issued, plus the output. We may or may not want that.
# We'll experiment with removing the prompt and the command.
# First, leave the lines below commented to see the result we want
# to change.
# (This experiment is performed on Win7 Pro 64-bit. It has not been tested
# on Windows 10 yet.)
#loop i from 0 to 4
#Variables.RemoveItemFromList ItemIndex: 0 List: TextList NewList=> TextList
#end
Display.ShowMessage Title:'After manipulation, CMD output ' \
Message:TextList \
Icon:Icon.None \
Buttons:Buttons.OK \
DefaultButton:DefaultButton.Button1 \
IsTopMost:False \
ButtonPressed=> ButtonPressed
# List to text
Text.JoinWithDelimiter List: TextList \
StandardDelimiter:Text.StandardDelimiter.NewLine \
DelimiterTimes:1 \
Result=> CmdOutputText
Console.Write Message: CmdOutputText
Here’s the result before modifying our script (please ignore the caption , we haven’t modified the results yet):
Now we’ll uncomment our loop and get rid of the extraneous information.
In the loop, i.e.
loop i from 0 to 4
Variables.RemoveItemFromList ItemIndex: 0 List: TextList NewList=> TextList
end
we’re removing the first 5 lines of the output (remember List indexes are zero-based). Each time we remove the first item from the list (‘ItemIndex: 0’), we update the list itself by declaring ‘NewList=> TextList’, with ‘TextList’ thus being our list undergoing modification each time we remove an item.
(It wasn’t clear to me at first (although it probably should have been) that I needed to always remove ‘List[0]’ and then assign the newly modified list to the original List variable. For reference, think perhaps of ‘shift()’ in Perl (although the value isn’t returned in Robin, you can easily get it) or perhaps of ‘del list[0]’ in Python.)
Here’s a screenshot of our code with the loop added:
and the code:
# Start a command line session
CommandLine.Open Directory:'' \
Session=> Session \
# Issue a command
CommandLine.Write Session: Session \
Command: "ping www.autohotkey.com" \
SendEnter:True
# This command will take awhile, so we'll wait
wait 5
# Read the commandline output (STDOUT, in theory)
CommandLine.Read Session: Session \
CmdOutput=> CmdOutput \
# We're done with the session, wo we'll close it.
CommandLine.Close Session: Session
# Turn the output into a list
Text.Split Text: CmdOutput \
StandardDelimiter:Text.StandardDelimiter.NewLine \
DelimiterTimes:1 \
Result=> TextList
# Sometimes we get not only STDOUT back, but the command prompt itself,
# plus the command issued, plus the output. We may or may not want that.
# We'll experiment with removing the prompt and the command.
# No we've uncommented the loop below commented to see the result we want.
loop i from 0 to 4
Variables.RemoveItemFromList ItemIndex: 0 List: TextList NewList=> TextList
end
Display.ShowMessage Title:'After manipulation, CMD output ' \
Message:TextList \
Icon:Icon.None \
Buttons:Buttons.OK \
DefaultButton:DefaultButton.Button1 \
IsTopMost:False \
ButtonPressed=> ButtonPressed
Text.JoinWithDelimiter List: TextList \
StandardDelimiter:Text.StandardDelimiter.NewLine \
DelimiterTimes:1 \
Result=> CmdOutputText
Console.Write Message: CmdOutputText
Here’s the result after modifying the CMD output with our loop:
Best regards,
burque505