 |
Search
IT Courses |
|
|
|
|
|
|
|
|
|
|
|
IT
dart News Letter
|
Get
ITdart.com weekly headlines before it's published on our site! Subscribe
and receive the articles delivered to your inbox!
|
|
|
Batch files using VB Script
Windows Scripting Host is a new component available with Windows 98, Windows NT SP4 and is also downloadable from the Microsoft site. This allows you to write very powerful batch files using VBScript .VBS files.
Along with this functionality, a new object is made available for programming. The base object is called Wscript.wshshell. This object provides extensive functionality required for managing shell, network, registry management functions. Here is a sample that can be used to create a desktop icon using Windows Shell Script.
' This sample demonstrates how to use the WSHShell ' object to create a shortcut on the desktop. Dim WSHShell Set WSHShell = _ WScript.CreateObject("WScript.Shell") Dim MyShortcut, MyDesktop, DesktopPath ' Read desktop path using WshSpecialFolders object DesktopPath = WSHShell.SpecialFolders("Desktop") ' Create a shortcut object on the desktop Set MyShortcut = _ WSHShell.CreateShortcut(DesktopPath & _ "\Shortcut to notepad.lnk") ' Set shortcut object properties and save it MyShortcut.TargetPath = _ WSHShell.ExpandEnvironmentStrings _ ("%windir%\notepad.exe") MyShortcut.WorkingDirectory = _ WSHShell.ExpandEnvironmentStrings("%windir%") MyShortcut.WindowStyle = 4 MyShortcut.IconLocation = _ WSHShell.ExpandEnvironmentStrings _ ("%windir%\notepad.exe, 0") MyShortcut.Save Here are some important methods of this wshshell object.
Method Description CreateShortcut Creates a WshShortcut object and returns it ExpandEnvironmentStrings Expands a PROCESS environment variable and returns the result string Popup Pops up a message box window containing a specified message RegDelete Deletes, from the registry, a specified key or value RegRead Returns a specified key or value from the registry RegWrite Sets a specified key or value in the registry Run Creates a new process that executes a specified command with a specified window style Here are the details of the WshShortcut object.
Property Description
Arguments Parameters to a shortcut object Description A description of a shortcut object Hotkey The hotkey of a shortcut object IconLocation The icon location of a shortcut object TargetPath The target path of a shortcut object WindowStyle The window style of a shortcut object WorkingDirectory The working directory of a shortcut object Save is the only method for this object.
The .VBS files can be simply run as Batch files.
The WScript object has the following methods:
Method Description CreateObject Creates an object and establishes its event handling DisconnectObject Disconnects a previously connected object from Windows Scripting Host Echo Displays parameters in window or at a command prompt in a command prompt window GetObject Retrieves an Automation object from a file Quit Quits execution with a specified error code The script can accept command line arguments like the batch files. The Arguments property of WScript exposes these parameters.
This code will display all parameters passed on a command line:
' Display all command-line parameters Set objArgs = WScript.Arguments For I = 0 to objArgs.Count - 1 WScript.Echo objArgs(I)
|
|
|