[listing 1] Util class
--------------------------
Class Util
Dim m_oFso, m_oShl, m_oNet, m_appLoc
Private Sub Class_Initialize
Set m_oFso = CreateObject("Scripting.FileSystemObject")
Set m_oShl = CreateObject("WScript.Shell")
Set m_oNet = CreateObject("WScript.Network")
m_appLoc = m_oFso.GetFolder(".")
End Sub
Private Sub Class_Terminate
Set m_oFso = Nothing
Set m_oShl = Nothing
Set m_oNet = Nothing
End Sub
Public Property Get HostName
HostName = m_oNet.ComputerName
End Property
Public Sub CopyFolder(srcDir, tarDir, bOverWrite)
m_oFso.CopyFolder srcDir, tarDir, bOverWrite
End Sub
Public Sub DeleteFolderFiles(folderPath)
Dim oFile, oFolder
If Not m_oFso.FolderExists(folderPath) Then Exit Sub
Set oFolder = m_oFso.GetFolder(folderPath)
For Each oFile In oFolder.Files
DeleteFile oFile
Next
End Sub
Public Sub DeleteFile(strPath)
m_oFso.DeleteFile strPath, True
End Sub
Public Sub CreateFile(strPath)
Dim g: Set g = m_oFso.CreateTextFile(strPath)
g.Close
End Sub
Public Sub RenameFile(strOrig, strNew)
m_oFso.MoveFile strOrig, strNew
End Sub
Public Sub CreateFolder(strPath)
If m_oFso.FolderExists(strPath) Then Exit Sub
Dim oPF: oPF = m_oFso.GetParentFolderName(strPath)
If Not m_oFso.FolderExists(oPF) Then CreateFolder oPF
m_oFso.CreateFolder(strPath)
End Sub
Public Function PathExists(strPath)
Dim bRes: bRes = m_oFso.FileExists(strPath)
If Not bRes Then bRes = m_oFso.FolderExists(strPath)
PathExists = bRes
End Function
Public Function WriteReg(keyRoot, vValue, vType)
On Error Resume Next
m_oShl.RegWrite keyRoot, vValue, vType
WriteReg = (Err.Number = 0)
End Function
Public Function WriteRegString(keyRoot, strValue)
WriteRegString = WriteReg(keyRoot, strValue, "REG_SZ")
End Function
Public Function WriteRegExpString(keyRoot, strValue)
WriteRegExpString = WriteReg(keyRoot, strValue, "REG_EXPAND_SZ")
End Function
Public Function WriteRegDword(keyRoot, dwValue)
WriteRegDword = WriteReg(keyRoot, dwValue, "REG_DWORD")
End Function
Public Function WriteRegBinary(keyRoot, binValue)
WriteRegBinary = WriteReg(keyRoot, binValue, "REG_BINARY")
End Function
Public Function ReadReg(keyRoot)
On Error Resume Next
ReadReg = m_oShl.RegRead(keyRoot)
If Not Err.Number = 0 Then ReadReg = "[notfound]"
End Function
Public Function RegValueExist(keyRoot)
RegValueExist =( ReadReg(keyRoot) <> "[notfound]")
End Function
Public Property Get AppPath
AppPath = m_appLoc
End Property
Public Function RunCommand(cmd)
RunCommand = ""
On Error Resume Next
Dim tmpOut: tmpOut = m_oShl.ExpandEnvironmentStrings("%TEMP%\$tmp.out")
Dim retCode: retCode = m_oShl.Run("%comspec% /c " & cmd & " >""" & tmpOut & """" , 0, True)
Dim outFile: Set outFile = m_objFso.OpenTextFile(tmpOut)
If Err.Number <> 0 Then
RunCommand = outFile.ReadAll
'MsgBox outFile.ReadAll
outFile.Close
m_objFso.DeleteFile tmpOut
End If
Set outFile = Nothing
End Function
Public Sub RunRawCmd(cmd, bWin,bWait)
m_oShl.Run "%comspec% /c " & cmd, bWin, bWait
End Sub
Public Sub RestartSystem()
m_oShl.Run "shutdown -r -f -t 00", 0, False
End Sub
Public Function ImportRegFile(regFile)
'ImportRegFile = RunCommand("reg import """ & regFile & """")
ImportRegFile = RunCommand("regedit /s """ & regFile & """")
End Function
Public Sub Sleep(nSecs)
Dim t: t="~$slp.vbs"
RunRawCmd "echo wscript.sleep " & nSecs & "000> " & t & " & " & t & " & del /f/q " & t, 0, True
End Sub
Public Property Let CurrentDirectory(strDir)
m_oShl.CurrentDirectory = strDir
End Property
Public Property Get CurrentDirectory
CurrentDirectory = m_oShl.CurrentDirectory
End Property
End Class
--------------------------