Technology in terms you understand. Sign up for the Confident Computing newsletter for weekly solutions to make your life easier. Click here and get The Ask Leo! Guide to Staying Safe on the Internet — FREE Edition as my thank you for subscribing!

Do you have any tips for writing batch files in Windows?

Question:

I’m very interested in writing my own batch files, some scripts for Windows
since I have learned to do this in Linux. But I think Windows acts differently
than Linux when running scripts in the command prompt. Do you know any tips
about writing batch files and scripts in Windows command prompt that you can
share? Or know of any good documentation that I can look for?

Having started in Linux, you’ve got a good foundation – many of the concepts
that you might have learned writing scripts in Linux actually apply to Windows
batch files as well. The devil, as they say, is in the details and the
different syntax of the various commands.

In this segment from an Ask
Leo! webinar
, I’ll show a couple of places to get information about what
Windows batch files can do.

]]>

View in HD (1280×720)

Transcript

I’m very interested in writing my own batch files, some scripts for Windows since I have learned to do this in Linux. But I think Windows acts differently than Linux when running scripts in the command prompt. Do you know any tips about writing batch files and scripts in Windows command prompt that you can share? Or know of any good documentation that I can look for?

Let me think about that for a second. I’ve certainly had my share of batch files in both Windows and Linux. Firing up the command prompt here. One of the things I’m kinda weighing it here.

One place to start is with the Help command. You can see all of the mostly command line tools and commands that you can use from within your batch file. Yea, and it precludes some of the actual batch file command line language. For example, I was looking for FOR here. FOR actually isn’t a standalone program. You won’t find FOR.exe on your machine.

FOR is a command that is understood by the command prompt. CMD.exe and does things so that for any of these commands, the same thing with if here by the way. For any of these commands, these are the commands that you would use in a command prompt and a batch file. .bat or .cmd file will be a Windows command batch file. For any of them, you can type Help and the command and it will give you a bit of a background on how the command works and what the syntax and so forth. In some ways, I want to say it’s similar to the man command that you may be familiar with in Unix or in Linux, but it will give you a start on ways to do that.

Now, I’m a try-it-and-see-what-happens kind of guy and that’s kind of sort of my approach. I’m sure that there are some great books out there on writing Windows batch files; writing DOS batch files. I wouldn’t know what they are. I learned all the hard way just by trial and error mostly. So, I’ve done a fair amount of it, but that’s probably the best thing I can lead you; to write that would at least point you in a direction that may be helpful. I hope it is.

Do this

Subscribe to Confident Computing! Less frustration and more confidence, solutions, answers, and tips in your inbox every week.

I'll see you there!

10 comments on “Do you have any tips for writing batch files in Windows?”

  1. It’s out of print now but you might look for a used copy of my Ultimate Batch File book. It was written for Windows 95. New commands have been added but all the ones mentioned in the book are still there.

    Reply
  2. The Windows command processor understands a few commands (Leo mentioned FOR) but much more capable scripts can be written using the standard scripting hosts, wscript.exe and cscript.exe. Code written in vbScript which is a scripting version of visual basic. Another popular and very powerful scripting language (freely available) is Python. Both languages require nothing more than Notepad (or your text editor of choice). Technical questions on both are generally answered quickly (and for free) at the technology site, daniweb. pytony on the Python forum is extremely knowledgeable in all things Python and Reverend Jim on the vb.net forum has many years experience writing vbScript.

    Reply
  3. I had a very good batch file book for DOS. When I went to Windows I assumed it was a thing of the past and threw it out. I’ve wished for it many times since. I liked batch files a lot better than Windows script files because they weren’t nearly as complex.

    Reply
  4. Leo – thanks much for the batch file info. I also do a lot using VBScript and .vbs files. Seems to me at least to be much more powerful than Batch and .bat files. And there’s all kind of help out there for vbs on the internet. I’ve never not been able to figure out how to do something using VBScript. Keep up the outstanding work!

    Reply
  5. All the above information is useful, but if someone is proficient in Linux script processing I think they will be frustrated with the limitations of Windows batch files, even supplemented with VBScript. You will need to do a lot of VBScript to emulate what can easily be produced in a simple grep | sed, or awk script. Therefore I would suggest trying a Unix command processor for Windows, e.g. cygwin. This will replicate the Linux environment on Windows and your previous Linux scripts will probably run in cygwin without modification. But if the scripts are to be used by others who may not have cygwin then it will be necessary to get to grips with Dos and VBScript.

    Reply
  6. I have used both batch files and VBA and got very frustrated with the syntax. For several years I used Macro Express, particularly for keyboard shortcuts, but that too is a typically “mature” software product, i.e. one that has been extended way beyond its original purpose, and become baroque.

    Then I discovered AutoHotkey (a freeware fork from AutoIt) which I now use for anything that can be done with a script. It still has its little quirks, but I use it for all sorts of purposes, such as mounting and dismounting TrueCrypt volumes, checking whether files in Dropbox are up to date or syncing, minimising the risk of conflicting files in Dropbox, and sorting them out if they out if they do occur.

    By the way, I like Python and have used it, but I wouldn’t know how to do a lot of the stuff I do in AHK.

    Reply
  7. Other tools which I’ve found very useful for command line scripts are the command processors available from JP Software (www.jpsoft.com). They aren’t free, but they greatly extend and are upward compatible with the Windows command line syntax. One especially valuable extension that I’ve used quite heavily is the ability to “poke” keystrokes into a Windows application. Using this allows one to automate the transfer of data from one application to another, run applications dependent on conditions encountered, etc.

    Reply
  8. I suggest Windows PowerShell rather than old-style batch commands. It’ s really where Microsoft is putting its emphasis for command-line scripting, it seems to me. :)

    Reply

Leave a reply:

Before commenting please:

  • Read the article.
  • Comment on the article.
  • No personal information.
  • No spam.

Comments violating those rules will be removed. Comments that don't add value will be removed, including off-topic or content-free comments, or comments that look even a little bit like spam. All comments containing links and certain keywords will be moderated before publication.

I want comments to be valuable for everyone, including those who come later and take the time to read.