Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Getting Output of a Command Prompt in the Shortest Way


  • Please log in to reply
3 replies to this topic
frukoprof
  • Members
  • 2 posts
  • Last active: Oct 27 2015 08:51 AM
  • Joined: 18 Oct 2015

I'm kinda new to this language but I have a modest programming background. You know the title. I've been searching new ideas and methods for these ideas during a few days. I decided the shortest way must be just "copy it to the clipboard", in other ways it goes too hairy (to me at least :)). Maybe it's not suitable for big projects but I think, newbies like me will love it :p. If you have any improvements for that please comment. Hope, it'd be useful for you.

Run, cmd.exe /k echo off & cls & dir echo Hello World & title OKE
WinWait, OKE
ClipBoard = 
Send !{Space}es{Enter}
MsgBox %ClipBoard%

So all the thing goes in the first line. If you wonder what's going there you can continue to read.

1st Line:
1- We run cmd.exe or you can use %comspec% for that.

Note: You can't use RunWait because AHK will wait until you close the prompt.

2- /k parameter simply means execute the code and don't terminate.

3- echo off simply means don't show my working directory. It's important because we don't want anything useless.

4- By putting an & between them, you can execute multiple commands in one line.
5- It clears the screen.
7,8- Just any command. It can be echo Hello World! or devcom restart *  ;) 
10,11- When it finished, in some way we need to let AHK know our process finished. I did it changing title, you can do it in another way. 
2nd Line: Wait for a window named OKE. 
3rd Line: Clear the clipboard.
4th Line: Send the key combination ALT+SPACE+E+S and press enter, which selects all and copies it.
5th Line: Show the clipboard in a message box.



Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
How about ...
MsgBox % ComObjCreate("WScript.Shell").Exec("cmd.exe /q /c dir").StdOut.ReadAll()
?

gilliduck
  • Members
  • 109 posts
  • Last active: Nov 09 2015 01:07 AM
  • Joined: 19 Dec 2013

If you want the shortest way to send it to the clipboard, pipe it to clip 

ipconfig | clip

Lexikos is more stable than just putting to the clipboard though (since that can easily be overwritten by accident)



frukoprof
  • Members
  • 2 posts
  • Last active: Oct 27 2015 08:51 AM
  • Joined: 18 Oct 2015

How about ...

MsgBox % ComObjCreate("WScript.Shell").Exec("cmd.exe /q /c dir").StdOut.ReadAll()
?

 

 

Oh why I've never seen this? This is absolutely shorter and it doesn't require a window. What is a COM object? Does Windows creates that object in temp memory or saves something on hardware? 

 

If you want the shortest way to send it to the clipboard, pipe it to clip 

ipconfig | clip

Lexikos is more stable than just putting to the clipboard though (since that can easily be overwritten by accident)

Seen this, doesn't work on XP. If it does, it'd be shortest.