‘ Create File System Object
Set oFSO = CreateObject(“Scripting.FileSystemObject”)
‘Set folder and file name
strFolder = “C:QTPSchools”
strFile = “abhi.txt”
‘ Check if strFolder folder exists. If not, create it.
If oFSO.FolderExists(strFolder).Exist = False Then
Set oFolder = oFSO.CreateFolder(strFolder)
Print “Folder did’t exist already. Created now – ” & strFolder
End If
‘ Check if oFile folder exists. If not, create it.
If oFSO.FileExists(strFolder & strFile).Exist = False Then
Set oFile = oFSO.CreateTextFile(strFolder & strFile)
Print “File did’t exist already. Created now – ” & strFolder & strFile
End If
set oFile = Nothing ‘Relese object
set oFolder = Nothing ‘Relese object
Following code demonstrate how to open an existing file and write a line to file and close.
Set oFSO = CreateObject(“Scripting.FileSystemObject”)
Const ForWriting = 2
Set oTxtFile = oFSO.OpenTextFile(“C:QTPSchoolsabhi.txt”, ForWriting, True)
oTxtFile.WriteLine “This is the new line – ForWriting”
oTxtFile.Close
Append a line to file
Const ForAppending = 8
Set oTxtFile = oFSO.OpenTextFile(“C:QTPSchoolsabhi.txt”, ForAppending, True)
oTxtFile.WriteLine “This is the new line – ForAppending”
oTxtFile.Close
Please note that if you open file in Writing mode, all the existing data will be overridden by new data. But if you open file in Appending mode then all existing data will be preserved and new data will be appended at bottom.
Const ForReading = 1
Set oTxtFile = oFSO.OpenTextFile(“C:QTPSchoolsabhi.txt”, ForReading, True)
Print oTxtFile.Readline ‘it will read first line from the file
oTxtFile.Close
Const ForReading = 1
Set oRegEx = CreateObject(“VBScript.RegExp”)
oRegEx.Pattern = “new.*” ‘ Enter pattern/string you want to search
Set oFSO = CreateObject(“Scripting.FileSystemObject”)
Set oFile = oFSO.OpenTextFile(“C:QTPSchoolsabhi.txt”, ForReading)
Do Until oFile.AtEndOfStream
strSearchString = oFile.ReadLine
Set colMatches = oRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
print strSearchString
End If
Loop
oFile.Close
strFilename = “C:DIRall_Filestry.txt”
Const ForReading = 1
Const ForWriting = 2
Set oRegEx = CreateObject(“VBScript.RegExp”)
oRegEx.Pattern = “requestId””:””d{3}”
Set oFSO1 = CreateObject(“Scripting.FileSystemObject”)
Set oFile = oFSO1.OpenTextFile(strFilename, ForReading)
strOldString = oFile.ReadAll
Set colMatches = oRegEx.Execute(strOldString)
For each match in colMatches
msgbox match.value
strNewString = Replace(strOldString, match.value , “requestId””:””” &Corr_Id&””)
Next
oFile.Close
Set oFSO1 = Nothing
Set oFile = oFSO2.OpenTextFile(strFilename, ForWriting,True)
oFile.WriteLine strNewString
oFile.Close
End Function
Set oFSO = CreateObject(“Scripting.FileSystemObject”)
Set oFile = oFSO.DeleteFile(“C:QTPSchoolsabhi.txt”)
Dim fso, f, s
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set f = fso.GetFile(filename)
s = “Path: ” & f.Path & vbnewline
s = s & “Created: ” & f.DateCreated & vbnewline
s = s & “Last Accessed: ” & f.DateLastAccessed & vbnewline
s = s & “Last Modified: ” & f.DateLastModified
ShowFileAccessInfo = s
msgbox ShowFileAccessInfo
In case of any queries, please leave your comments.
Awesome job man….
Thank you for good examples
I am trying to copy a file form network to the c drive on my PC. Script is running fine with a hard coded pathes but when I run the same script using parameters for sourceflocation and source destination from QT-Scripted (BPT component)- I have an error "Permission denied". Any Ideas?
Hi I am Mumthaj. Could you tell me how to count words in a text file