Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to write a batch file and we want to use if-then-else and for loop statements, but I don't know it and its option.

Please can anyone help me?

share|improve this question
You can do help if and help for at a Windows command prompt to see the syntax and some examples. – Dennis Williamson Mar 9 '11 at 14:56
up vote 1 down vote accepted

Microsoft usually has good information on their website. Here's info on batch files.

share|improve this answer

If you are looking to do anything relatively complex, you might be better of using something more modern than batch files.

Since windows 2000 MS have provided the "Windows Scripting Host" which lets you script tasks using either VBScript (preferable if you have experience with VB6 or "Classic" ASP) or JScript (Microsoft's Javascript implementation). See MS's documentation for reference information, and there are many online tutorials. Of course some simple tasks that batch files are used for are more work in WSH (copying and moving files using the FileSystemObject takes a few more lines of code than simply "copy x y" in a batch file, for instance), so a batch file may be the best tool for the job in hand even though the flow control options are very clunky.

There is also PowerShell, available for XP/2003 and above, which also has a far more expressive scripting featureset (though I've not got around to playing with PS myself, so I can't comment on how it compares to other options).

If you are familiar with unix-a-like systems you can transfer some of that knowledge by having something like bash installed (I have cygwin installed on all by Windows boxes, and use bash and all the other common tools like rsync and the GNU text processing utilities through cygwin's ports).

share|improve this answer
I'm all for using GNU tools everywhere, but why not just use python or perl - they are crossplatform and the scripts can be run on any supported platform – dyasny Mar 9 '11 at 14:58
@dyasny: From a unix-a-like point of view it is unusual to not find bash or something very similar on any install, and while perl and python are also commonly present they are not as ubiquitous. From a Windows PoV you are right, this makes little difference as none of them are overly likely to be present by default so it comes down to preference/experience levels. – David Spillett Mar 9 '11 at 16:09
even though you're right on general terms, bash is pretty limited as a scripting language. especially compared to monsters like python or powershell (yes, it is quite a beast actually). Generally, I opt out for python because I can run it corssplatform, ad because it doesn't limit me like bash or simple cmd would – dyasny Mar 9 '11 at 16:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.