1. Batch Files
Although Windows Script Host is the most powerful
tool for creating your own helpful programs, it’s also useful to know
how to use the batch file language. Batch files let you take advantage
of the hundreds of command-line programs supplied with Windows.
A batch file, at the simplest level, is just a
list of command prompt commands that have been typed into a file whose
extension is .bat or .cmd. When you enter the name of a
batch file at the command prompt, Windows looks for a file with this
name in the current directory and in the folders of the PATH
environment variable. Windows treats each line in the batch file as a
command, and runs them one after the other as if you’d typed the
commands by hand. At this simplest level, then, a batch file can be a
big help if you find yourself typing the same commands over and over.
Beyond this, there are several commands that you
can use to write rudimentary “programs” within a batch file, so that it
can take different actions depending on what you type on the command
line, or depending on the results of the commands it executes. These
“programming” commands have been greatly improved since the MS-DOS days,
so writing useful batch files on Windows 7 is much easier than writing
them was in the old days. In particular, the IF and FOR
statements have been greatly extended. You can prompt the user for
input. It’s possible to manipulate strings and filenames and perform
arithmetic calculations. You can create subroutines within a single
batch file. And there’s more.
Unfortunately, I don’t have room to provide coverage of batch file programming in this book, but I do in Windows 7 and Vista Guide to Scripting, Automation, and Command Line Tools, published by Que.
And some Microsoft documentation is available online. Go to www.microsoft.com and search for these phrases:
Command Shell Overview
Environment Variables
Using Batch Parameters
Using Batch Files
Using Command Redirection Operators
Cmd
Command-Line Reference
Then, open a Command Prompt window and type the commands
help cmd
help set
help for
help if
and so on.
Batch File Tips
Table 1
lists several short batch files that I put on every computer that I
use. These short command scripts let me edit files, change the path,
view a folder with Explorer, and so on, simply by typing a couple of
letters followed by a folder or filename. They don’t involve fancy
programming, but they save me a significant amount of time when I’m
working with the Command Prompt window.
Table 1. Useful Tiny Batch Files
Filename | Contents and Purpose |
---|
ap.bat | @echo off
for %%p in (%path%) do if /%%p/ == /%1/ exit /b
set path="%1";%path%
Adds the named folder to the PATH if it is not already listed. (This lasts only as long as the Command Prompt window is open.)
Example: ap c:\test |
bye.bat | @logout
Logs off Windows.
Example: bye |
e.bat | @if /%1/ == // (explorer /e,.) else explorer /e,%1
Opens Windows Explorer in Folder mode to
view the named directory, or the current directory if no path is entered
on the command line.
Example: e d: |
h.bat | @cd /d %userprofile%
Changes the current directory to your user profile (home) directory.
Example: h |
n.bat | @start notepad "%1"
Edits the named file with Notepad.
Example: n test.bat |
s.bat | @cd /d c:\scripts
Makes c:\scripts the current directory, when you want to add or edit batch files and scripts.
Example: s |
If you create a c:\scripts folder and add it to the PATH, you might want to create these same batch files in that folder for your own use.
Windows PowerShell
Microsoft has developed a new command-line
environment called Windows PowerShell (WPS), which is installed as a
standard accessory starting with Windows 7. WPS in many ways looks and
acts like the familiar Command Prompt window, but it’s actually a very
strange animal, and it gives you access to some very powerful
programming tools.
I used the word “strange.” Can a computer program
be strange? Definitely! For one thing, most Windows PowerShell commands
(which are properly called cmdlets) generate streams of objects, not text. Objects are computer representations of real-world things. They have properties that describe attributes of the things they represent, and methods
that let you manipulate the things. For example, an object that
represents a specific file on your hard disk might have properties like Name, Size, and LastWriteTime, and methods like Delete, Edit, and Open. Windows PowerShell works with objects in a new, unusual, and ultimately very powerful way.
Now, if you type dir in a regular Command Prompt window, the command shell interprets dir and generates a bunch of text listing the current folder’s files by name. The dir command is programmed very specifically to print information about files in text form. That’s all it can do.
In WPS, you can type dir and this will also print out a list of filenames, but something completely different is happening behind the scenes. In WPS, dir is a shortcut for the Get-Childitem cmdlet, which in its simplest use generates a stream of File objects; each object represents one of the files in a folder, and each object has properties and methods (for example, name and size).
When an object (of any sort) lands in the WPS prompt window, WPS prints
out a line of text listing the object’s most important properties. For a
File object, this includes the file’s name, size, and the date it was created. So, when you type dir, WPS produces a stream of File objects and they end up as a nice, tabular listing of files.
The end result is the same as in the old Command
Prompt environment, but it’s happening in a general, more abstract way.
The cmdlet doesn’t know about or care about text or formatting: it
simply spits out a bunch of File objects. And the WPS window will turn any
list of objects into a nice tabular listing. Files, user accounts, hard
drives, Windows services; whatever object a cmdlet throws into the WPS
window turns into a nice text listing.
In addition, WPS includes a full-scale
object-oriented programming language and has access to Microsoft’s .NET
programming platform, which means WPS scripts can perform complex
computations and communicate with other computers and networked
(“cloud”) services.
WPS even lets you do complex things with objects without programming. You can use the familiar |
pipe symbol to direct streams of objects from one cmdlet to another,
and this lets you do very complex, specific things with tools that are
separately very simple and general-purpose in nature. For example, the
following command will delete all files in the current folder that are
more than 6 months old:
dir | where-object {$_.LastWriteTime -lt (get-date).addmonths(-6)} | remove-item
It looks complex at first, but it’s not so bad. This command line strings three separate cmdlets together:
dir— Spits out a list of all the File
objects in the current directory. Here, they don’t land in the WPS
command window, so they don’t make a text listing. Instead, the pipe (|)
symbol instructs WPS to pass the objects to the next command.
where-object—
Passes just some of the objects through, based on the “filtering”
condition inside the curly brackets. In this example, it passes through
only those files that have not been changed for more than six months
(that is, whose LastWriteTime value is less than the date/time six months back). So, objects representing just the old files are piped to the next command.
remove-item— Deletes the hard disk files corresponding to each of the file objects it receives.
Caution
Don’t just open a WPS
window and type this command to see whether it works! You’ll most likely
delete a bunch of important files from your Windows profile folder. If
you want to see whether it works, type just the first two parts of the
command: dir | where-object {$_.Last-
WriteTime -lt (get-date).
addmonths(-6)}
This will print out a list of the selected files but will not delete them. |
As
I said earlier, you’re not limited just to using commands that you type
into the WPS window. WPS has a full-scale programming language with
variables, loops, subroutines, user-defined objects, and so on. You can
use these at the command prompt or in script files. You also can create
shortcuts (called aliases) for commonly used commands and scripts to make typing easier, and a bunch of aliases are predefined for you.