QTP:- Working with Drives and Folders examples

Folder

Working with Drives and Folders

a) Creating a Folder
b) Deleting a Folder
c) Move folder
d) Copying Folders
e) Display all the subfolders in a folder.
f) Add  folder  to folder  collection
g) Checking whether the folder available or not, if not creating the folder
h) Returning a collection of Disk Drives
I) getting available space on a Disk Drive

a) Creating a Folder
Option Explicit
Dim objFSO, objFolder, strDirectory
strDirectory = "D:\logs"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder(strDirectory)

b) Deleting a Folder
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.DeleteFolder("E:\FSO")

c) Move folder
Sub MoveAFolder(Drivespec)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   fso.MoveFolder Drivespec, "c:\windows\desktop\"
End Sub ‘object.MoveFolder ( source, destination );

d) Copying Folders
Set oFSO=createobject("Scripting.Filesystemobject")
oFSO.CopyFolder "E:\kkk6", "C:\jvr", True ‘syntax: object.CopyFolder ( source, destination[, overwrite] ); If true, files are overwritten; if false, they are not. The default is true.

e) Display all the subfolders in a folder.
The following code illustrates how to get a Folders collection and how to iterate the collection using the For Each...Next statement:
Function ShowFolderList(folderspec)
   Dim fso, f, f1, fc, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(folderspec)
   Set fc = f.SubFolders
   For Each f1 in fc
      s = s & f1.name
      s = s &   "<BR>"
   Next
   ShowFolderList = s
End Function

f) Add  folder  to folder  collection
Adds a new folder to a Folders collection.
Sub AddNewFolder(path, folderName)
   Dim fso, f, fc, nf
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(path)
   Set fc = f.SubFolders
   If folderName <> "" Then
      Set nf = fc.Add(folderName)
   Else
      Set nf = fc.Add("New Folder")
   End If
End Sub

g) Checking weather the folder available or not, if not creating the folder
Option Explicit
Dim objFSO, objFolder, strDirectory
strDirectory = "D:\logs"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDirectory) Then
   Set objFolder = objFSO.GetFolder(strDirectory)
   msgbox strDirectory & " already created "
else
Set objFolder = objFSO.CreateFolder(strDirectory)
end if

h) Returning a collection of Disk Drives
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = oFSO.Drives
For Each oDrive in colDrives
MsgBox "Drive letter: " & oDrive.DriveLetter
Next

I) Getting available space on a Disk Drive

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oDrive = oFSO.GetDrive("C:")
MsgBox "Available space: " & oDrive.AvailableSpace

j) Get all the drives
The following code illustrates how to get the Drives collection and iterate the collection using the For Each...Next statement:
Function ShowDriveList
   Dim fso, d, dc, s, n
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set dc = fso.Drives
   For Each d in dc
      n = ""
      s = s & d.DriveLetter & " - "
      If d.DriveType = Remote Then
         n = d.ShareName
      ElseIf d.IsReady Then
         n = d.VolumeName
      End If
      s = s & n & "<BR>"
   Next
   ShowDriveList = s
End Function

k) Provide access to particular drive.
Provides access to the properties of a particular disk drive or network share.
Function ShowFreeSpace(drvPath)
   Dim fso, d, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set d = fso.GetDrive(fso.GetDriveName(drvPath))
   s = "Drive " & UCase(drvPath) & " - "
   s = s & d.VolumeName   & "<BR>"
   s = s & "Free Space: " & FormatNumber(d.FreeSpace/1024, 0)
   s = s & " Kbytes"
   ShowFreeSpace = s
End Function

l) Available space property
Returns the amount of space available to a user on the specified drive or network share.
Function ShowAvailableSpace(drvPath)
   Dim fso, d, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set d = fso.GetDrive(fso.GetDriveName(drvPath))
   s = "Drive " & UCase(drvPath) & " - "
   s = s & d.VolumeName   & "<BR>"
   s = s & "Available Space: " & FormatNumber(d.AvailableSpace/1024, 0)
   s = s & " Kbytes"
   ShowAvailableSpace = s
End Function

m) DriveLetter Property
Returns the drive letter of a physical local drive or a network share. Read-only.
Function ShowDriveLetter(drvPath)
   Dim fso, d, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set d = fso.GetDrive(fso.GetDriveName(drvPath))
   s = "Drive " & d.DriveLetter & ": - "
   s = s & d.VolumeName & "<BR>"
   s = s & "Free Space: " & FormatNumber(d.FreeSpace/1024, 0)
   s = s & " Kbytes"
   ShowDriveLetter = s
End Function

n) DriveType Property
Returns a value indicating the type of a specified drive.
Function ShowDriveType(drvpath)
   Dim fso, d, t
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set d = fso.GetDrive(drvpath)
   Select Case d.DriveType
      Case 0: t = "Unknown"
      Case 1: t = "Removable"
      Case 2: t = "Fixed"
      Case 3: t = "Network"
      Case 4: t = "CD-ROM"
      Case 5: t = "RAM Disk"
   End Select
   ShowDriveType = "Drive " & d.DriveLetter & ": - " & t
