FileSystemObject


File System Operations


File System Object Model:

The File System Object (FSO) model provides an object-based tool for working with folders and files. It allows us to use the familiar object. method syntax with a rich set of properties, methods, and events to process folders and files. We can also employ the traditional Visual Basic statements and commands.

The FSO model gives our application the ability to create, alter, move, and delete folders, or to determine if and where particular folders exist. It also enables us to get information about folders, such as their names and the date they were created or last modified.

The FSO model makes processing files much easier as well. When processing files, our primary goal is to store data in an efficient, easy-to-access format. We need to be able to create files, insert and change the data, and output (read) the data. Although we can store data in a database, doing so adds a significant amount of overhead to our application. We may not want to have such overhead, or our data access requirements may not call for the extra functionality associated with a full-featured database. In this case, storing our data in a text file or binary file is the most efficient solution.

The FSO model, contained in the Scripting type library (Scrrun.dll), supports the creation and manipulation of text files through the TextStream object; however, the FSO model does not support binary files. To manipulate binary files, use the FileOpen Function with the Binary keyword.

FileSystemObject

Main object. Contains methods and properties that allow you to create, delete, gain information about, and generally manipulate drives, folders, and files. Many of the methods associated with this object duplicate those in other FSO objects; they are provided for convenience.

Drive Object

This Object Contains methods and properties that allow you to gather information about a drive attached to the system, such as its share name and how much room is available. Note that a "drive" isn't necessarily a hard disk, but can be a CD-ROM drive, a RAM disk, and so forth. A drive doesn't need to be physically attached to the system; it can be also be logically connected through a network.


Folder Object

This Object contains methods and properties that allow you to create, delete, or move folders. Also allows you to query the system for folder names, paths, and various other properties.

TextStream Object

This Object allows you to read and write text files.

Examples:

1) Read Content from a text file and Write in another text file
 
Dim objFso, myFile1, myFile2, x
Set objFso=createobject("Scripting.Filesystemobject")
Set myFile1=objFso.opentextfile("C:\Documents and Settings\gcr\Desktop\gcreddy.txt",1)
Set myFile2=objFso.createtextfile("C:\Documents and Settings\gcr\Desktop\gcrqtp.txt",1)

Do Until myFile1.AtEndOfStream=True
x=myFile1.Readline
myFile2.Writeline x
Loop

myFile1.Close
myFile2.Close

Set objFso=Nothing

2) Read Numbers only from a text file and export to another text file

Dim objFso,
objTextStream, myText, x
set objFso=createobject("Scripting.Filesystemobject")
set objTextStream=objFso.opentextfile("C:\Documents and Settings\gcr\Desktop\gcreddy.txt",1)
Set objTextStream2=objFso.opentextfile("C:\Documents and Settings\gcr\Desktop\gcrqtp.txt",2)
objTextStream2.WriteLine "Numbers"
objTextStream2.WriteLine "---------"

Do Until objTextStream.AtEndOfStream
myText=objTextStream.ReadLine
x=split(myText," ")

For i=lbound(x) to ubound(x)
If isnumeric(x(i)) =True Then

objTextStream2.WriteLine x(i)
End if
Next
Loop

objTextStream.Close
objTextStream2.Close
Set objFso=Nothing 


3) 'Count Lines in a Text File
Dim objFso, objTextstream, LineCount
Set objFso=CreateObject("Scripting.filesystemobject")
Set objTextstream=objFso.OpenTextFile("C:\Documents and Settings\gcr\Desktop\abc.txt")

Do While Not objTextstream.AtEndOfStream
       LineCount = LineCount + 1
        objTextstream.ReadLine
  Loop
  Msgbox LineCount

objTextstream.Close
Set objTextstream=Nothing
Set objFso=Nothing

 


For More Examples Visit:



0 comments:

Post a Comment