Thursday, September 24, 2009

VBS Utility Class

i had written several vbs and hta scripts at work to do some automation for me. here's a utility class for registry read/write, file create/write, getting hostname and others. just customize it in anyway you want. if you have any questions, just drop me a line.

[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

--------------------------

Tuesday, September 15, 2009

File Comparison Batch Script

at work, i needed to compare the files in the mounted ISO image to the extracted raw files. there are several tools which compares directories like the built-in fc.exe of windows and other freewares but what i needed is something simple which would only check for the files on the the same folder location which is just relative to the root location. for example, the mounted ISO image is my reference subject and the files in it should be found on the extracted file structure of the same relative folder location.

here's what i've came up. posting it here for future reference and for those who might have the same needs.

listing: compdir.bat
---------------
::reference dir
@set REFDIR=%~1
::this is the copied directory which should be checked
@set CMPDIR=%~2
::call the root first
@call :CMPPROC
@goto XXX

:CMPPROC
@set MRGPATH=%REFDIR%%~1
@echo RefDir: %MRGPATH%
@echo TarDir: %CMPDIR%%~1
@echo ------------------------------------
::check on the files
@for /f "usebackq tokens=* delims=" %%i in (`@dir /b/a:-d "%MRGPATH%"`) do @if exist "%CMPDIR%%~1\%%i" (@call :CMPFILE "%%i" "%MRGPATH%\%%i" "%CMPDIR%%~1\%%i" ) else (@echo [NG] "%%i")
@echo ------------------------------------
::iterate on the sub directories
@for /f "usebackq tokens=* delims=" %%i in (`@dir /b/a:d "%MRGPATH%"`) do @call :CMPPROC "%~1\%%i"
@goto :EOF

:CMPFILE
@fc /B "%~2" "%~3" >nul
@if %ERRORLEVEL%==0 (@echo [OK] "%~1") else (@echo [NG] "%~1")
@goto :EOF

:XXX
@echo Done

---------------