Jump to content

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

Retrieving the output of a command line operation


  • Please log in to reply
17 replies to this topic
Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
I've added this to the FAQ since it seems to be a common request:

How can the output of a command line operation be retrieved?
A small freeware utility cb.zip captures up to 512 KB of output from a command or program. The text is captured to the clipboard, which a script can access via the clipboard variable. Example:
RunWait %comspec% /c dir | cb.exe
MsgBox %clipboard%

http://www.autohotkey.com/misc/cb.zip

Beastmaster
  • Members
  • 191 posts
  • Last active: Apr 23 2007 02:58 PM
  • Joined: 15 Apr 2004
That is very usefull !

I've already parsed files via the clipboard using this tool (as it was recommended by Chris some weeks ago). So there was no need to create an input file, to parse that file, to write an output file ... 8)

GetClipboard>VarName (an MSched command) is covered on AHK by the %CLIPBOARD% variable (the workaround, VarName = %CLIPBOARD%)

PutClipboard>VarName (an MSched command) has no exact equivalent in AHK (the workaround, CLIPBOARD = %VarName%)

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
I tried to use the following script to retrieve the ouput of CPU usage of a remote server via clipboard. But it doesn't work. The exampel that came with cb.exe runs well.

RunWait %comspec% /c dir | cb.exe              ;this works
  MsgBox %clipboard% 

  EXE = T4eUnixinfo.exe
  LOGIN = user
  PASSWD = password
  Host = hostname
  RunWait, %EXE% -l %LOGIN% -p %PASSWD% %HOST% CPU | cb.exe  ; this doesn't work
  MsgBox %clipboard% 


To retrieve the CPU usage via a file works well too. But I would like to avoid to write temporary files.

EXE = T4eUnixinfo.exe
  LOGIN = user
  PASSWD = password
  Host = hostname
  FILE = cpu.txt
  RunWait, %EXE% -l %LOGIN% -p %PASSWD% -r %FILE% %HOST% CPU,, hide    ; this works
  FileReadLine, CPULOAD, %FILE%,1
  MsgBox, %CPULOAD%
  FileDelete, %FILE%

Could someone please point me to the source of problem? Thanks
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

BoBo
  • Guests
  • Last active:
  • Joined: --
Kinda coincident --> [here]

Chris wrote:

One thing that might help with command line programs is the fact that you do not need to use %Comspec% unless you're doing a pipe (|), redirect to file (>), etc.

-----

RunWait, %EXE% -l %LOGIN% -p %PASSWD% %HOST% CPU | cb.exe ; this doesn't work

RunWait, %COMSPEC% /C %EXE% -l %LOGIN% -p %PASSWD% %HOST% CPU | cb.exe,, Hide ; this should work



toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
No it doesn't.

I tried it, and it didn't run

RunWait, %COMSPEC% /C %EXE% -l %LOGIN% -p %PASSWD% %HOST% CPU | cb.exe ,, hide

So I tried the one with the temporary file (to check if it is related to cb.exe)

RunWait, %COMSPEC% /C %EXE% -l %LOGIN% -p %PASSWD% -r %FILE% %HOST% CPU,, hide  ;This didn't worked

RunWait, %EXE% -l %LOGIN% -p %PASSWD% -r %FILE% %HOST% CPU,, hide ; This worked

And it isn't related to cb.exe

Any idea?
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
I found the source of the problem. All my scripts were saved in my home directory [Documents and Settings]. But as I'm in a faily big company that directory is on a server somewhere and not on my hard drive.
The solution:
Create a new directory under C:\ and put the scripts there. Then it worked well with
Runwait, %comspec% /c %EXE% | cb.exe,,hide

Thanks for the help.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

BoBo
  • Guests
  • Last active:
  • Joined: --

MyFavouritePath=\\mb-xyz\blabla\toralf\My Documents
Runwait, %comspec% /c %EXE% | cb.exe, %MyFavouritePath%,hide

Should work, as well ?!

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
Hi Bobo,

Thanks, but it doesn't. And I do not want to put a directory in there If I do not have to. Since I'm not sure if the file will stay there. And %a_scriptdir% doesn't work either.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

savage
  • Members
  • 207 posts
  • Last active: Jul 03 2008 03:12 AM
  • Joined: 02 Jul 2004
Just as an aside, winclip (part of the outwit suite) is more powerful than cb.

