Old School Wizardry Tip: Batch File Comments

Posted by jkauffman on Geeks with Blogs See other posts from Geeks with Blogs or by jkauffman
Published on Thu, 12 Apr 2012 20:07:23 GMT Indexed on 2012/04/12 23:31 UTC
Read the original article Hit count: 361

Filed under:

Johnny, the Endangered Keyboard-Driven Windows User

Some of my proudest, obscure Windows tricks are losing their relevance. I know I’m not alone.

Keyboard shortcuts are going the way of the dodo. I used to induce fearful awe by slapping Ctrl+Shift+Esc in front of the lowly, pedestrian Windows users. No windows key on the keyboard? No problem: Ctrl+Esc. No menu key on the keyboard: Shift+F10. I am also firmly planted in the habit of closing windows with the Alt+Space menu (Alt+Space, C); and I harbor a brooding, slow=growing list of programs that fail to support this correctly (that means you, Paint.NET).

Every time a new version of windows comes out, the support for some of these minor time-saving habits get pared out. Will I complain publicly? Nope, I know my old ways should be axed to conserve precious design energy. In fact, I disapprove of fierce un-intuitiveness for the sake of alleged productivity. Like vim, for example. If you approach a program after being away for 5 years, having to recall encyclopedic knowledge is a flaw. The RTFM disciples have lost.

Anyway, some of the items in my arsenal of goofy time-saving tricks are still relevant today. I wanted to draw attention to one that’s stood the test of time.

Remember Batch Files?

Yes, it’s true, batch files are fading faster than the world of print. But they're not dead yet.

I still run into some situations where I opt to use batch files. They are still relevant for build processes, or just various development workflow tools. Sure, there’s powershell, but there’s that stupid Set-ExecutionPolicy speed bump standing in your way; can you really spare the time to A) hunt down that setting on all machines affected and/or B) make futile efforts to convince your coworkers/boss that the hassle was worth it?

When possible, I prefer the batch file wild card. And whenever I return to batch files, I end up researching some of the unintuitive aspects such as parameters, quote handling, and ERRORLEVEL. But I never have to remember to use “REM” for comment lines, because there’s a cleaner way to do them!

Double Colon For Eye-Friendly Comments

Here is a very simple batch file, with pretty much minimal content:

@ECHO OFF

SETLOCAL

REM This is a comment

ECHO This batch file doesn’t do much

If you code on a daily basis, this may be more suitable to your eyes:

@ECHO OFF

SETLOCAL

:: This is a comment

ECHO This batch file doesn’t do much

Works great! I imagine I find it preferable due to the similarity to comments in other situations: // or ;  or #

I’ve often make visual pseudo-line breaks in my code, and this colon-based syntax works wonders:

@ECHO OFF

SETLOCAL

:: Do stuff

ECHO Doing Stuff

::::::::::::::::::::::::::::

:: Do more stuff

ECHO This batch file doesn’t do much

Not only is it more readable, but there’s a slight performance benefit. The batch file engine sees this as an invalid line label and immediately reads the following line. Use that fact to your advantage if this trick leads you into heated nerd debate.

Two Pitfalls to Avoid

Be aware of that there are a couple situations where this hack will fail you. It most likely won’t be a problem unless you’re getting really sophisticated with your batch files.

Pitfall #1: Inline comments

@ECHO OFF

SETLOCAL

IF EXIST C:\SomeFile.txt GOTO END ::This will fail

:END

Unfortunately, this fails. You can only have whitespace to the left of your comments.

Pitfall #2: Code Blocks

@ECHO OFF

SETLOCAL

IF EXIST C:\SomeFile.txt (

        :: This will fail

        ECHO HELLO

)

Code blocks, such as if statements and for loops, cannot contain these comments. This is ultimately due to the fact that entire code blocks are processed as a single line.

I originally learned this from Rob van der Woude’s site. He goes into more depth about the behavior of the pitfalls as well, if you are interested in further details.

I hope this trick earns you serious geek rep!

© Geeks with Blogs or respective owner