Using the Shell object to perform shell tasks
Run applications
'Create the shell object
Set WshShell = CreateObject("WScript.Shell")
'Run notepad
WshShell.Run "notepad"
'Run notepad with parameters
WshShell.Run "notepad c:\mydoc.txt"
'Run notepad maximized
WshShell.Run "notepad c:\mydoc.txt", 3
'Run notepad and wait for the process to
end
WshShell.Run "notepad c:\mydoc.txt", 3, true
Read/write text files
'Declare constants
Const ForReading = 1, ForWriting = 2, ForAppending = 8
'Declare the file system object
Dim fso, f, Msg
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a file and write some text
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
'Open the same file for reading
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
ReadTextFileTest = f.Read(5)
//Open, read a text file line by line and
then properly close it (Javascript)
var fso;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.OpenTextFile("inputfile.txt", 1);
while (!f.AtEndOfStream)
{
inputString = f.ReadLine( );
//...
}
f.Close( );
Create shortcuts
Set WshShell =
CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
msgbox strDesktop
msgbox ScriptFullName
Set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
oShellLink.TargetPath = "notepad.exe"
oShellLink.WindowStyle = 1
oShellLink.Hotkey = "CTRL+SHIFT+F"
oShellLink.IconLocation = "notepad.exe, 0"
oShellLink.Description = "Shortcut Script"
oShellLink.WorkingDirectory = strDesktop
oShellLink.Save
Set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
oUrlLink.TargetPath = "http://www.microsoft.com"
oUrlLink.Save
Access the Windows registry
' Creates the shell object
Dim WshShell, bKey
Set WshShell = CreateObject("WScript.Shell")
' Creates/sets the defaul value of the \Test\ key
WshShell.RegWrite "HKCU\Software\MySoft\Test\", 1, "REG_BINARY"
' Sets a string value
WshShell.RegWrite "HKCU\Software\MySoft\Test\value1", "this is a test value",
"REG_SZ"
' Reads the previously set values
bKey = WshShell.RegRead("HKCU\Software\MySoft\Test\")
msgbox WshShell.RegRead("HKCU\Software\MySoft\Test\value1")
' Removes them
WshShell.RegDelete "HKCU\Software\MySoft\Test\value1"
WshShell.RegDelete "HKCU\Software\MySoft\Test\"
WshShell.RegDelete "HKCU\Software\MySoft\"
Specs:
| Root key Name | Abbreviation |
|---|---|
| HKEY_CURRENT_USER | HKCU |
| HKEY_LOCAL_MACHINE | HKLM |
| HKEY_CLASSES_ROOT | HKCR |
| HKEY_USERS | HKEY_USERS |
| HKEY_CURRENT_CONFIG | HKEY_CURRENT_CONFIG |
| Type | Description | In the Form of |
|---|---|---|
| REG_SZ | A string | A string |
| REG_DWORD | A number | An integer |
| REG_BINARY | A binary value | A VBArray of integers |
| REG_EXPAND_SZ | An expandable string (e.g., "%windir%\\calc.exe") |
A string |
| REG_MULTI_SZ | An array of strings | A VBArray of strings |
References
Full docs here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsMthRun.asp
Full docs on the file system object:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/FSOoriFileSystemObject.asp