|
||||||
Choosing a Windows Scripting LanguageA Quick Comparison of MS-DOS, VBScript and PowerScriptThe Windows script developer can choose between MS-DOS (or CMD), VBScript and now PowerShell. There are pros and cons for each of them, so which is best?
When it comes to Windows scripting a programmer has a few languages that they can choose from. One has been around for as long as Windows itself, one is virtually brand new. Two will be very familiar to most programmers:
But one may be new to many people:
It really comes down to what languages the programmer is used to and what it is that they’re wanting to do. The Microsoft Command PromptThe Microsoft command prompt comes in slightly different guises according the version of Windows installed on the user`s computer. For example it may be:
Many simple tasks can be carried out with the command prompt such as:
It even allows simple "if" statements to be used, for example: D = C:\Windows
if exist D echo D exists
if not exist D" echo D does not exist
And, most importantly, this can all be done by saving the commands to a script file (which requires a ".bat" file name extension). However, the scripting functionality is limited and the programmer will quickly need to turn to other scripting languages in order to carry out their work. Once such language is VBScript. Microsoft VBScriptMicrosoft VBScript (or Visual Basic Script) is (as it’s name suggests) derived from Microsoft Visual Basic. However, it must be emphasized that although VBScript is similar to Visual Basic it is not the same. This is a programming language in its own right. For example it enables if...then...else statements but instead of having specific built in functions, it uses system objects in order to carry out many operations, for example: D = " C:\Windows"
set fs = CreateObject ("Scripting.FileSystemObject")
if fs.FileExists(D) then
msgbox D & " exists"
else
msgbox D & " does not exist"
end if
It's worth noting that VBScript is only a scripting language and is not a command line language. The code has to be saved into a file (normally with a ".vbs" file extension). The user can then run the file by double clicking or, or from the command line using the cscript command, for example: cscript check_dir.vbs
The big advantage that VBScript has is that it can manipulate system objects, for instance it can:
Of course even VBScript is outdated now. PowerShellPowerShell is Microsoft Windows new scripting language and is based on the .NET framework. It combines the ability to carry out command line operations with the ability to create script files (which must always be saved with a ".ps1" file name extension). It also has access to all of the .NET objects installed on a computer. A simple PowerShell script looks like: $D = "C:\Windows"
if (test-path $D) {
write-output "$D exists"
} else {
write-output "$D does not exist"
}
The programmer can store this code to a file (for example c:\powershell\check_directory.ps1) and then run it from the command line: powershell c:\powershell\check_directory
It should be noted that PowerShell (unlike VBScript and CMD) are not installed in Windows XP and Vista, although it is a part of Windows Server (and will be part of Windows 7). Any XP and Vista users will, therefore, need to download and install PowerShell. SummaryThe Microsoft Windows programmer has a number of options when it comes to scripting. They can:
With one of these (or a mixture of them) the programmer has complete control of all of the objects on a Windows computer. Further ReadingAn Introduction to the Windows PowerShell
The copyright of the article Choosing a Windows Scripting Language in Command Line Programming is owned by Mark Alexander Bain. Permission to republish Choosing a Windows Scripting Language in print or online must be granted by the author in writing.
Comments
Sep 29, 2009 7:31 AM
Guest :
1 Comment:
|
||||||
|
|
||||||
|
|
||||||