Besides Emacs and Firefox, the program I use most is probably the Windows command prompt. My daughters make fun of it, and telling them that in the old days the entire computer screen was just one big version of that window is like telling them that at their age, I didn’t know that “The Wizard of Oz” went from black and white to color when Dorothy landed in Oz because our TV set showed everything in black and white. Still, the “DOS box” is the command line interface to the operating system that I need to use the most.
Years ago I often tried to push the capabilities of the batch file language further, but it’s not a real scripting language. To get beyond simple tasks, I would just write a perl script to do what I needed or else make the argument that the process in question was better off running on a Unix-based box. Recently, however, I learned that the Windows 2000 command language can do more proper programming language tasks than I realized, such as finding substrings of variables, prompting for input, differentiating between string and numeric variables, and even generating random numbers.
I’ll keep the description to a minimum and just demonstrate. First, substrings:
C:\>set teststring="abcdefghijkl" C:\>set testsubstring=%teststring:~2,4% C:\>echo %testsubstring% bcde
Prompting the user and storing the result in a variable:
C:\>set /P color="what color?" what color?red C:\>echo %color% red
C:\>set i=3 C:\>set x=%i%+5 C:\>echo %x% 3+5 C:\>set /a i=3 3 C:\>set /a x=%i%+5 8 C:\>echo %x% 8
My favorite thing in any programming language: a random function!
C:\>echo %random% 28278 C:\>echo %random% 3127 C:\>set /a i=%random% % 3 1 C:\>echo %i% 1 C:\>set /a i=%random% % 3 1 C:\>set /a i=%random% % 3 0 C:\>set /a i=%random% % 3 1 C:\>set /a i=%random% % 3 0 C:\>set /a i=%random% % 3 2
You can stack commands on one line, and if you use two ampersands, the failure of one command means that the command processor won’t try to execute succeeding ones:
C:\>cd \bin && echo cd successful cd successful C:\bin>cd \xx && echo cd successful The system cannot find the path specified. C:\bin>
Something that isn’t part of the command language, but which an Emacs geek like me was happy to learn about, is tabbing for completion of both directory and file names. In addition to completing a name, it often adds quotes for you:
C:>cd doc[tab] C:\>cd "Documents and Settings"
I learned about the substring trick on a mailing list, and when I did a few searches to find out more, I learned about the others at two articles that I found: Windows 2000 and DOS Help and Tips and Information about the SET command.
I’m guessing that for each person reading this, some of this is old news, but that one or two things are new to you. I know that bash, sh, and other Unix scripting shells are in cygwin and therefore available to Windows users, so that there’s still no reason to write complex scripts in the Windows batch command language. Still, it was fun to see that an old dog that I’d lived with for many years had learned a few new tricks when I wasn’t watching.
You should also check out the information about the FOR command, which since Win2K has been able to recurse over all the files in a directory tree, among other things.
Thanks Tony! Also, I didn’t mention the IF command, which doesn’t seem to have progressed over the years, but does give batch files a fairly basic programming language capability. (And, I suppose, GOTO, something that the younger folk may not even recognize.)