March 10, 2004

Copy and Paste Files from the Command Line

I’m one of those programmers that tries to use the keyboard and mouse equally. I do this mainly because I hate to clutter my desktop, Quick Launch bar and Start menu with a zillion icons.

I tend to write a lot of batch files. I have a directory called C:\utils, which has interesting binaries I’ve collected over the years. I also have a C:\utils\bat directory with abotu 150 batch files I’ve written over the years. Some of the common batch files are:

n —Starts my default text editor
eb —edits a batch file in C:\utils\bat, without having to specify the path
set_env —Using Python and Win32all, I can set an environment variable permanently, not just in my shell
desktop —Changes to my desktop folder, at the command prompt: C:\Documents and Settings\ncody\desktop.

I’m also a big fan of Cygwin, since I need to ssh and scp files to my web server almost daily. To copy files from my desktop to Cygwin, I generally type “explorer .” while in my Cygwin home directory and drag the file from my desktop to the new explorer instance. I close explorer and now have the file available.

I also have a bunch of batch files that switch me to important directories. All of these batch files use the most excellent pushd* and *popd commands, so I can move from directory to directory and always have the ability to go back up my current directory stack.

One welcome addition to this workflow is a new utility, written in C# that allows you to copy files from one directory into another. You can read about it here.

All you need to do is say:

ezclip copy myfile.txt
cd some_other_directory
ezclip paste

Done. I’ve already been using this tons, which is a lot easier than navigating my complex directory tree in explorer.

UPDATE: I noticed that wildcards, directories, and sub-directory traversal are not supported. It seems to me that this wouldn’t be hard to implement after looking at the source.

UPDATE #2: With a minor tweak, I made a minor improvement to the code. Unfortunately, you can no longer use the multi-file syntax using my method (file1 file2 file3), but you can use wildcards. To do this, I simply changed this like:

string[] files = new string[args.Length - 1];
Array.Copy(args, 1, files, 0, files.Length);

To this:

string[] files = Directory.GetFiles(@".", args[1]);

To make this code production quality, of course, I shouldn’t have to specify “.” as the current directory. If args[1] specifies a directory, I should extract it and use that. Also, it wouldn’t be hard to accept multiple files as Gus Perez did originally, allowing each argument to be a single file or a filespec (myfile.txt *.log *.xml)

Posted by Nick Codignotto at March 10, 2004 11:03 PM
Posted to Programming

Valid XHTML 1.0!   Valid CSS!