havlock havlock - 1 year ago 59
Python Question

In what circumstances are batch files the right approach?

I've inherited various task with moderately confusing batch files. I intend to rewrite them as Python scripts, if only so I can thoroughly see what they're doing, as can my successors.

I honestly don't see why anyone would have more than a startling simple (under five line, do a, b, c, d, e in order) batch file: proper logic deserves a more modern approach, surely? But am I missing something? Is there a real advantage in still using such an approach? If so, what is it?

Clarified in response to comments: Windows .bat fils, that check for existence of files in certain places and move them around and then invoke other programs based on what was found. I guess what I'm really asking is, is it normal good practice to still create batch files for this sort of thing, or is it more usual to follow a different approach?

Answer Source

I'm pretty sure that if I were still doing sysadmin on Windoze systems I would be replacing all but the simplest of BAT files with something -- anything! -- else. Because that particular command language is so awfully deficient in just about everything you need. (Exception handling? Block structured code? Procedures? Parsing text strings and filenames? )

Python is highly readable and "batteries included", so it has to be worthy of consideration as a Windows scripting language. However, Powershell may offer advantages with respect to interfacing to Windows components. It was designed with that in mind. With Python you'd be relying on extra modules which may or may not do all that you need, and might not be as well-supported as Python and its standard libraries. On the other hand, if it's currently done with BAT it's probably not that sophisticated!

The main argument against change is "if it ain't broke, don't fix it". So if the BAT scripts do all you need and no changes are needed, then don't make any. Not yet. (Sooner later a change will be needed, and that is the time to consider scrapping the BAT script and starting again).