Tuesday, February 4, 2014

A Command Extension Conundrum

So far as I know, the documented way to test whether command extensions are enabled is something like the following.

   echo %0, Version 1.00 Starting

   if CMDEXTVERSION 1  goto CHK_REQ1

 

    echo.

    echo This script requires command extensions to be enabled. Since they are

    echo enabled, by default, they have been disabled, directly or by GPO.

 

    goto ERR_DONE

 

 

:CHK_REQ1

For the first time in a long while, I temporarily disabled command extensions to perform a special test. To verify that command extensions were actually disabled, I ran a command script that begins by verifying that command extensions are enabled. I expected the error message listed above to be displayed. This is what actually happened.

C:\bin>CopyOneToAll.CMD

CopyOneToAll.CMD, Version 1.00 Starting

1 was unexpected at this time.

C:\bin>

This isn’t exactly what I expected.

Since I can complete the test without further investigation, the mystery has been put aside for now.

David A. Gray

Chief Wizard

WizardWrx

 

Email: dgray@wizardwrx.com

WWW: www.wizardwrx.com

 

Cell: +1 (817) 298-0867

Land: +1 (817) 812-3041

 

 

4014 Double Tree Trail

Irving, TX  75061-3936

 

Saturday, January 25, 2014

Retrieving Previous Version of File from Windows Recycle Bin

Today, when I went in search of a previous version of a file that I had just sent to the Recycle Bin, I noticed that the context menu has a Cut option. Since I had already reused the file name in the directory from which I deleted the other version, I decided to see what happened when I cut the file.

Much to my delight, I was able to paste the file into a different directory. This feat enabled me to preserve the good copy that I had just assigned, while allowing me to temporarily restore the discarded file, so that I could retrieve a snippet of text from it.

David A. Gray

Chief Wizard

WizardWrx

 

Email: dgray@wizardwrx.com

WWW: www.wizardwrx.com

 

Cell: +1 (817) 298-0867

Land: +1 (817) 812-3041

 

 

4014 Double Tree Trail

Irving, TX  75061-3936

 

Saturday, April 14, 2012

Basic for the 8080

Most people believe that the first BASIC published by Bill Gates was for the 8080, which accounts for the last 4 digits of the main phone number for Microsoft Corporation. However, I'm almost certain that I saw a Microsoft logo banner on the Basic interpreter that I used for a project I did when I was in graduate school. My recollection of that project, while a bit fuzzy (We're talking 1978 - 34 years ago!), is that the CPU in the machine on which I developed it was an Intel 4004, and that the interpreter displayed a Microsoft copyright notice when it loaded. Nevertheless, since the 8080 had been around since 1974, it might well have been the CPU in that box.

Tuesday, August 23, 2011

If (!expression) Considered Harmful

Let me begin by saying that I don’t feel any need to apologize for adapting the well- known aphorism, “GOTO Considered Harmful,” because I think this little essay ranks with it in importance.

Since I began programming as an almost daily activity in 1977, I have learned and used, one way or another, and for an equally wide variety of reasons, many programming languages, including the following (in no particular order): Fortran, COBOL, IBM 307 Assembler, Intel/Microsoft Macro Assembler, Perl, Basic (everything from Bill Gates’ original Basic for the Intel 4004 through current day Visual Basic .NET 2010), C, C++, C#, SQL, the DataEase Query Language, and the Microsoft batch language in all its forms. Due to the nature of my work, I often use two or more languages in the span of a single work day, and they can run the gamut from basic DOS batch files to advanced C, back to back.

The advantage of this huge diversity is that I’ve seen and used dozens of idioms and design patterns, some of which appear in only one or a handful of the languages cited above. The case of interest today is the unless idiom, found only in Perl, so far as I know. There is a simple example in the section titled “and, or, & not” of Perl Idioms. Programming Perl, the Bible of Perl, covers it in 4.3. if and unless Statements.

In terms of demonstrating the power for clarification of the unless idiom, the examples usually cited to illustrate it are pretty lame. Nevertheless, since their aim is to demonstrate the idiom, itself, their simplicity is justified. Following are some examples from production Perl scripts.

if ( !$gmatches )

