Bat File Coding Standard

[A proposed coding standard for Microsoft MsDos/Windows batch files. (Similar to UnixShell scripts.)]

It's really frustrating to read a BAT file. Usually it's not indented, capitalization varies etc. The following is an attempt to formalize some standards for BAT files.

   @echo off
   rem ***********************************************************************
   rem This bat file makes the JAR library files out of the source files.
   rem This expects the following format
   rem call LibMaker -name <jarName> -root <-pkgRoot> *[subPackages]
   rem -name            Name of the JAR file including the extension
   rem -root            The package root
   rem *subPackages     Any number of sub-packages that you need to compile 
   rem                  into this JAR.
   rem ***********************************************************************

E.g.
   :init
        call SetupEnv

if not "%1"=="-name" goto noPkgName shift set _PKG_NAME=%1 shift

if not "%1"=="-root" goto noRootPkg shift set _PKG_ROOT=%1 shift

:cleanUp set _PKG_ROOT=

Please add to the standards more useful ones. -- VhIndukumar.


These generally seem like good ideas. But "All batch file names should be nouns"? Don't you want your batch files to do something? Given the example header, doesn't this batch file "MakeLibrary"? -- JeffGrigg

That header is fundamentally broken - if you echo that information out (standardizing the switch used), you get all of the benefits of keeping that information in the source file and the benefits of making it available to the users. -- DanilSuits


How safe is it to use the cmd extensions that you get in NT or 2K? In particular, setlocal/endlocal does what SetUpEnv.bat and CleanupEnv.bat would do. There's is an excellent book on cmd scripts that I go and find the title of.

I've used the extensions extensively and had no problems, the ability to use do subroutines is very handy as well as the extensions to the set command.

Is the book Windows NT Shell Scripting, I had it once, but lent it to someone, it is excellent


I think it's a good idea to standardize the switches. Probably the following will be useful. The code can check if the user has not given any input or has typed "/?"

   :help
       echo call LibMaker -name <jarName> -root <-pkgRoot> *[subPackages]
       echo -name            Name of the JAR file including the extension
       echo -root            The package root
       echo *subPackages     Any number of sub-packages that you need to compile 
       echo                  into this JAR.
-- VhIndukumar


"@echo off" is an AntiPattern, because the script cannot restore the echoing to its previous state, and because it can accidentally be toggled by echoing nothing. -- JasperPaulsen


See DosPatterns

--- several undocumented, though not uncommon things are case sensitive in Windows Command scripts. ss64 dot com has several examples, one of which is when using the FOR construct: "Format letters are case sensitive in Windows 2000 and above, so using a capital letter is also a good way to avoid conflicts %%A rather than %%a"


EditText of this page (last edited November 23, 2014) or FindPage with title or text search