Introduction
cmd.exe is default shell for all Windows client operating systems, from Windows XP to Vista, as well as the default shell for Windows NT 4.0 through Windows 2003. It is a
piece of crap. It has inconsistent syntax, is poorly documented, and is all-around hard to use for nontrivial tasks (and some trivial ones). Following is a bit of evidence to support my claim. I would not be surprised if
cmd.exe is responsible for stunting the growth of Windows software developers and IT professionals. It scares people away from the console and into GUIs that, while good for one-off activities, are hardly automatable. In fact, I'll bet part of the reason Microsoft created VBScript and WScript was because people wanted to automate processing and
cmd.exe was not up to the task.
CliDebugger
CliDebugger.exe is a small, handy .NET console program that simple examines console input, to show how cmd.exe is passing arguments into programs.
static void Main(string[] args)
{
Console.WriteLine("{0} arguments {1}", args.Length, Environment.NewLine);
Console.WriteLine(
args
.Select((s, i) => string.Format("{0} : <{1}>", i, s, s.Length))
.Join(Environment.NewLine));
}
One Escaped Quote
Why isn't this getting passed to
cat?
C:>clidebugger -T "s/"//g" sedtest.txt | cat -n
6 arguments
0 : <-T>
1 : <s/"//g>
2 : <sedtest.txt>
3 : <|>
4 : <cat>
5 : <-n>
Two Escaped Quotes
The numbering indicates output to
cat, but what if I don't want the last
"?
C:>clidebugger -T "s/"//g" sedtest.txt " | cat -n
1 4 arguments
2
3 0 : <-T>
4 1 : <s/"//g>
5 2 : <sedtest.txt>
6 3 : <">
Looping through files
Notice the syntax requirement; the highlighted code is invalid.
for /r %%f in (*.csv) do (
echo %%f
)
for /r %%f in (*.csv)
do (
echo %%f
)
for /r %%f in (*.csv) do
(
echo %%f
)
Echo is special
Echo gets special dispensation — it is able to see the spaces between arguments.
E:>echo what when
what when
CliDebugger gets no such treatment. No matter how many spaces are between
what and
when, if they are not surrounded by quotes, those spaces are just a big delimiter.
E:>clidebugger what when
2 arguments
0 : <what>
1 : <when>