1 Replies - 422 Views - Last Post: 29 August 2013 - 01:55 PM Rate Topic: -----

#1 netnewbie  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 28-August 13

Converting VB Script code to VB.Net on file manipulation

Posted 29 August 2013 - 01:51 PM

I have a VB script code which will real the variables and stores them in Global variables for use in scripts.
Here is the code in VB Script. Can some one help me to convert this VB script function into VB.Net class or module.
Code:
'**** This initialize the script and sets the ini data in global variables **** 
 Declare Function FileLib_IniFileReader BasicLib "FileLib" (strIniFileName As String) As Integer
 Declare Function IniRead (sFileName as String, sSection as String,sKey as String) as String
Declare Sub LoadIniFileValuetoGlobalVariabls(sGlobalVariableName As String, sKey_Value As String)  
Declare Function ReadFileIntoArray(sFileName as String, asFileContents() as String, _
                                   Optional vRemoveBlankLines as Variant) as Integer)

Const conStrIniFileName As String = "Regression-Shell.ini"          
    '*** Open the INI file, load the global variables, and set up ***
    '*** initial status for web site display ***
     Result = FileLib_IniFileReader (conStrIniFileName)
     If Result = sqaFail then[attachment=33830:Regression-Shell.txt]
        SQALogMessage SQAFail, "Can not open ini file. " _
             & "Script has aborted.", ""    
        Exit Sub 
     End If

'###############################################################################################
'# Function:        FileLib_IniFileReader(strIniFileName As String) As Integer
'# 
'# Description:        Function to initialize the ini file.
'###############################################################################################
'# Change Log:
'# mm/dd/yy       Name        Summary of Change
'#                                   
'###############################################################################################
Function FileLib_IniFileReader(strIniFileName As String) As Integer

  If Trim(strIniFileName) = "" Then
      FileLib_IniFileReader = sqaFail
      SQALogMessage SQAFail, "The script name or the ini file name wasn't specified" _
                             & " inside the script ", ""
  Else
     If LCase(GS_IniFileAdvanceFilter) <> "false" then
        Call IniRead(strIniFileName,"","")
     End If   
     FileLib_IniFileReader = sqaPass
     
  End If    
           

'###############################################################################################
'# Function IniRead 
'#          =======
'#
'# DESCRIPTION:
'#   Read the value for a given key in an .ini file, without using the Win API.    
'#   Does not verify .ini file existence before execution.
'#   
'# EXAMPLES:    sKeyValue = IniRead(sIniFileName, sSection, sKey)
'#              - or - 
'#              sKeyValue = IniRead("c:\test.ini", "ListItems", "Count")
'#
'# PARAMETERS:
'#       [In]   sIniFileName = valid .ini path/filename
'#       [In]   sSection = section name, without brackets
'#       [In]   sKey = key name
'#
'# RETURNS:
'#       Value for section/key entry if found, else blank.
'#
'# ERRORS:
'#       None
'#
'# Change History:
'# 00/00/00  Author       Change details
'#
'###############################################################################################
  Function IniRead(sFileName as String, sSection as String, sKey as String) as String
    Dim Position As Integer
    Dim sFunctionName as String
    Dim sErrMessage as String
    Dim sFileInputString as String
    Dim iFileNum as Integer
    Dim iRowCount as Integer
    Dim i as Integer
    Dim bRemoveBlankLines as Integer
    Dim sName As String
    Dim sValue As String

    sFunctionName = "IniRead"
     
   'file existence will be validated by ReadFileIntoArray function
    bRemoveBlankLines = TRUE                'this is used for clarity
    iRowCount = ReadFileIntoArray(sFileName, GS_FileArray(), bRemoveBlankLines)
   
    For i = 1 to iRowCount 
       If Trim(sSection) <> "" and Trim(sKey) <> "" then
          If StrComp("[" & sSection & "]", Trim(GS_FileArray(i)), 1) = 0 Then
             i = i +1
             Do Until Left(Trim(GS_FileArray(i)), 1) = "[" Or i > iRowCount 
                If StrComp(sKey, Trim(Left(GS_FileArray(i), Instr(GS_FileArray(i), "=") -1)), 1) = 0 Then 
                    IniRead = Mid(GS_FileArray(i), Instr(GS_FileArray(i), "=") +1) 
                    Exit Function
                End If
                i = i +1
             Loop
          End if
       
       Else 
          If Instr(GS_FileArray(i),"[") = 0 then
             sName = "GS_" & Trim(Left(GS_FileArray(i), instr(GS_FileArray(i),"=")-1))
             Position = Instr(GS_FileArray(i),"=")+1
             sValue = Trim(Mid(GS_FileArray(i), Position))
             Call LoadIniFileValuetoGlobalVariabls (sName , sValue)
          End If          
        
        
       End If
    
    
    Next i

 End Function

'###############################################################################################
'# Function ReadFileIntoArray(sFileName as String, asFileContents() as String _
'#                            Optional vRemoveBlankLines as Variant) as Integer
'#          
'# DESCRIPTION:
'#    Read a text file into a string array, returning the number of lines.
'#
'# PARAMETERS:
'#    [In]      sFileName       
'#    [Out]     asFileContents()    Array containing file contents
'#    [In]      vRemoveBlankLines   (Optional) TRUE or FALSE (default)
'#
'# RETURNS:
'#    Number of array elements (rows), or
'#    0  = file is not found, or
'#    -1 = file is not a text file or other error
'#
'# ERRORS:
'#    none
'# Change History:
'# 00/00/00  Author       Change details
'#
'###############################################################################################

