Helping people with computers... one answer at a time.

Batch files or scripts are sequences of commands written in text files that run as small programs. Windows has its own scripting language in the form of the Windows Command Prompt commands.

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 (1280x720)

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.

Article C5438 - June 9, 2012 « »

Share this article with your friends:

Share this article on Facebook Tweet this article Email a link to this article
Leo Leo A. Notenboom has been playing with computers since he was required to take a programming class in 1976. An 18 year career as a programmer at Microsoft soon followed. After "retiring" in 2001, Leo started Ask Leo! in 2003 as a place for answers to common computer and technical questions. More about Leo.

Not what you needed?

10 Comments
Ronny Richardson
June 9, 2012 7:07 PM

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.

Roj
June 12, 2012 8:36 AM

Batch file commands: http://ss64.com/nt/
The Command line in windows: http://commandwindows.com/

Frank Camp
June 12, 2012 9:15 AM

Check this link for the very basics:
http://www.computerhope.com/batch.htm

Jim de Graff
June 12, 2012 9:35 AM

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.

Lester
June 12, 2012 9:37 AM

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.

Ken Kopke
June 12, 2012 7:01 PM

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!

Andy
June 13, 2012 12:15 AM

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.

James
June 13, 2012 3:36 AM

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.

nytibcp
June 13, 2012 5:59 AM

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.

José
June 13, 2012 1:38 PM

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. :)

Comments on this entry are closed.

If you have a question, start by using the search box up at the top of the page - there's a very good chance that your question has already been answered on Ask Leo!.

If you don't find your answer, head out to http://askleo.com/ask to ask your question.