You can make DOS more of a first-class Windows citizen with this handy little clipboard utility.
Even with today's GUI operating systems and "visual" development tools, I generally have one or more Command Prompt windows running on my desktop. I may use them to manipulate files, start a script, launch an application, ping another machine, etc. In many instances, the Command Prompt provides a highly efficient (even if sometimes cryptic) means of accomplishing a task that can simply not be done with the same ease in the GUI environment. The command-line buffer (DOSKEY) makes the Command Prompt even more productive by allowing you to quickly perform a task or sequence of tasks again with minimal effort.
My one complaint with the Command Prompt is that it is not well integrated with the rest of the Windows environment. It does offer cutting and pasting of text between the command-line window and other applications, and if you drag a file and drop it into a Command Prompt window, the file path will be pasted onto the command line. I would like to go a step further with this integration. For example, I want to be able to copy files from Windows Explorer to the Command Prompt's active directory via the Windows clipboard. Obviously, it is not too difficult in Windows Explorer to copy the files to the clipboard, go find the destination directory, and paste the files. I could also use the Command Prompt's copy command and key in the paths of the files I want to copy, but this approach also requires more effort than it should.
This article provides a very simple utility, CBcopy, that will paste files copied onto the Windows clipboard into a Command Prompt's active directory. While CBcopy has a very specific use, it is generically applicable, and its simple design can be extended to perform a variety of similar tasks in a Command Prompt environment.
CBcopy Usage
CBcopy's goal is to copy any file that may be on the Windows clipboard to the active directory of the Command Prompt where it is run. Simply running the utility with no options will copy all files on the clipboard to the current directory, including any directories and their contents. The -d option will prevent subdirectories from being copied, and the -f option will cause a file copy to fail if the file already exists. The -s option is a cosmetic silent mode to prevent the utility from displaying the filenames as it copies, and the -nc option lists the filenames on the clipboard instead of copying them. The utility's usage is shown below:
Usage: CBcopy {[-d | -s | -f | -nc]} -d (do not copy subdirectories) -s (silent mode) -f (fail if file already exists) -nc (do not copy files, just list) -? (show usage)Implementation
CBcopy (see Listing 1, CBcopy.cpp) works by opening the Windows clipboard and querying for a drop file handle (CF_HDROP), which points to a list of files currently on the clipboard. If a drop file handle is present, the DragQueryFile function retrieves the list of file paths and then processes each file. Regular files are copied to the Command Prompt's active directory. Directories are recursively scanned, and the files they contain are processed. (The functions OpenClipboard, GetClipboardData, GetCurrentDirectory, GlobalLock, DragQueryFile, GlobalUnlock, and CloseClipboard are Win32-specific APIs.)
Conclusion
CBcopy serves a fairly specific purpose. However, being able to do something that was previously not possible provides a new branch in your decision tree for finding the easiest way to accomplish a task (something that seems to proliferate in the developer community). CBcopy is just one tool, but there could be a collection of command-line tools that interact with the Windows clipboard or other aspects of Windows that are not generally considered in the command-line environment. The Command Prompt is not going away any time soon, so the use of utilities like CBcopy can further enhance the productivity of both the Command Prompt and the GUI environment by leveraging the best parts of each.
Joey Rogers is an application developer for Intergraph Corporation in Huntsville, Alabama. He is also the author of Object-Oriented Neural Networks in C++. He can be reached at samuel_joe_rogers@hotmail.com.