VBS Script to Verify INI file structure

All the random crap I spend all day long figuring out.
Post Reply
User avatar
syntax
Site Admin
Posts: 54
Joined: Tue Jan 06, 2009 9:25 pm

VBS Script to Verify INI file structure

Post by syntax » Mon Apr 28, 2014 5:35 pm

Couldn't find one online so I wrote this. Just verifies keys have [ and ] and also checks to make sure there aren't any duplcate keys or parameters.

Code: Select all

Option Explicit
Dim ServerName, Computer, PrintQueue, wshShell, oINI, sLine, nErrors, x, z
Dim fso, objFile, objFolder, filename, length, strPath, strRootFolder, sTemp
Dim sINIKeys(), sParams(), sArr

CONST WindowsFolder = 0
CONST SystemFolder = 1
CONST TemporaryFolder = 2
CONST ForReading = 1
CONST ForWriting = 2
CONST ForAppending = 8
CONST TristateUseDefault = -2
CONST TristateTrue = -1
CONST TristateFalse = 0

Set wshShell = CreateObject( "WScript.Shell" )

Sub forceCScriptExecution
    Dim Arg, Str
    If Not LCase( Right( WScript.FullName, 12 ) ) = "\cscript.exe" Then
        For Each Arg In WScript.Arguments
            If InStr( Arg, " " ) Then Arg = """" & Arg & """"
            Str = Str & " " & Arg
        Next
        CreateObject( "WScript.Shell" ).Run _
            "cscript //nologo """ & _
            WScript.ScriptFullName & _
            """ " & Str
        WScript.Quit
    End If
End Sub
forceCScriptExecution

Sub Pause(strPause)
     on error resume next
	 WScript.Echo (strPause)
     z = WScript.StdIn.Read(1)
End Sub

Set fso = CreateObject("Scripting.FileSystemObject")

strPath = Wscript.ScriptFullName
Set objFile = fso.GetFile(strPath)
strRootFolder = fso.GetParentFolderName(objFile)
set objFile = Nothing

nErrors = 0
Set objFolder = fso.GetFolder(strRootFolder)
For Each objFile in objFolder.Files
	if UCase(fso.GetExtensionName(objFile.Name)) = "INI" then
		set oINI = fso.OpenTextFile(objFile.Path, ForReading)
		if IsNull(objFile) = False AND Err.Number = 0 then
			z = 1
			ReDim sINIKeys(0)
			ReDim sParams(0)
			Do While oINI.AtEndOfStream = False
				sLine = oINI.ReadLine
				if Left(sLine, 1) = ";" then
					'comment
				elseif Trim(sLine) = "" then
					'blank line
				elseif Left(sLine, 1) = "[" then
					ReDim sParams(0)
					if not Right(sLine, 1) = "]" then
						nErrors = nErrors + 1
						Wscript.Echo objFile.Name & " - invalid key on line " & z
					else
						sTemp = mid(sLine, 2, len(sLine)-2)
						'Wscript.Echo sTemp
						if sINIKeys(0) = "" then
							sINIKeys(0) = sTemp
						else
							for x = 0 to UBound(sINIKeys)
								if sINIKeys(x) = sTemp then
									nErrors = nErrors + 1
									Wscript.Echo objFile.Name & " - duplicate key on line " & z
									x = -1
									exit for
								end if
							next
							if x <> -1 then
								ReDim Preserve sINIKeys(UBound(sINIKeys)+1)
								sINIKeys(UBound(sINIKeys)) = sTemp
							end if
						end if
					end if
				elseif InStr(sLine, "=") then
					sArr = Split(sLine, "=")
					if sParams(0) = "" then
						sParams(0) = sArr(0)
					else
						for x = 0 to UBound(sParams)
							if sParams(x) = sArr(0) then
								nErrors = nErrors + 1
								Wscript.Echo objFile.Name & " - duplicate parameter on line " & z
								x = -1
								exit for
							end if
						next
						if x <> -1 then
							ReDim Preserve sParams(UBound(sParams)+1)
							sParams(UBound(sParams)) = sArr(0)
						end if
					end if
				else
					nErrors = nErrors + 1
					Wscript.Echo objFile.Name & " - line " & z & " invalid"
				end if
				z = z + 1
			Loop
		end if
	end if
Next

Wscript.Echo ""
Wscript.Echo "Number of Errors: " & nErrors
Wscript.Echo ""

set objFile = nothing
set fso = nothing
set wshShell = nothing

Post Reply