Menu Bar of the blog

Tuesday, 30 December 2014

UFT CODE#4: File System Object

1. Following code can be used to read and write in a text file:-

Set fso = CreateObject("Scripting.FileSystemObject")    'Creating File System Object.
filePath = "C:\Pharmacy\Automation\myFile.txt"            'Defining File Path.
If fso.FileExists(filePath) Then                        'Checking whether file exists.
    Set myFile = fso.OpenTextFile(filePath, 2true)    'Opening the file; 2 represents write mode(8 represents append mode); true represents file will be created if file doesn't exist.
End If
myFile.WriteLine "This is the first line."                'writing in a file.
myFile.WriteLine "This is the second line."
Set myFile = nothing
Set myFileReadMode = fso.OpenTextFile(filePath, 1true)    'Opening file in read mode.
While myFileReadMode.AtEndOfStream <> true                'condition of loop: till the end of file is found.
    msgbox myFileReadMode.ReadLine                        'Reading a line.
Wend


2. Finding no. of files in a directory:-
Set fso = CreateObject("Scripting.FileSystemObject")    'Creating File System Object.
Set f = fso.GetFolder("U:\Humana\Automation")            'Getting contents of the folder.
Set fc = f.Files                                        'Getting files of the folder.
For each fa in fc                                        'Getting each file one by one.
    msgbox fa.Name                                        'Displaying names of the files.
Next

3. Finding no. of folders in a directory:-
Set fso = CreateObject("Scripting.FileSystemObject")    'Creating File System Object.
Set f = fso.GetFolder("C:\Pharmacy")                    'Getting contents of the folder.
Set fc = f.SubFolders                                    'Getting folders of the directory.
For each fa in fc                                        'Getting each file one by one.
    msgbox fa.Name                                        'Displaying names of the files.
Next


'''''''''''''''''''''''''Just Like that'''''''''''''''''''''''''''''''''''''''
4. Random string generation of 5 characters, from a given string:-
str = "asdfmlzdkmbfd"
For i = 1 To 5
    r = randomNumber(1,len(str))
    s = s & mid(str, r, 1)
Next
msgbox s                           'output - kslzs
======================*****************************======================

No comments:

Post a Comment