http://www.dmst.aueb...it-bin-1.23.zip

SYNOPSIS
winclip [-w|-u-m] [-b] -c|-p [file]


DESCRIPTION
Winclip provides access to the Windows clipboard from a console or MS-DOS window. Its operation depends on its mandatory argument. When executed with the -p (paste) argument it pastes the Windows clipboard to its standard output or to the file optionally specified in the command line. The winclip output may be redirected to a file (using the shell > syntax) or to another process (using the shell | pipeline syntax). When winclip is executed with the -c (copy) argument it copies its standard input (or the file optionally specified in the command line) to the Windows clipboard. Again, winclip input may be redirected to be read from a file (using the < shell syntax), or from another process of a pipeline.

Winclip can currently paste text clipboard data, filenames that have been copied in the Windows explorer, and bitmaps which are pasted in binary ppm format. When tranfering text, it will automatically perform the appropriate Windows to console character code (OEM) set translations. Winclip is an Intel architecture Win32 program that only works under the Intel processor versions of Windows NT, 95, 98, Me, 2000, XP.


OPTIONS
-c
Copy the standard input to the Windows clipboard.

-p
Copy the Windows clipboard contents to the standard output.

-w
Transfer text in the Windows-native format (CF_TEXT) instead of the (default) DOS-native format (CF_OEMTEXT). The Windows format is also known as ANSI text, but often represents another Windows and locale-specific code page. Setting this option will affect how non-ASCII characters will be encoded for clipboard text and copied file icons.

-u
Transfer text in Unicode format (CF_UNICODETEXT) instead of the (default) DOS-native format (CF_OEMTEXT). The Unicode encoding is the same as the one used by the Windows clipboard: apparently UCS-16 in little-endian byte order (UTF-16LE). Setting this option will affect how non-ASCII characters will be encoded for clipboard text and copied file icons.

-m
Transfer text in Unicode format (CF_UNICODETEXT) instead of the (default) DOS-native format (CF_OEMTEXT). The Unicode encoding is UTF-8. Setting this option will affect how non-ASCII characters will be encoded for clipboard text and copied file icons.

-b
When outputing Unicode text, prepend a byte order mark (BOM) at the beginning. This may make it easier for other tools to recognize winclip’s output format. When reading data for the clipboard, automatically detect Unicode data, and set the encoding and endianess, if a byte-order-mark is detected.


EXAMPLE
dir /b | winclip -c
Copies the directory list to the Windows clipboard.

winclip -p file.txt
Saves the Windows clipboard to file.txt.

winclip -p | perl -pe "tr/[A-Z]/[a-z]/" | winclip -c
Converts the contents of the Windows clipboard to lowercase.



toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
Thanks Chris,

You are right, it is more powerful. But also slower. I treid it with two scripts with individual GUIs. One is measuring the CPU load of my local machine the other script is measuring the CPU load of a remote server.
With cb.exe both GUIs work simultaniously.
With winclip.exe -c the one for the local machine doesn't show the CPU load any more when both scripts are running.
I assume this is due to speed problems. But in the case of these GUIs I will use cb.exe. But winclip.exe might help in avoiding the temporary text file with login and password for ftp. :) I will at least give it a try.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004
you could also try this (it leaves the clipboard alone... plug, plug :lol: :lol: )

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
I just noticed this topic now and I have the solution: first, download this Unix ports package. It has a bunch of ports of very useful Unix utils, but if you only want the solution to this problem, just extract pclip.exe and gclip.exe and delete the rest. pclip puts the text in the clipboard to stdout, and gclip takes stdin (by way of a pipe, just like cb.exe) and puts it in the clipboard. To make it easier to use these, put them in your windows directory and you'll be able to use them without the path before the name.

I know for a fact pclip and gclip are quite fast and efficient, as I've used them for a long time.

Maybe sometime I'll actually post this in Utils so I don't have to make a separate link and explanation every time someone requests this.

  • Guests
  • Last active:
  • Joined: --
only problem is it uses the clipboard. What if I wanted to keep what was in my clipboard?

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
Not a problem. You already know that AHK's clipboard variable is limited to ascii text. You can get around this by using s3, which can copy binary data from a file to the clipboard. If you can't figure out how to use it in conjunction with pclip and gclip, I can help you out with an example.

  • Guests
  • Last active:
  • Joined: --
it's a matter of preference I guess but the other way seems easier