if ( !$_ ) {                    # Is line blank?

if ( !$paramisok ) {            # Warn about invalid lines.

if ( !$appending )

if ( $_ =! /^.U / )             # Skip detail lines that list unencrypted files.

The last one is especially troublesome, because it joins the result of two expressions, both expressed in negative terms, with a logical and operator (&&), which has positive semantics.

Here are a few in C, which has a similar grammar.

if ( !CloseClipboard ( ) )

if ( !IsTextUnicode ( pBOMInfoP6C->lpCharBuff , ( int ) pBOMInfoP6C->dwNBytes , &intUnicodeTestMask ) )

if ( !( utpOutFile = fopen ( pszTestCipherTextFQFN , "wb" ) ) ) {

Wait a minute, I hear you saying. The whole idea of an if statement is to do something if a condition is true.

Exactly! That’s why the not operator (!) is so harmful.

Enter the unless Idiom

Now, let’s rewrite the above expressions, starting with the Perl examples.

unless ( $gmatches )

unless ( $_ ) {                 # Is line blank?

unless ( $paramisok ) {         # Warn about invalid lines.

unless ( $appending )

if ( $_ =! /^.U / )             # Skip detail lines that list unencrypted files.

Next, let’s give the C statements the same treatment.

Unless ( CloseClipboard ( ) )

Unless ( IsTextUnicode ( pBOMInfoP6C->lpCharBuff , ( int ) pBOMInfoP6C->dwNBytes , &intUnicodeTestMask ) )

Unless ( ( utpOutFile = fopen ( pszTestCipherTextFQFN , "wb" ) ) ) {

Hold that thought. None of the above three statements is valid C. Indeed, out of the box, it is invalid in every version of C of which I am aware, which includes Microsoft Visual C++ 6.0 through Visual C# 2010, in addition to the open source standard, GCC.

Unless in C

Please excuse the pun, but the unless idiom can be implemented in C and (and C++).

All you need is a C preprocessor capable of expanding and substituting into a one-line macro.

Let’s have the drum roll, please. Here it comes.

#define Unless(pexpr) if ( !(pexpr) )

Make this one-line macro visible to all of your C and C++ code by including it into a header that you always include, and you can clarify the intent of your code, unless you really need all those obtuse if ( !expressions.

Wait, you say, my compiler doesn’t support macros. There is another way, which is just as straightforward, still fits on one line, and goes into that header that you always include.

__inline Unless(pexpr) { return ( !( pexpr ) ); }

Fight coding horrors, one expression at a time.

 

 

Wednesday, March 17, 2010

Better Batch Files Through Command Extensions

The first batch files arrived alongside the very first versions of MS-DOS and PC-DOS. If you have been around the personal computer for many years, you undoubtedly remember, perhaps not fondly, AUTOEXE.BAT. Power users probably had two or more of them, and some means by which to choose which one to use the next time they engaged their DOS boot diskette. Later came such features as batch files that displayed menus, read inputs from the beloved DOS prompt, and made processing decisions based on those inputs. More sophisticated batch jobs accepted command line arguments, just as did the commands that were built into DOS itself, and the dozens of utility programs, such as XCOPY, that came with it.

The Primitive Command Line Interface

While those early batch files accepted arguments, sometimes called parameters, the command parser was very picky. Testing the value of an argument was extremely limited, and followed a syntax that was familiar to C programmers, but alien to most other computer users, as shown in Listing 1.

IF "%1" == "DE4DATA" GOTO CHECK_1
IF "%1" == "de4data" GOTO CHECK_1
IF "%1" == "De4data" GOTO CHECK_1
IF "%1" == "De4Data" GOTO CHECK_1
 
Listing 1 shows the only way you could have any degree of case insensitivity in your command line argument tests in MS-DOS and PC-DOS.

Listing 2, below, shows a set of tests that gives more complete coverage.

IF "%1" == "DE4DATA" GOTO CHECK_1
IF "%1" == "de4data" GOTO CHECK_1
IF "%1" == "DE4data" GOTO CHECK_1
IF "%1" == "DE4Data" GOTO CHECK_1
IF "%1" == "DE4DAta" GOTO CHECK_1
IF "%1" == "DE4DATa" GOTO CHECK_1
 
IF "%1" == "dE4DATA" GOTO CHECK_1
IF "%1" == "de4DATA" GOTO CHECK_1
IF "%1" == "de4dATA" GOTO CHECK_1
IF "%1" == "de4daTA" GOTO CHECK_1
IF "%1" == "de4datA" GOTO CHECK_1
 
IF "%1" == "De4Data" GOTO CHECK_1
IF "%1" == "De4DaTa" GOTO CHECK_1
IF "%1" == "De4DatA" GOTO CHECK_1
 
IF "%1" == "DE4data" GOTO CHECK_1
IF "%1" == "DE4Data" GOTO CHECK_1

Listing 2 requires 16 tests to provide partial case sensitivity.

That is 16 lines of code to provide partial case sensitivity for one command line argument! Although the example above is extreme, it illustrates why batch programmers usually confined themselves to terse parameter values.

The Command Parser Grows Up

Windows NT brought with it a new command processor, CMD.EXE. Unlike its ancestor, COMMAND.COM, CMD.EXE is a full fledged 32 bit console mode Windows program. It can do everything that COMMAND.COM can do, and much more. One of its most useful new capabilities arises from command extensions, which are enabled by default. You can turn them off, but why?

IF "%~1" EQU ""                         GOTO DO_ALL
IF /I "%~1" EQU "DE4Data"               GOTO CHECK_1
IF /I "%~1" EQU "Home_Office"           GOTO CHECK_2
IF /I "%~1" EQU "Remote_IncrEase"       GOTO CHECK_3
IF /I "%~1" EQU "Home_Office_IncrEase"  GOTO CHECK_4
IF /I "%~1" EQU "UTIL"                  GOTO CHECK_5
IF /I "%~1" EQU "QB4_Source"            GOTO CHECK_6
Listing 3 does, in its second of only 7 lines, much more than the 16 lines shown in Listing 2, because it is fully case insensitive.

Listing 3, taken from a production batch file, illustrates several of the capabilities provided by command extensions.

  1. The second through seventh lines modify the IF command with a new /I switch, making its tests fully case insensitive.
  2. All seven lines take advantage of another command extension, the tilde (~), to strip away quotation marks around the command line argument ("%~1"). I'll say more about this shortly.
  3. The third command extension illustrated in Listing 3 is the mnemonic relational operator, EQU. Thanks to command extensions, the IF command now supports a complete set of relational operators.

Who Can Use Command Extensions?

By default, command extensions are enabled, and are available in all versions of Windows that derive from the Windows NT code base, starting with Windows NT 4. In addition to NT 4, this includes Windows 2000, Windows XP, Windows Server 2003 and 2008, Windows Vista, and Windows 7.

Why Keep the Quotation Marks?

Despite many improvements, the command parser still has a few quirks, one of which is that it sometimes gets confused by bare words that are neither internal commands, nor recognizable program or batch file names. Thus, it is usually best to keep the quotation marks around the text against which an argument is being compared. However, if the argument is enclosed in quotation marks because it contains embedded spaces, and the test, itself, is also enclosed in quotation marks, the result is that the comparison string is enclosed in two sets of quotation marks, as shown in Table 1.

Command Line Argument

“Document to Process.doc”

Old Style Comparison

If “%1” == “This is the Document.doc”

Outcome of Old Style Comparison

If ““This is the Document.doc”” == “This is the Document.doc”

New Style Comparison

If “%~1” == “This is the Document.doc”

Outcome of New Style Comparison

If “This is the Document.doc” == “This is the Document.doc”

Table 1 illustrates the behavior of old style comparisons, done without the benefit of command extensions, and the new style, which leverages them to make the test work as you would expect.

Conclusion

While graphical interfaces and tools are nice, and they have a valuable place in our tool sets, so does the lowly command line interface. Batch files are fairly easy to write, test, and debug, run fast, are ready to run without a compiler or specialized interpreter, run without fuss on any machine, and can go where a program with a graphical is a waste, such as in logon and logoff scripts and scheduled tasks, none of which has a visible user interface.

While I do my share of graphical programming, batch files remain an essential part of my production environment, and occasionally become part of packages that I deliver to clients.

Use the following resources to learn more about command extensions, and start building flexible, powerful, modern batch files.

References

  1. http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/if.mspx?mfr=true is the official documentation of the modern IF command.
  2. http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true is nominally about the new command processor, CMD.EXE, but it includes basic information about command extensions, including several ways to enable and disable them.
  3. http://www.robvanderwoude.com/local.php is a discussion of the SETLOCAL and ENDLOCAL commands, which includes one of several nifty tests that you can use to verify that command extensions are enabled.
  4. http://technet.microsoft.com/en-us/library/bb490920.aspx lists and documents the full set of relational operators that become available with command extensions enabled.

Saturday, January 16, 2010

Formatting Windoes Explorer Detail Columns to Fit Contents

If you use Microsoft Excel, you may have discovered a shortcut that is undocumented, so far as I know, that allows you to auto-fit a single worksheet column to fit its contents by double-clicking on the divider between the column and its right hand neighbor.

Today, I discovered that, not too surprisingly, the same shortcut works in the Windows Explorer. Since I work in the Detail view most of the time, I am always adjusting the widths of one or another of its columns, depending on the information that is currently of most interest. Just for fun, I decided to test the double-click. Sure enough, the width adjusted, exactly as it would in Microsoft Excel!

In both cases, you must double-click on the border at the top of the window. In Excel, this is the part where you see the column labels, A, B. C ... etc.

Likewise, in the Windows Explorer, you must double-click on the area that contains its column labels, File, Size, Type, Date Modified, ... etc.

This new knowledge will save me lots of time!

Friday, January 8, 2010

To Copy A Directory Tree, Without Copying the Files

For the first time in a long while, I just needed to duplicate the structure of a directory tree, without copying the files.

The Problem

Duplicate the structure of the directories that hold my program source code, so that I can move the archives of completed projects out of the active development directories, which are backed up nightly to an offsite location, while keeping them organized as they currently are in the development directories.

The Solution

I began by starting a conventional copy, using the Windows Explorer. As it got underway, it occurred to me that there had to be a more efficient way. A quick Google search yielded instant results.

The search term was straightforward, "copy directory structure only."

The command that got the job done, in less than a minute, is equally straightforward, as shown in Listing 1, which I copied and pasted from my command prompt window.

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
 
C:\Common_Data\WinZip_archives\code_libs>XCOPY "C:\Documents and Settings\David\
My Documents\Programming" /T /E
 
C:\Common_Data\WinZip_archives\code_libs>
 
Listing 1 - The command above, XCOPY "C:\Documents and Settings\David\My Documents\Programming" /T /E, leverages the sparsely documented T switch of the faithful XCOPY command, which has been a stalwart friend since at least 1989.

More Shortcuts

OK, so I cheated.

  • I used a shell extension to open my command prompt window with the target directory already made current.
  • I used the Windows Clipboard to get the name of the source directory right the first time.

This second shortcut is especially noteworthy, because it calls attention to another sparsely documented feature of Windows, which is that pretty much anything that you can highlight can be copied, using the CTRL-C keyboard shortcut.

Among other things, this means that you can copy from any of the following locations.

  • The address bar of a Windows Explorer window.
  • The file name text box of a file property sheet.
  • Any other text box.
  • Some other text on property sheets. Among others, I've had success with the following.
    • File create, modify, and access dates on file property sheets.
    • File sizes, also on file property sheets.
    • Version strings, from the Version page of the property sheet for an EXE or DLL file.

If you need text from almost anywhere in a Microsoft Windows application, try to highlight it. If it's text on the flat surface of a dialog box, try dragging the mouse across it. If you can highlight it, a quick CTRL-C  puts it into the clipboard.

For Geeks Only - Why This Works

Although it isn't obvious, selectable text lives in a Text Box control. It isn't obvious that it's a control, because of the way its properties were set at design time.

  • Border is set to None.
  • Background is set to Transparent, or its color is set to be the same as that of the dialog box surface.
  • 3D effects are off.

How these settings are applied depends on the development environment, but all can be set in any environment that lets you build custom dialog boxes and other types of forms. Among others, this includes VBA (hosted in Microsoft Access, Word, Excel, PowerPoint, and Outlook, among others), Visual Basic (classic versions 1 through 6), VB.NET, and C#. This also includes the WinBatch dialog editor, and probably numerous others.