VB

HOME
UP
VB .NET2005

 

This is Visual Basic / VBScript related stuff

Parse Multiple Textlines in forward or reverse Order of Lines

this codesnippet allows You to parse Textfiles from e.g. etc\services by line in forward or reverse order and do something with it.

Parse Textlines ' (c) 2004 by CleverITS, Wolfgang Zerzawy
' free for Non-Commercial Use


Option Explicit
' On Error Resume Next
Dim strFile, strLine
Dim aMyReadLines
strFile = "C:\Boot.ini" ' this is Your InputFile

aMyReadLines = ReadLines(strFile)

'read Forward
ReadLinesForward(aMyReadLines)
'now read Reverse
ReadLinesReverse(aMyReadLines)

WScript.Quit(0)

'#################### Display Lines #####################
Sub ReadLinesForward(aMyReadLines)
  For strLine = LBound(aMyReadLines) to Ubound(aMyReadLines) Step 1
    Wscript.Echo aMyReadLines(strLine)
  Next
End Sub

'#################### Display Lines Reverse #####################
Sub ReadLinesReverse(aMyReadLines)
  For strLine = Ubound(aMyReadLines) to LBound(aMyReadLines) Step -1
    Wscript.Echo aMyReadLines(strLine)
  Next
End Sub

'#################### Read Lines Function #####################
Function ReadLines(strFile)
  Dim FSO, oFile 'Object
  Dim aReadLines() 'Array
  Dim i, ret 'Variant

  Set FSO = CreateObject("Scripting.FileSystemObject")
  Set oFile = FSO.OpenTextFile(strFile, 1)
  i = 0
  Do Until oFile.AtEndOfStream
    Redim Preserve aReadLines(i)
    aReadLines(i) = oFile.ReadLine
    i = i + 1
  Loop

  ReadLines = aReadLines

  oFile.Close 'Close Filehandle
  Set oFile = Nothing 'Release oFile
  Set FSO = Nothing 'Release FSO
End Function

Manipulate etc\services from WindowsInstaller (MSI) Custom Action

Gruppenfeld ' (c) 2004 by CleverITS, Wolfgang Zerzawy
' free for Non-Commercial Use

option Explicit
On Error Resume Next
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim FSO, WSHShell
Dim args, EnvVar
Dim strText, strFile, windir
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSHShell = CreateObject("Wscript.Shell")
Set args = WScript.Arguments

Dim arrServices(11)
arrServices(0) ="aris50 8003/tcp"
arrServices(1) ="_aris50 8004/tcp"
arrServices(2) ="aris50_adm 8005/tcp"
arrServices(3) ="aris61_name_public 16010/tcp"
arrServices(4) ="aris61_name_private 16011/tcp"
arrServices(5) ="aris61_name_admin 16012/tcp"
arrServices(6) ="aris61_local_public 16013/tcp"
arrServices(7) ="aris61_Sybase 16014/tcp"
arrServices(8) ="aris61_local_private 16016/tcp"
arrServices(9) ="aris61_local_Sybase 16015/tcp"
arrServices(10) ="aris61_local_admin 16018/tcp"
arrServices(11) ="aris61_admin_agent 16017/tcp"

Set EnvVar = wshShell.Environment("PROCESS")
strFile = EnvVar.Item("windir") & "\system32\drivers\etc\services"


If args.Count > 0 Then
  Select Case args(0)
  Case "/i"
    AddService (strFile)
  Case "/u"
    RemoveService (strFile)
  Case Else
    AddService (strFile)

  End Select
Else
MsgBox "Usage: " & vbCrLF & "services.vbs /i to add Services" & vbCrLF & "services.vbs /u to remove Services"

End If
'split lines into service name, serviceport and comment


Sub AddService(strFile)
Dim strService, oTargetText, f, ret
'strFile = CStr(strFile)
If FSO.FileExists (strFile) Then
ret = FSO.CopyFile(strFile, strFile & ".bak",True)
Set oTargetText = FSO.OpenTextFile(strFile,ForAppending)
oTargetText.WriteLine (vbCrLF)
For Each strService In arrServices
oTargetText.WriteLine (strService)
Next
oTargetText.Close
End If
End Sub

Sub RemoveService(strFile)
Dim arrWriteServices, arrSearchServices, strServices, strWriteService, oTargetText, oSourceText
Dim strElement
If FSO.FileExists (strFile) Then
'read all lines and split into array
Set oSourceText = FSO.openTextFile(strfile, ForReading)
strServices = oSourceText.ReadAll
arrSearchServices = Split(strServices,vbCrLF)
oSourceText.Close


If UBound (arrSearchServices) > 0 Then
arrWriteServices = arrSearchServices
'arrServices is defined on Top on this File (the Services we've added)
For Each strElement In arrServices
arrSearchServices = Filter(arrSearchServices,strElement,FALSE,1)
Next

strWriteService = Join(arrSearchServices,vbCrLF)
Set oTargetText = FSO.openTextFile(strFile, ForWriting,TRUE)
oTargetText.Write strWriteService
oTargetText.Close
End If
End If
' now write the Services back...

End Sub

WScript.Quit(1)

 

see navigation on the left side ....

(c) by Wolfgang Zerzawy 07-Mai-08 12:11   

XING