Function ReadFileIntoArray(sFileName as String, asFileContents() as String, _
                           Optional vRemoveBlankLines as Variant) as Integer
    Dim Result as Integer
    Dim Result1 as Integer
    Dim Result2  as Integer    
    Dim Position as Integer
    Dim iFileNum as Integer
    Dim sLineInput as String
    Dim iRowCount as Integer
    Dim sErrMessage as String
    Dim sFunctionName as String
    Dim strFileName as string
    sFunctionName = "ReadFileIntoArray"
    On Error Goto ErrorHandler
    
    ReadFileIntoArray = -1
    
    'validate file existence and file type (= text)    
    'If FSOExists(FILE, sFileName) <> TRUE Then
     sErrMessage = "File not found = " & sFileName
     If IsMissing(sFileName) <> False Then
        
        MsgBox Space(4) & sErrMessage, (0+48+0), "FUNCTION " & sFunctionName 
        SQAConsoleWrite "FUNCTION " & sFunctionName & ": " & sErrMessage
        SQAScriptCmdFailure "FUNCTION " & sFunctionName & ": " & sErrMessage
        ReadFileIntoArray = 0
        Exit Function
    Else
'        strFileName = "C:\IniFiles\" & sFileName
        SQASuspendLogOutput         
          
          'Result1 =  FileVP (Exists, "Name=" & strFileName , "VP=" &sFileName) 
          Result1 =  FileVP (Exists, "Name=" & sFileName , "VP=" &sFileName) 

          If Result1 <> 1 then
             strFileName = GS_iniFile_LocationName & "\" & sFileName
          End If
          
          Result2 =  FileVP (Exists, "Name=" & strFileName , "VP=" &sFileName)   
          If Result2 <> 1 then
             SQAConsoleWrite sErrMessage
             SQAScriptCmdFailure sErrMessage 
          Else
             SQAConsoleWrite strFileName
             SqalogMessage SqaNone, "Read the ini file = " &strFileName,""  
          End IF
        SQAResumeLogOutput  
    
    End If

    'validate optional parameter
    If IsMissing(vRemoveBlankLines) Then
        vRemoveBlankLines = FALSE       'default condition
    End If
    
    iFileNum = Freefile
    
    Open strFileName for Input as #iFileNum 
    
    Do Until EOF(iFileNum)
        Line Input #iFileNum, sLineInput
        If vRemoveBlankLines = TRUE Then
            If Trim(sLineInput) <> "" Then
                Position = Instr(sLineInput, ";")
                If Position = 0 then
                   iRowCount = iRowCount + 1
                   ReDim Preserve asFileContents(iRowCount)
                   asFileContents(iRowCount) = sLineInput
                End If   
            End If
        Else
            iRowCount = iRowCount + 1
            ReDim Preserve asFileContents(iRowCount)
            asFileContents(iRowCount) = sLineInput
        End If
    Loop

    Close #iFileNum 

    ReadFileIntoArray = iRowCount
    Exit Function
    
ErrorHandler:
'    MsgBox "Error number " & Err & " occurred at line: " & Erl
End Function

'###############################################################################################
'# Sub Program:        LoadIniFileValuetoGlobalVariabls(sGlobalVariableName As String, _
'#                                                      sKey_Value As String)
'# '# Description:        This sub program writes loads the ini file values to the 
'#                     Global variables.
'##############################################################################################
'# Change Log:
'# mm/dd/yy       Name        Summary of Change
'#                                   
'##############################################################################################
Global GS_DataPoolName As String
 Global GS_DataPoolName1 As String
 Global GS_DataPoolName2 As String
Global GS_PrintCaption As String
Global GS_MCNum As String
Global GS_IniFileAdvanceFilter
Global GS_DPBookmark
(The below ones are the one which are read from the .ini(or .txt) file.)
Sub LoadIniFileValuetoGlobalVariabls(sGlobalVariableName As String, sKey_Value As String)  
    Dim Result As String
    
    Select Case sGlobalVariableName
           '################[General]##################
           Case "GS_PrintCaption"
                 GS_PrintCaption = sKey_Value
           Case "GS_MCNum"
                 GS_MCNum = sKey_Value   
          '##### [Datapool] ######
           Case "GS_DataPoolName"
                 GS_DataPoolName = sKey_Value
           Case "GS_DataPoolName1"
                 GS_DataPoolName1 = sKey_Value 
                     '##### [Test Configuration] ######
           Case "GS_IniFileAdvanceFilter"
                 GS_IniFileAdvanceFilter = sKey_Value
           Case "GS_DPBookmark"
                 GS_DPBookmark = sKey_Value
           Case "GS_TestVP"
                 GS_TestVP = sKey_Value
           Case "GS_Prefix"
                 GS_Prefix = sKey_Value
           Case "GS_CheckSystemAvail"
                 GS_CheckSystemAvail = sKey_Value
        
            
    End Select
End Sub

'##############################################################################################

Attached File(s)


This post has been edited by modi123_1: 29 August 2013 - 01:54 PM
Reason for edit:: use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Converting VB Script code to VB.Net on file manipulation

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8379
  • View blog
  • Posts: 31,148
  • Joined: 12-June 08

Re: Converting VB Script code to VB.Net on file manipulation

Posted 29 August 2013 - 01:55 PM

Seems straight forward - where are you having problems?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1