Log in

View Full Version : Print cmd output to file.


nirpaudyal
5th October 2007, 17:41
Hello all.

I am looking to print the entire output of a command prompt window to a text file (note - not only the output of the current command) - is this possible without using any programming software such as C++ or VB?

Ta!

JohnnyMalaria
5th October 2007, 18:37
You can mark the output and copy it to the clipboard. Click the cmd window's "C:\" icon (top left), edit, mark.

If you have a lot of output, you'll need to configure the cmd prompt window to keep a history buffer (under Properties).

HyperHacker
5th October 2007, 20:03
I *think* you can put all the commands you want to run in a batch file, and pipe that batch file's output to a file (foo > bar.txt), and it will pipe all the commands to that file. I haven't tried it though, so don't quote me on that.

nirpaudyal
5th October 2007, 20:05
Hi,

Ta for the reply. Are there no command that can be entered into dos prompt to save everything to a text file?

Alternative is a command to the contents of a dos prompt window to the printer.

any help?

Unearthly
5th October 2007, 20:22
> [filename]
Example:
echo 1 2 3 4 > log.txt

It won't display the output anymore though.

Shinigami-Sama
5th October 2007, 22:15
umm
find tee for windows, I seen it on sourceforge

it should allow you pipe into file and back onto the screen

just like unix tee

nirpaudyal
5th October 2007, 22:32
Ta for the help attempts guys.

HyperHacker - your method did not work. only wrote the contents of the bat file :(

Unearthly - I was looking to copy everything in the cmd window, not just the output from the last command.

**for me, it does not matter if the output is shown on screen or not... just need it in a file.


Shinigami-Sama - I am about to try this method.

Ta

nirpaudyal
5th October 2007, 22:41
Shinigami-Sama - tee for windows does not copy the contents of the entire window, only the last command output.

help me!

setarip_old
5th October 2007, 22:44
See if the "Echo" command serves your purpose:

http://www.computerhope.com/echohlp.htm

JohnnyMalaria
5th October 2007, 23:36
What's wrong with my suggestion? It will copy the entire contents of the cmd window so you can paste it into notepad etc and print it.

Are you looking for method that doesn't require doing the copy/paste manually?

JohnnyMalaria
5th October 2007, 23:45
Another method that may work depending on what you need:

Open a cmd window.
In that window, enter this command:
cmd >mycmdout.txt

You will now be in a cmd session but you can't see anything that is output/prompts etc. However you can still type commands/run batch files etc. e.g., enter 'echo hello'
To finish, enter 'exit'

This will return you to the first cmd window. You can use type to see the output from the "hidden" cmd window:

type mycmdout.txt

Microsoft Windows XP [Version 5.1.2600] <--- output from startup of cmd session
(C) Copyright 1985-2001 Microsoft Corp.

C:\>hello

If you only need the output, this will work.

nirpaudyal
6th October 2007, 00:46
Guys, thank you all for the replies. Unfortunately, its still not solving my problem.

setarip_old - Echo command will only copy what follows after "Echo" to a file.

JohnnyMalaria - I need everything in a window to be copied. The window has everything thats needed to be copied within it, I just need to write it to a file - This is to be done through some sort of dos command, and not through a manual copy/paste action.

Basically, Its like having a text box full of text already, then programmatically saving it to a text file without adding anything to it.


---
Ideas?

foxyshadis
6th October 2007, 02:34
I suspect Sendkeys is your only real option. Save the clipboard, then send Alt+Space, E, S, Alt+Space, E, C to the window, save the clipboard to a text file, and then restore the old clipboard.

If there is a way to read text off a command window I've never seen it, sorry.

shevegen
6th October 2007, 07:25
Although others already presented solutions, i recommend using mingw to get a basic bash-like environment

This way you get to use "Unix" on Windows, including all shell functions (if you wrote them), aliases (if you want to define them) and the usual shell magic to do stuff (i think command piping as well)

foxyshadis
6th October 2007, 08:20
I know how you might go about copying the full contents of an xterm buffer in linux, but I don't have any idea how you'd do that in mingw, so I don't think that really solves this particular problem. I can't really figure out why this is even a problem in the first place, it really smells like trying to hammer a nail with a shoe (http://weblogs.asp.net/alex_papadimoulis/archive/2005/05/25/408925.aspx) to me.

You get a lot of posix with unix utils (http://unxutils.sourceforge.net/) anyway, and piping and aliasing (http://open.itworld.com/5040/nls_unixdos2050804/page_1.html) already works in cmd. You don't get bash scripting, but no big loss, better to get a decent scripting language than cajole bash or batch into doing anything useful, imho.

nirpaudyal
6th October 2007, 15:16
Thank you all for the attempted help!

The solution which is closest to the best for my problem has been offered by foxyshadis - the sendkeys method.

I was actually looking for a dos prompt command, but did not realize that something so simple does not actually exist!

I am not looking to use any third party software, so mingw will have to be bypassed (shevegen, ta anyhoo). I will just use the sendkeys - possibly its API equivalent to ensure 100% success!

thank you people for offering support.

JohnnyMalaria
6th October 2007, 16:03
If you are doing this programmatically then there are Windows API functions that can be used to get the output from a console window.

ReadConsoleOutput

The ReadConsoleOutput function reads character and color attribute data from a rectangular block of character cells in a console screen buffer, and the function writes the data to a rectangular block at a specified location in the destination buffer.


BOOL ReadConsoleOutput(
HANDLE hConsoleOutput,
PCHAR_INFO lpBuffer,
COORD dwBufferSize,
COORD dwBufferCoord,
PSMALL_RECT lpReadRegion
);

If you have Visual Studio documentation, it is under Console Functions in the Platform SDK: DLLs, Processes, and Threads section.

Basically, this command does the mark/copy I described before but without the need for manual operation.

You also need to use AttachConsole(..) in your program to attach to the console of another program that you want to control via the Windows API.

Shinigami-Sama
6th October 2007, 18:26
oh I'm an idiot

you can enable telnet(Or rather SSH) and then remote into yourself with your hyperterminal(or putty can do it too I think) and tell it to save the output to a file

we used to do that on the cisco routers a lot

its not the most secure way mind you but it'll work

pc_speak
9th October 2007, 09:36
Hi.
You want snipper.com & snipper.doc 'circa 1987'

I have used this prog many many times. Run it from a command prompt. Takes a little to get a handle on it, but it does work. I just tried it with XP without a problem.

Found here.
http://www.filewatcher.com/b/ftp/ftp.ualg.pt/users/dubuf/unix4dos.0.0.html

LOL