Dave's Tech Blog
VBScript
VBScript – Find and replace text in text file
Jul 29th
VBS has a great function - replace. Next VBS script lets you replace string in a text file. Replace works with text compare (case insensitive). Use:
[cscript|wscript] replace.vbs Find Replacewith File
Find … Required. Substring being searched for.
Replacewith … Required. Replacement substring.
File … Source and destination file for the replacement
Dim FileName, Find, ReplaceWith, FileContents, dFileContents Find = WScript.Arguments(0) ReplaceWith = WScript.Arguments(1) FileName = WScript.Arguments(2) 'Read source text file FileContents = GetFile(FileName) 'replace all string In the source file dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1) 'Compare source And result if dFileContents <> FileContents Then 'write result If different WriteFile FileName, dFileContents Wscript.Echo "Replace done." If Len(ReplaceWith) <> Len(Find) Then 'Can we count n of replacements? Wscript.Echo _ ( (Len(dFileContents) - Len(FileContents)) / (Len(ReplaceWith)-Len(Find)) ) & _ " replacements." End If Else Wscript.Echo "Searched string Not In the source file" End If 'Read text file function GetFile(FileName) If FileName<>"" Then Dim FS, FileStream Set FS = CreateObject("Scripting.FileSystemObject") on error resume Next Set FileStream = FS.OpenTextFile(FileName) GetFile = FileStream.ReadAll End If End Function 'Write string As a text file. function WriteFile(FileName, Contents) Dim OutStream, FS on error resume Next Set FS = CreateObject("Scripting.FileSystemObject") Set OutStream = FS.OpenTextFile(FileName, 2, True) OutStream.Write Contents End Function
VBScript – Disconnecting Network Drive and Reconnecting with User Name in the Path of the Network Drive.
Jan 7th
The below VBScript will get the current user that is logged into the computer, then disconnect and reconnect a failed network drive. In this case the user name is a part of the path to the user’s personal drive, \\spksan\users\davidh\ for example. It then displays a pop up stating that it’s done.
On Error Resume Next
userName = “”
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2″)
Set colItems = objWMIService.ExecQuery(“Select * from Win32_ComputerSystem”,,48)
For Each objItem in colItems
userName = objItem.UserName
If InStr(objItem.UserName, “\”) Then
userNameArray = Split(objItem.UserName, “\”)
userNameCounter = 0
For Each userNames In userNameArray
If userNameCounter = 1 Then
userName = userNames
End If
userNameCounter = userNameCounter + 1
Next
End If
Next
Set objItem = Nothing: Set colItems = Nothing: Set objWMIService = Nothing
If Not userName = “” Then
Set objShell = CreateObject(“WScript.Shell”)
Set objNetwork = CreateObject(“WScript.Network”)
objNetwork.MapNetworkDrive “U:”, “\\spksan\users\” & userName
If Err.Number <> 0 Then
objNetwork.RemoveNetworkDrive ”U:”, true
WScript.Sleep 10000
objNetwork.MapNetworkDrive “U:”, “\\spksan\users\” & userName
End If
End If
WScript.Echo “Done fixing U Drive”