Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Showing posts with label FileSystem Object. Show all posts
Showing posts with label FileSystem Object. Show all posts

Renaming all/specific subfolders in a folder using vbscript FSO

In this post, how to rename all the subfolders in a folder using vbscript is explained

  • Renaming all sub folders in a folder removing spaces from the file

call RemoveblankSpace_Subfolders("D:\TextTest")

 ''strbasefoldername - Folder Path in which file needs to be replaced
 Public Function RemoveblankSpace_Subfolders(strbasefoldername)
 On error resume next
 Dim objFSO
 Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Dim objFolder 
    For Each objSubFolder In objFSO.GetFolder(strbasefoldername).SubFolders
     FlderName = objSubFolder.Name
  FlderName = replace(FlderName," ","")
  objSubFolder.Name = FlderName
    Next 
    Set objSubFolderFolder = Nothing 
    Set objFSO = Nothing 
End Function

  • Rename specific subfolder in a folder using vbscript


call Rename_Subfolders("D:\TextTest","es","twist",false,"test")

'' strbasefoldername - Folder Path in which file needs to be replaced
'' strFindtext - text in the folder name that needs to be replaced
'' strReplaceText - new text to replace the existing test
'' boolAllFlders - boolean flag to indicate if all the subfolder needs to be renamed or
'' specific folders needs to be renamed
'' OnlyfoldersWithText - replace only folder with specific text. will consider only if
'' boolAllFlders =true
 Public Function Rename_Subfolders(strbasefoldername,strFindtext,strReplaceText,boolAllFlders,OnlyfoldersWithText)
 On error resume next
 Dim objFSO
 Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Dim objFolder
    For Each objSubFolder In objFSO.GetFolder(strbasefoldername).SubFolders
 FlderName = objSubFolder.Name
  if boolAllFlders = false then
   if(instr(1,FlderName,OnlyfoldersWithText)>0) then
    FlderName = replace(FlderName,strFindtext,strReplaceText)
    objSubFolder.Name = FlderName
   End If
  else
    FlderName = replace(FlderName,strFindtext,strReplaceText)
    objSubFolder.Name = FlderName
  End If
    Next 
    Set objSubFolderFolder = Nothing 
    Set objFSO = Nothing 
End Function

How to compare content of files using fc command

I was having issues with comparison of two large csv files that I came across fc comparision command of Microsoft DOS. fc  is a command line program that compares multiple files and outputs the differences between them


The syntax of fc Command and results of execution of fc with different arguments is shown below:




Syntax for fc command:


fc filename1 filename 2 [Parameter for comparision type] >Log File Path

where filename1 and filename2 are paths of the two files to be compared .
LogFilePath is path for saving result log
Commonly used Parameters are as described below:
/a :  fc displays only the first and last line for each set of differences.
/b : Compares the files in binary mode.
/c : Ignores the case of letters.
/l :  Fc compares the two files line by line and attempts to resynchronize the files after finding a mismatch. 
/lbn : Sets the n number of lines for the internal line buffer.
/n : Displays the line numbers during an ASCII comparison.

How to write to Text Files using fileSystemObject in QTP

Using FileSystemObject we can write data to a text file. Below are the steps to write to a text file.


1. Create an instance of the FileSystemObject

 This is done through
Set objFSO = CreateObject("Scripting.FileSystemObject")

2. Use OpenTextFile to open an existing file or CreateTextFile to create a new text file to write data into.

Set otf = objFSO.CreateTextFile(FilePath,[OverWrite])
where FilePath is path of file to be created.OverWrite – Flag for overwriting content 

OR

 Set otf = objFSO.OpenTextFile(FilePath,ioMode)

where FilePath is path of file to be opened.ioMode – Define mode in which file to be opened. Use 1 to overwrite data and 8 to append data. 


3. Use below methods to write to the file.


a. Write(strText) - Write specified string in the text file.

Otf.Write(strText)

b. Writeline(strText) - writes a specified string and newline character.

otf.writeline(strText)

c. WriteblankLines(intLine) - Writes the specified number of blank lines in the text file

otf.WriteblankLines(4)

4. Close the text file.

otf.close This will close the file. It will close the text file. Note file is saved automatically when we write to a text file.


5. Set all objects as nothing


How to read text files in QTP using VBScript- Working With FileSystem Object


We can read the content of a text file using FileSystemObject methods and properties. Below are the various methods and properties used to extract data from file.

1. Creating an instance of filesystem object

Before reading content from a file, we have to create instance of filesystemobject.
Set objFSO = CreateObject("Scripting.FileSystemObject")

2. Opening an existing file to read

Set objTxtFile = objFSO.OpenTextFile(FilePath,iomode)

Iomode can have following values:

0 – For reading – opens the file for reading.
1 – For Writing – opens the file for writing, does overwrite the existing content in file.
8 – For Appending – opens the file for appending, new content is appended at the end of file.

3. ReadAll method 

It stores all the content of a file in a variable as string.sContent = objTxtFile.ReadAll

4. ReadLine method

It allows a script to read individual lines in a text file.
sContent = objTxtFile.Readline.
To read through all lines, use a Do Loop that continues until the AtEndOfStream property is True.
Below code snippet shows how to read through each line.

i=0
Do Until objTxtFile.AtEndOfStream
        sContent = objTxtFile.ReadLine
        sDataArray(i) = objTxtFile.ReadLine
        MsgBox sContent
        i = i+1
Loop

Content of each line can be stored in an array also.

5. Read(chr) 

 The Read method allows you to read only a specified number of characters.
Stores number of characters as defined in chr.

sContent = objTxtFile.Read(5)

6. Skip(chr) 

This method skips the number of characters defined in chr from the text file
skip(5)

7. SkipLine

This method skips line of data from the text file.
skipline

8. AtEndOfLine Property

 Returns true if the file pointer is positioned immediately before the end-of-line marker in a TextStream file. Can be useful to find characters in a line or reading characters till end of line.
Do While file.AtEndOfLine <> True 

9. Line property

gives the current line number of the textStream.
objTxtFile.line

10. Column Property

 Read-only property that returns the column number of the current character position in a TextStream file.
objTxtFile.Column       

11. OpenasTextStream Method

This method is used with getfile method to read from file.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTxtFile = objFSO.getFile(FilePath)
var objts = objTxtFile.OpenAsTextStream(iomode)

FileSystemObject - Manipulation of Files and Files Properties

This post shows what are the basic operation we can do on files using filesystemobject in VBScript. We will discuss on writing to a file and reading from file in next posts.

Below are the tasks we can perform on files using filesystemobject and the properties of file object through which we can extract useful information. 


Tasks Performed on files are as follows: 

1. Creating object for FSO.

2. Verify if file exists.

3. binding to existing file

4. Creating a new text file.

5. Copy or moving a file

6. Deleting a file



Tasks performed on Files
Tasks performed on Files

Properties of File Object
Properties of File Object

How to Work with Folders in QTP using filesystemObject

Below are the tasks which we can perform with folders and properties of folder object which can be used to manipulate folders.

  • Creating object for FSO
  • Verifying existing of an Object
  • Binding to an existing folder
  • Creating a new folder
  • Deleting a folder
  • Copying or moving folder.

Tasks for folder using filesystemobject
Tasks for folder using filesystemobject


Properties of Folder Object
Properties of Folder Object