0

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?

splattne
  • 28,508
  • 20
  • 98
  • 148
Mohammad AL-Rawabdeh
  • 1,612
  • 12
  • 33
  • 54

2 Answers2

1

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

Joe Internet
  • 1,449
  • 8
  • 6
1

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

David Spillett
  • 22,754
  • 45
  • 67
  • 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 09 '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 09 '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 09 '11 at 16:12