Copy all text from Command Window programatically - cmd

This article shows the well-known way to manually copy text from a Command window.
I'm looking for a way to do this programmatically. How could the cmd script that is running select all the text in its output window, and write it to a file?
I've found tools to do a screen capture into a file, but that may miss output if it has scrolled out of view? I'm looking for a way to capture an unattended cmd script's output (without modifying the whole script to redirect every line to a file).
Possibly this could be done with the windows API by finding the cmd window, sending CTRL-A and CTRL-C keycodes, and then write the clipboard text to a file. A possible approach for clip2txt is presented in this post.
Is there any other approach that should be considered?

Related

Can a Windows batch file determine its own window name?

When a Windows batch file is launched via a shortcut (from the Start Menu, for example), its window will typically receive the title of the shortcut.
Is it possible for the batch file to determine the name of its own window?
This question is related to Can a Windows batch file determine its own file name?, but that question asks if the batch file can determine its own filename. I am asking if a batch file can determine its own window name.
StackOverflow user QcFe lent a kind hand by finding this QA on Superuser: https://superuser.com/questions/1155853/how-to-get-the-window-title-text-from-batch-file
The short answer is that Windows provides no simple and direct way for batch files to accomplish this task.
Windows provides the internal title command to set the window title, but no way to retrieve it. (Which is disappointing, because it would be such a simple addition to the title command.)
The functionality can be attained using PowerShell or possibly tasklist, but for the latter, there are issues that need to be addressed if there are more than one command shell window open.
I elected to force the window to have a unique name using the Title command. That way I could reference it correctly for tools that need to know the window title, such as NirSoft's excellent nircmd utility.

Read current terminal

Is it possible to read what's currently displayed on the windows terminal pragmatically using any available API?
For example, I've got an app that tail's some log files. I'd like to be able to hit a key and open a text editor at the line that is currently being viewed. The problem is the terminal also has scroll bars.
Not easy. Perhaps you could capture the screen and use OCR to identify its contents, or make a shortcut to some sort of macro that selects all the screen and copies the text. But there is no API available to perform the task you ask.
Of course, you can tee the command you're running in the console to a file, and open such file with an editor whenever you like, however it will show the full output of the command and not the visible part. If you like more information on that topic, it is answered in SO - Displaying Windows command prompt output and redirecting it to a file
.

How to use ExifTool in a batch file?

ExifTool is a free and open-source software program for reading, writing, and manipulating image, audio, video, and PDF metadata.
It has one problematic feature, after it finishes it stops and tells you to 'press RETURN' to continue which is a game breaker, because if you use it in some batch file that also performs other commands, all them gets delayed until user manually presses ENTER each time on his keyboard.
How to bypass that?
From the exiftool main page:
"rename to "exiftool.exe" for command-line use."
Exiftool allows for command line options to be accessed through drag and drop if you put them in the name of the program. In the default case, the -k option is in the name to keep the CMD window open so users and drag/drop and then read the output.

open a text file via windows double click with ruby

I'm fairly new to Ruby, so please excuse me if my question is too basic...
I wrote a program that searches a text file for certain values.
The way it works is that when I run the program from command line it asks for the file to search through. So I drag the file over to the command window and press Enter. The program takes over and does what it's supposed to... I also created an exe file using Ocra just so I don't have to run it from command line.
It works great, but I want to take it a step further. How can I modify my program to be able to double click the text file in Windows and have my program automatically open and do it's search?

how to detect programmatically when a powerpoint slideshow starts

I would like to write a windows program that would enable the user to choose a ppt file. When that ppt file starts the slideshow, my program would automatically display some message in front of the slideshow. When the slideshow is over, my program would close itself. What do I need to learn to write this program?
I'm not good with VBS (which I'm sure you could use for this) but if you set your show's extensions to .PPS instead of .PPT that will force them to start playing automatically on open. Then you won't need to see when the show starts because you'll control that.
As for opening it, if you write BATCH script you can use it to prompt for a file location but it will all be manually typed because it will load from the command window. If a show opens, the command screen can stay open until that slideshow closes, then continue with the next line of script.
Depending on if these are your powerpoints that you want opened, I personally would put VBA script in them to trigger On_Open and On_Close That will get you a clean customizable popup error message.
Edit
After doing some quick searching the On_Open and On_Close is not found in PPT, you would need to install an add-in.

Resources