End Function

o) FileSystem Property
Returns the type of file system in use for the specified drive.  (Available return types include FAT, NTFS, and CDFS.)
Function ShowFileSystemType(drvspec)
   Dim fso,d
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set d = fso.GetDrive(drvspec)
   ShowFileSystemType = d.FileSystem
End Function

p) FreeSpace Property
Returns the amount of free space available to a user on the specified drive or network share. Read-only. (The value returned by the FreeSpace property is typically the same as that returned by the AvailableSpace property. Differences may occur between the two for computer systems that support quotas.)
Function ShowFreeSpace(drvPath)
   Dim fso, d, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set d = fso.GetDrive(fso.GetDriveName(drvPath))
   s = "Drive " & UCase(drvPath) & " - "
   s = s & d.VolumeName   & "<BR>"
   s = s & "Free Space: " & FormatNumber(d.FreeSpace/1024, 0)
   s = s & " Kbytes"
   ShowFreeSpace = s
End Function

q) IsReady Property
Returns True if the specified drive is ready; False if it is not. (For removable-media drives and CD-ROM drives, IsReady returns True only when the appropriate media is inserted and ready for access.)
Function ShowDriveInfo(drvpath)
   Dim fso, d, s, t
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set d = fso.GetDrive(drvpath)
   Select Case d.DriveType
      Case 0: t = "Unknown"
      Case 1: t = "Removable"
      Case 2: t = "Fixed"
      Case 3: t = "Network"
      Case 4: t = "CD-ROM"
      Case 5: t = "RAM Disk"
   End Select
   s = "Drive " & d.DriveLetter & ": - " & t
   If d.IsReady Then
      s = s & "<BR>" & "Drive is Ready."
   Else
      s = s & "<BR>" & "Drive is not Ready."
   End If
   ShowDriveInfo = s
End Function

r) Path Property
Returns the path for a specified file, folder, or drive. (For drive letters, the root drive is not included. For example, the path for the C drive is C:, not C:\)
Function ShowFileAccessInfo(filespec)
   Dim fso, d, f, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFile(filespec)
   s = UCase(f.Path) & "<BR>"
   s = s & "Created: " & f.DateCreated & "<BR>"
   s = s & "Last Accessed: " & f.DateLastAccessed & "<BR>"
   s = s & "Last Modified: " & f.DateLastModified  
   ShowFileAccessInfo = s
End Function

s) RootFolder Property
Returns a Folder object representing the root folder of a specified drive. Read-only. (All the files and folders contained on the drive can be accessed using the returned Folder object.)
Function ShowRootFolder(drvspec)
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetDrive(drvspec)
   ShowRootFolder = f.RootFolder
End Function

t) SerialNumber Property
Returns the decimal serial number used to uniquely identify a disk volume.
Function ShowDriveInfo(drvpath)
   Dim fso, d, s, t
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set d = fso.GetDrive(fso.GetDriveName(fso.GetAbsolutePathName(drvpath)))
   Select Case d.DriveType
      Case 0: t = "Unknown"
      Case 1: t = "Removable"
      Case 2: t = "Fixed"
      Case 3: t = "Network"
      Case 4: t = "CD-ROM"
      Case 5: t = "RAM Disk"
   End Select
   s = "Drive " & d.DriveLetter & ": - " & t
   s = s & "<BR>" & "SN: " & d.SerialNumber
   ShowDriveInfo = s
End Function

g) ShareName Property
Returns the network share name for a specified drive.
Function ShowDriveInfo(drvpath)
   Dim fso, d
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set d = fso.GetDrive(fso.GetDriveName(fso.GetAbsolutePathName(drvpath)))
   ShowDriveInfo = "Drive " & d.DriveLetter & ": - " & d.ShareName
End Function

h) TotalSize Property
Returns the total space, in bytes, of a drive or network share.
Function ShowSpaceInfo(drvpath)
   Dim fso, d, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set d = fso.GetDrive(fso.GetDriveName(fso.GetAbsolutePathName(drvpath)))
   s = "Drive " & d.DriveLetter & ":"
   s = s & vbCrLf
   s = s & "Total Size: " & FormatNumber(d.TotalSize/1024, 0) & " Kbytes"
   s = s & vbCrLf
   s = s & "Available: " & FormatNumber(d.AvailableSpace/1024, 0) & " Kbytes"
   ShowSpaceInfo = s
End Function

I)VolumeName Property
Function ShowVolumeInfo(drvpath)
   Dim fso, d, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set d = fso.GetDrive(fso.GetDriveName(fso.GetAbsolutePathName(drvpath)))
   s = "Drive " & d.DriveLetter & ": - " & d.VolumeName
   ShowVolumeInfo = s
End Function

