Shock News (to me anyway): Windows Command Line is actually useful

Having used Windows since my earliest memories, I’ve only rarely used the command line. I never found it that useful and was always annoyed by the fact that I couldn’t click somewhere in my command to edit it, and instead had to use the arrow keys to move it forwards and backwards.

However, I’ve recently had to carry out quite a few batch tasks in Windows and have, to my surprise, found the command line to be pretty useful. So, as much to remind myself as anything, I thought I would note down the things that have helped me.

Changing File Extensions

This is easy using the REN command, e.g. REN *.svs *.tif

FOR Loops

As you would expect, this loops over a set of files/directories and carries out a command on each. The basic structure of a FOR command is as follows:

FOR %V IN (set) DO command [command-parameters]

The %V is a variable (it does not have to be ‘v’, but must be a single letter, and it is case sensitive, i.e. %v and %V are different) that can be used in the command parameters to refer to each of the files matched by set. As a spurious example, to echo all of the files in the current working directory, you would give the following command:

FOR %V IN (*) DO echo %V

Variable Modifiers

Variable modifiers can be used to output the variable in a variety of ways, e.g. for %V above:

  • %~fV outputs the full path name for the file
  • %~nV outputs the file name only
  • %~xV outputs the file extension

There are others apart from those mentioned above – see the FOR command help for more.

FOR /D

If the /D extension is used, the FOR command will match  directories rather than files.

For example, to separately zip up all of the subdirectories in the current working directory

FOR /D %D IN (*) DO C:\zip\zip.exe -r "%~fD.zip" "%~fD"

Using FOR in batch files

If you are using a FOR command in a batch file, it is necessary to use, for example, %%V, rather than %V for the variables.

START /WAIT

I was using the START command in a batch file to carry out a certain command on multiple files, but I needed it to wait until one command had finished executing before it called the next. Thankfully there is the /WAIT parameter, which means that the next command will not be called until the previous one has finished.

2 Replies to “Shock News (to me anyway): Windows Command Line is actually useful”

  1. Sadly, the standard Windows cmd window is shockingly bad. As you say, trivial things like copy and paste are almost unusable. Fortunately, the open source world has come to the rescue: http://sourceforge.net/projects/console/ makes life much pleasanter. Still not as good as a Linux terminal window 10 years ago though.

    Speaking of Linux, the best thing you can do on Windows is to install cygwin (http://www.cygwin.com) then you have all the power of the Linux command line on Windows.

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.