What this batch file needs is more escape characters


(Employing the snowclone "What this X needs is more Y.")

Each time you add a parsing pass to the batch processor, you have to add another layer of escaping. This is just a special case of the more general rule of thumb: any problem in quoting can be solved by adding another layer of escaping.

(Okay, it's not actually true, nor is it a rule of thumb, but it's still something to keep in mind.)

When you enable delayed variable expansion, you add another parsing pass to the batch processor. It used to expand % variables at the time the line is ready, but now you told it that, oh wait, just before executing the command, expand it a second time (this time looking for ! variables.)

Which means that if you want to echo an exclamation point, you have to protect the exclamation point so the parser won't treat it as a delayed expansion.

echo Error^^!

The ^^ collapses to a ^ during the first parsing pass. On the second parsing pass, the ^! turns into a !

Remember, the batch language was not designed; it evolved. I admire the approach taken by commenter Nick, in a tip of the hat to Douglas Adams:

Much like the universe, if anyone ever does fully come to understand Batch then the language will instantly be replaced by an infinitely weirder and more complex version of itself. This has obviously happened at least once before ;)

Comments (16)
  1. Is there a reason why PowerShell is only included in Windows Server 2008 but not any other versions of Windows?  I would imagine if it were, it would people (slowly) migrate away from batch scripts.

  2. Josh says:

    Powershell is available as a download for XP SP2 and up.  Heck, I think it shows up in Windows Update (as an optional update) for some of them.  What more do you want?  If MS marked it as a critical update to get full deployment, people would complain that MS was "taking over their machines" or otherwise imposing itself on them.

  3. someone else says:

    PowerShell is also part of Windows 7. But it’s not, after all, something you’d want to bother granny with.

    And it needs more cowbell.

  4. alexx says:

    Before I knew that trick, This is what I use:

    (ECHO DONE!) | FIND "D"

    Parentheses are required, I don’t know why..

  5. Diego says:

    Strange, running Vista, it appears that a batch file containing:

    echo Error!

    …in fact does print:

    Error!

    So, what gives?

    [You skipped ahead to the punch line without reading the set-up. And then the phrases “delayed expansion” and “second parsing pass” make no sense because you didn’t read paragraph 4. -Raymond]
  6. porter says:

    If you want to rip your hair out over expansion, attempt to setup a scheduled task using the command line, where the task

    (a) is in a directory with spaces in the path

    (b) takes command line arguments

  7. acq says:

    For all readers, including me, who didn’t know what exactly "delayed environment variable expansion" is supposed to be for:

    http://technet.microsoft.com/en-us/library/bb490880.aspx

    "Enabling delayed environment variable expansion: If you enable delayed environment variable expansion, you can use the exclamation character to substitute the value of an environment variable at run time."

  8. acq says:

    There is also a big text about "delayed environment variable expansion" written by Raymond three years ago:

    "Environment variable expansion occurs when the command is read"

    http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx

  9. will says:

    Powershell 2.0 for everything from XP was released a few days ago

    http://go.microsoft.com/fwlink/?LinkID=151321

  10. Gabe says:

    The problem with Powershell is that now you have to install Powershell. And before you can install Powershell, you have to install the .Net framework. That means you can’t ship a simple Powershell script with your app unless you want the user to possibly have to download hundreds of MB of almost-unnecessary utilities. It also means that you can’t do casual scripting on arbitrary (i.e. other people’s) computers with it.

    I frequently remote into other people’s computers to do work, but I’ve never seen one that had Powershell installed, so I am stuck doing all my scripting in batch files.

  11. Daev says:

    Anyone who wants to do serious, principled, and robust batch file scripting should keep Tim Hill’s "Windows NT Shell Scripting" close at hand, although (as the title implies) it doesn’t cover newer features like delayed variable expansion.  I occasionally have to write complex batch files for colleagues and have found the book indispensible — what’s more, it actually gives me a twisted appreciation for the language.  Like watching a dancing bear, the joy of writing .bat scripts comes from the mind-boggling fact that you’ve made it perform at all.

  12. John says:

    What’s wrong with using VBScript?  It is essentially universally available on Windows, and much easier to write (and read, for that matter) than batch files.

  13. Nick says:

    I love how Raymond’s blog is a kind of time capsule.  You can post something a year ago, or two or three years ago, and then long after it’s been completely forgotten, bam! there it is again :)

    John:

    I agree.  I regularly find myself drawn towards Windows Scripting Host (whether via VBScript or JScript) when I need to automate or script some function in Windows that isn’t mind-numbingly trivial to do in Batch.  When combined with WMI and other COM objects (especially ADSI and IE), it is surprisingly powerful and capable. I’ve got a "Scripts" folder on my work machine with about 30 somewhat complex scripts (i.e., more than 40 or so lines) I’ve written and anticipate using again.

    I keep meaning to look into PowerShell more as it’s becoming more and more ubiquitous on Windows — Win7, and Server 2008 both have it by default.  The .NET framework is getting hard to find not installed on computers as more and more software is written with it, and having direct access to that whole framework from a script is pretty awesome. Overall PS will be a good thing to have.

  14. Mark (The other Mark) says:

    Indeed. VBScript is the tool of choice for any desktop management function in any reasonably large environment I’ve been in. Everyone who performs these functions talks about PowerShell, ranging from wanting to learn it to they are actively doing things with it on a small scale, but it’s not the method used for enterprise deployments yet.

    Give it a few years- it’s an excellent tool, but too few machines can run it, and it’s difficult to justify the business case for deploying PowerShell to the Enterprise.

  15. porter says:

    > VBScript is the tool of choice

    Tool of last resort perhaps? Billy G is having his revenge, BASIC *will* survive, it *will* survive….

  16. DWalker says:

    What this X needs is more Y.

    X = song, Y = cowbell…

Comments are closed.