j) Rename all files in a folder and its subfolder
At the end of the root procedure, it shows “path and names of files which are renamed”. This msgbox is useful if the number of files is very small. If files are in huge numbers then modify this code if you want to see the names and path of all the files renamed or if you don’t want this you can simply convert that msgbox into a comment.
Write this code on a notepad and save it with .vbs extension. It asks you the path of the folder and the string to be replaced in each filename and the new string by which the old string is to be replaced.
Set fso = CreateObject ("Scripting.FileSystemObject")
Dim uu, uu1
total_files_renamed = 0
total_files_skipped = 0
root
set fso = nothing
Sub root
path = inputbox ("enter path")
old_string=inputbox ("enter string to be replaced")
new_string=inputbox ("enter new string")
Set folder_path = fso.GetFolder(path)
msgbox "Warning: All files within the Folder """ &_
folder_path.Path &""" will be renamed."
procedure_sub_folders folder_path , old_string, new_string
Msgbox "Total Files Renamed :" &total_files_renamed
Msgbox "Total Files Skipped :" &total_files_skipped
msgbox "Path and names of files which are renamed" &uu1
End Sub
Sub procedure_sub_folders (ByVal curr_folder, ByVal old_val, ByVal new_val)
 Set Folders = curr_folder.SubFolders
procedure_folder curr_folder , old_val, new_val
For Each Folder in Folders
procedure_sub_folders Folder , old_val, new_val
next
end Sub
Sub procedure_folder (ByVal folder, ByVal old_val, ByVal new_val)
 Set Files = folder.Files
For Each File In Files
If inStr(1,File.Name,old_val) > 0 Then
uu= Replace(File.Path, old_val, new_val)
uu1= uu1 &vbcrlf &uu
File.Move Replace(File.Path,old_val,new_val)
total_files_renamed = total_files_renamed + 1
else
total_files_skipped = total_files_skipped + 1
End If
Next
End Sub

k) How to rename all the files in a folder?
Type the below code in a notepad, save it with .vbs extension and run it from command prompt. You have to change the folder name (C:\abc) and the strname to suit your requirements.
Below code renames all the files in a given folder to Sac 1, Sac 2....and so on.

strComputer = "."
Set objWMIService = GetObject ("winmgmts:\\" &strComputer &"\root\cimv2")
Set colFileList = objWMIService.ExecQuery ("ASSOCIATORS OF {Win32_Directory.Name='C:\abc'} Where " &"ResultClass = CIM_DataFile")
x=1
For Each objFile In colFileList
strname = "Sac " &x
strNewName = objFile.Drive &objFile.Path &strname &"." &objFile.Extension
errResult = objFile.Rename(strNewName)
x=x+1
Next

l) How to rename all subfolders within a given folder?
Type the below code in a notepad, save it with .vbs extension and run it from command prompt. In case nothing is entered for main_folder or search_string or replace_string, the script will quit.

main_folder = inputbox ("Enter the main folder whose subfolders need to be renamed" &vbcrlf &"e.g. C:\Folder")
if main_folder="" then quit_function
search_string = inputbox ("Enter the string to be searched")
If search_string = "" then quit_function
replace_string = inputbox ("Enter the string to be replaced")
If replace_string = "" then quit_function
Set object_FSO= CreateObject ("Scripting.FileSystemObject")
rename_function main_folder

Sub rename_function (byval Folder)
‘GetFolder method is used to get the Folder object for the path that you specify. You can ‘then use the new variable containing the folder object to access its various methods and ‘properties.
Set object_folder = object_FSO.GetFolder (Folder)
‘SubFolder property returns a Folders collection consisting of all the subfolders in a given ‘folder.
Set sub_folders = object_folder.Subfolders
For each subfolder in sub_folders
new_foldername = (Replace (subfolder.name, search_string, replace_string))
If new_foldername <> subfolder.Name Then
subfolder.Name = new_foldername
End If
rename_function subfolder.path
Next
End Sub
Sub quit_function
wscript.echo "Script quit_function"
wscript.quit
End sub

m) How to add any path in the Search List '(Tools -> Options -> Folders tab) through a script.
'How to add any path in the Search List '(Tools -> Options -> Folders tab) through a script.
'We are opening a test called "call twra" and adding a path C:\Program Files\Mercury Interactive\QuickTest Professional\Tests in the Search List '(Tools -> Options -> Folders tab)
Dim qtp_app
Dim str_path
Set qtp_app = CreateObject ("QuickTest.Application")
qtp_app.Launch
qtp_app.Visible = True
qtp_app.Open "C:\Program Files\Mercury Interactive\QuickTest Professional\Tests\call twra", True, False
'If the folder C:\Program Files\Mercury Interactive\QuickTest Professional\Tests is not there just add it.
str_path = qtp_app.Folders.Locate ("C:\Program Files\Mercury Interactive\QuickTest Professional\Tests")
‘If the folder is not found in the collection, add it
If qtp_app.Folders.Find (str_path) = -1 Then
qtp_app.Folders.Add str_path, 1 'Add the folder to the collection
End If
'If you have moved "<current test>" downwards in the list, bring it back to first position.
If qtp_app.Folders.Item (2) = "<current test>" Then
 qtp_app.Folders.MoveToPos 1, 2
End If

Popular posts from this blog

Online Selenium Training With Real Time Scenario

Online Tricentis Tosca Automation Training with Real Time Scenarios

Online Training for Manual/Functional