Sunday, May 29, 2011

Working With Text File

The File System object allows us to work with files and folders .We can read, write, create, and delete files and folders using the File System object and the objects that are associated with it.


Creation of Text File:
 
Three ways to create an empty text file:



1.CreateText File method


2.OpenTextFile method of the FileSystemObject object with the ForWriting flag set.


3.OpenAsTextStream method with the ForWriting flag set.

CreateTextFile Method

Creates a specified file name and returns a TextStream object that can be used to read from or write to the file. 
Example:



Const ForReading = 1, ForWriting = 2, ForAppending = 8


Dim fso, f


Set fso = CreateObject("Scripting.FileSystemObject")


Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)


f.Write "Hello world!“


f.Close

Syntax:



For Moving a File:


Object.Move(“Destination path”)


For Copying a File:


Object.Copy(“Destination path”)


For Deleting a File:


Object.Delete

Set MyFile = fso.GetFile("c:\testfile.txt")



MyFile.Move(c:\tmp\testfile.txt)


MyFile.Copy (“c:\QTP\testfile.txt “)


Set f2=fso.Getfile(“C:\QTP\testfile.txt”)


f2.Delete

There are 3 methods to read a file.



Read Method: Read a specified number of characters from a file.


ReadAll Method: Read the entire contents of a text.


ReadLine Method:Read an entire line (up to, but not including, the newline character).

Reads a specified number of characters from a TextStream file and returns the resulting string.  
Write Method
 
Example:



Function WriteToFile


Const ForReading = 1, ForWriting = 2


Dim fso, f


Set fso = CreateObject("Scripting.FileSystemObject")


Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)


f.Write "Hello world!"


WriteLine Method
 
Writes a specified string and newline character to a TextStream file.



Syntax :


object.WriteLine([string])
 
Example:



Const ForReading = 1, ForWriting = 2


Dim fso, f


Set fso = CreateObject("Scripting.FileSystemObject")


Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)


f.WriteLine "Hello world!“


f.WriteLine "VBScript is fun!"

Writes a specified number of newline characters to a TextStream file.


Syntax: object.WriteBlankLines(lines)

WriteBlankLines Method

Example:


Const ForReading = 1, ForWriting = 2

Dim fso, f

Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)

f.WriteBlankLines 2

f.WriteLine "Hello World!"


Syntax: object.Read(characters)

Const ForReading = 1, ForWriting = 2, ForAppending = 8


Dim fso, f

Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)

Read Method

f.Write "Hello world!"


Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)

s= f.Read(5)

Msgbox s

ReadAll Method

Reads an entire TextStream file and returns the resulting string.


Syntax :object.ReadAll( );

Readline Method
 
Example:


Dim fso, f1, ts, s

Const ForReading = 1

Set fso = CreateObject("Scripting.FileSystemObject")

Set f1 = fso.CreateTextFile("c:\testfile.txt", True)

Write Method

Writes a specified string to a TextStream file.


Syntax :

object.Write(string)


Syntax:

object.CreateTextFile(filename[, overwrite[, unicode]])

Object-> Name of a FileSystemObject or Folder object

Filename ->String expression that identifies the file to create.

Overwrite-> “True” if the file can be overwritten and “False” otherwise

Unicode-> value is “True” if the file is created as a Unicode file and “False” if the file is created as a ASCII file.


Example:

Dim fso, f1

Set fso = CreateObject("Scripting.FileSystemObject")

Set f1 = fso.CreateTextFile("c:\testfile.txt", True)

OpenTextFile method

Opens a Specified file and returns a Textstream object that can be used to read from,write to or append to the file.


Syntax: object.OpenasTextStream([iomode, [format]])

Object – Name of the File object

Iomode-Optional (Can be For Reading=1,For Writing=2 ,For Appending=8)

Format: Optional. Used to indicate the format of the opened file.


TristateUseDefault – 2

Tristate True –1

Tristate False 0

0 opens the file as ASCII and –1 opens as Unicode and –2 uses System default.

Example:


Const ForReading = 1, ForWriting = 2, ForAppending = 8

Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

Dim fso, f, ts

Set fso = CreateObject("Scripting.FileSystemObject") fso.CreateTextFile "test1.txt" ' Create a file.

Set f = fso.GetFile("test1.txt")


Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)

ts.Write "Hello World"

ts.Close

FileMethod

Syntax:


object.OpenTextFile(filename[, iomode[, create[, format]]])

Object: Always the name of a FileSystemObject.

filename :String expression that identifies the file to open.

iomode :Optional. Can be one of three constants: ForReading, ForWriting, or ForAppending
 
Create: Optional. Boolean value that indicateas whether a new file can be created if the specified filename doesn't exist. The value is True if a new file is created, False if it isn't created. If omitted, a new file isn't created.

No comments:

Post a Comment