Sunday, October 9, 2011

Change mts file through scripting

''*Mainly Make the File open as default "NotePad"

''* Declaring Variables
Dim fso, fs, pat, test_Name, mts_Action, backup_File, filepath, mts_filepath, tt, lineNo, bln_Flag, writ, z, act_Rows

''* Declaring Arrays
Dim Array1(), Array2(), act_Array()

''* Assigning Variable values
pat = "D:\Baba_QTP_Framework\QTP Tests"
test_Name = "ChangeThisMST"
mts_Action = "Action1"

''* Keeping One Backup file before changes with below name
backup_File = pat&"\"&test_Name&"\"&mts_Action&"\Script1.mts"

''* Creating a File system Object
Set fso = createobject("scripting.filesystemobject")

''* Creating Text File to keep updated lines from mts file
filepath = pat&"\"&test_Name&"\"&mts_Action&"\Script.txt"
fso.CreateTextFile (filepath)

''* Getting mts file
mts_filepath = pat&"\"&test_Name&"\"&mts_Action&"\Script.mts"
Set f = fso.GetFile(mts_filepath)

''* Opening mts file as TextStream and reading all lines to keep them in an arrays
Set tt = f.OpenAsTextStream(1, -2)
lineNo = 0
Do Until tt.AtEndOfStream
    bln_Flag = False
    ReDim Preserve Array1(lineNo)
    ReDim Preserve Array2(lineNo)
    writ = tt.ReadLine
    Array1(lineNo) = writ
    If Instr(UCase(Writ), UCase("Hai")) Then
        bln_Flag = True
        writ = "'"&writ
        Array2(lineNo) = writ
    End If
    If Not(bln_Flag) Then
        Array2(lineNo) = writ
    End If
    Call Fso_Write(filepath,writ)
    lineNo = lineNo+1

''* Closing the mts file

''* Capturing all lines into array from txt file
Set z = fso.OpenTextFile(filepath)
act_Rows = 0
Do Until z.AtEndOfStream
    ReDim Preserve act_Array(act_Rows)
    act_Array(act_Rows) = z.ReadLine
    act_Rows = act_Rows+1

''* Looping for all lines in actual mts file and saved txt file.
For k = 0 to ubound(act_Array)
    bln_Flag = False
    If Instrrev(act_Array(k), Array1(k))>0 Then
        bln_Flag = True
    End If

''* Validating whether all lines in copied file and actual file are matching or not?
If bln_Flag Then
    msgbox "all lines are matched"
    msgbox "all lines are not matched"
End If

''* Function to write line or append in Text file
Function Fso_Write(filepath,writ)
   Dim wr,wrt
   Set wr = CreateObject("Scripting.FileSystemObject")
   Set wrt = wr.OpenTextFile(filepath, 8, true)
   wrt.WriteLine writ
   Set wrt = nothing
   Set wr = nothing
End Function

''* Keeping backup file before changes
fso.CopyFile mts_filepath, backup_File,True

''* Copying text file to .mts file and replacing existing file
fso.CopyFile filepath, mts_filepath, True

''* Setting all objects to nothing --  releasing memory
Set f = nothing
Set fso = nothing
Set tt = nothing
Set z = nothing

"A good threat is worth a thousand tests" - Boris Beizer

1 comment:

mts to dvd osx said...

Thank you so much. I had been trying to make this work for 2 days. It took 1/2 hour after reading this.

There was an error in this gadget