Thursday, March 25, 2010

vbscript

username

Set WshNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "Username :" & WshNetwork.UserName

concatenation

use & to concatenate strings, for example,

FilePath = strPath & WshNetwork.UserName & ".V2\ntuser.dat"

check for existence of a file

Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(FilePath)) Then
WScript.Echo FilePath & " exists"
End If


check for a value in a registry key
Unlike other programming languages, you need to read the "Err". I wonder why MS don't implement return value. funny.

strValueName = "HKCU\abc\def"

Set WshShell = WScript.CreateObject("WScript.Shell")
On Error Resume Next

WshShell.RegRead(strValueName)

If Err = -2147024894 Then
'The key "HKCU\abc\def" does not exist
End If

If Err = 0 Then
'The key exist
End If

Executing a command
Set WshShell = CreateObject("WScript.Shell")
'encrypt Z: drive using EFS
Set oExec = WshShell.Exec("cipher /e /s:z:")