views:

900

answers:

6

What are the best free resources for learning advanced batch-file usage?

+1  A: 

I've found this one to be useful: http://www.ss64.com/ntsyntax/

Landon
+9  A: 

It's not free, but it's probably the best. "Windows NT Shell Scripting" by Tim Hill.

That said, whenever I try to do something 'advanced' in cmd batch files, I always end up regretting it.

Always.

edit: some explanation of the shortcomings of batch files:

  • math capabilities are pathetic
  • quoting support is an afterthought - if you need to pass a quoted string as an argument to a command that needs to be quoted itself, reserve a spot at the asylum (actually, I'm not sure it's possible)
  • string manipulation is a patchwork of half implemented functionality

Then there are the seemingly never-ending bits of oddities, corner cases, and inconsistencies that you run into at every turn.

The only thing going for batch files is that they're supported on every Windows box out there. If you just want to automate executing a few commands as a group, great. Maybe add a simple loop, a couple of subroutines, and some environment variables to parameterize things. Beyond that I strongly recommend you use something else.

Michael Burr
Do you suggest using a scripting language instead?
Brian R. Bondy
Agreed, I'd go with Python.
Unkwntech
Over the last few years I've not had an itch for this, so I have been able to stick with basic batch files. If I were going to try something fancy, I'd probably look at PowerShell. I don't have experience with it, but have heard good things about it.
Michael Burr
Agreed... AdvancedCmdBatchFiles.GetPainLevel() > UsingWindowsME.GetPainlevel()
James Schek
I was trying to automate our build process using batch files. I did get it to work, but I was much happier when I made the switch to Python.Python is easy to learn and use, but it also has all of the power behind it you may find yourself needing. (even Gentoo's build system is built on Python!)
Miquella
+2  A: 

Stack Overflow maybe?

http://stackoverflow.com/questions/tagged/batch-file

What I found to be really helpful was to save all of the built-in cmd.exe help to files. In cmd.exe type "help", that will show you a big list of commands you can get help on, for each of those type "help <command>", then save the results to appropriately named files. Personally I find it a lot more convenient to read the help info in file form.

Also, I very much agree with Mike B's proscription against using batch files too much. It's almost always better to use a more robust language than batch for any task that isn't trivial. If your batch file is more than a page long when printed, you will almost certainly live to regret it.

Wedge
+2  A: 

Also consider learning Windows Scripting Host as an alternative to batch scripting. You can use your choice of languages to write scripts (JScript, VBScript, even Python). It is supported by every modern version of Windows and provides a feature-rich library of functions that makes batch files look primitive in comparison.

What am I saying? Windows Batch files ARE primitive.

You can also create re-usable WSH libraries, modules, etc. There was a bit of a learning curve compared to batch files, but well worth it. No longer do my Linux friends make fun of my inferior scripting environment. Now they just make fun of my inferior OS.

James Schek
A: 

To get help on commands, this .bat file will put all commands' help into a nice HTML page: http://www.robvanderwoude.com/files/allhelp.zip

This guy has some other interesting .bat things as well. http://www.robvanderwoude.com/batexamples_a.html

Denes Tarjan