Thursday, August 4, 2011

File System Statement : FileSystemObject

Purpose :
The FileSystemObject object allows you to work with drives, folders and files programmatically just as you can in the windows explorer interactively. you can copy, move and create file and folder; get information about the drives , folders and files; and so on. FileSystemObject gives you direct access to the windows explorer functionality with better performance than the standard file system access in visual basic and see the object list.


Object of the FileSystemObject Model

Drive : Gathers information about drives attached to the system, such as how much room is available.
Folder : Creates , deletes , or moves folders , plus queries the system as to their names pates and so on.
Files : Creates , deletes or moves files , pluse queries the system as to their names, paths and so on.
FileSystemObject : Creates, deletes , gains information about and generally manipulates drives folders and files . this is he main object of the group.
TextStream : Reads and writes text files.

  • Use the create object method, or dimension a variable as a File System object object to create a File system object object
  • Use to appropriate method on the newly created object.
  • Access the object’s properties.
Creating a File System Object 
Dimension a variable as type FileSystemObject object.
Dim fso As New FileSystemObject
Use the CreateObject nethod to create a FileSystemObject object
Set fso = CreateObject ( “ Scripting.FileSystemObject “ )

Getting Drive information:
- Total size of the drive in bytes ( TotalSize property )
- How much space is available on the drive in bytes ( AvailableSpace and FreeSpace properties )
- What letter is assigned to the drive ( DriveLetter property )
- What type of drive it is, such as removable, fixed, network, CD-ROM, or RAM disk ( DriveType property )
- Drive’s serial number ( SerialNumber property )
- Type of file system the drive use, such as FAT, FAT32, NTFS and so on ( FileSystem property )
- Whether a drive is available for use (IsReady property)
- Name of the share and /or volume ( ShareName and VolumeName properties )
- Path or root folder of the drive (path and RootFolder properties)

Project>References>Microsoft Scripting Runtime 
Option Explicit
Private Sub Command1_Click()
Dim fso As New FileSystemObject, drv As Drive, s As String
Dim t As String
Set drv = fso.GetDrive(fso.GetDriveName ( " c : " ) )
s = "Drive " & drv.DriveLetter & " . "
s = s & "Total Space : " & FormatNumber(drv.TotalSize / 1024, 0 )
s = s & " kb " & vbCrLf
s = s & drv.VolumeName & vbCrLf
s = s & " Free Space : " & FormatNumber(drv.FreeSpace / 1024, 0 )
s = s & " kb " & vbCrLf
'
Select Case drv.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 = s & t & " Drive " & vbCrLf
s = s & drv.SerialNumber & vbCrLf
s = s & drv.FileSystem & vbCrLf
s = s & drv.Path & vbCrLf
s = s & drv.RootFolder & vbCrLf
MsgBox s
End Sub

Download Vb Program


Working with Folder

-Create a folder : FileSystemObject.CreateFolder
-Delete a folder : Folder.Delete or FileSystemObject.DeleteFolder
-Move a folder : Folder.Move or FileSystemObject.MoveFolder
-Copy a folder : Folder.Copy or FileSystemObject.CopyFolder
-Retrieve the name of a folder : Folder.Name
-Find out whether a folder exists on a drive : FileSystemObject.FolderExists
-Get an instance of an existing folder object : FileSystemObject.GetFolder
-Find out the name of a folder’s parent folder : FileSystemObject.GetParentFolderName
-Find out the Path of System folder : FileSystemObject.GetSpecialFolder


Option Explicit
'Project>References>Microsoft Scripting Runtime
Private Sub Command1_Click()
Dim fso As New FileSystemObject, fldr As Folder, s As String
Set fldr = fso.GetFolder ( " c:\a\b " )
Debug.Print "Folder Name : " & fldr.Name
Debug.Print "Contained on drive : " & fldr.Drive
If fldr.IsRootFolder = True Then
Debug.Print " This folder is a root folder"
Else
Debug.Print " This folder isn't a root folder "
End If
Debug.Print fldr.ParentFolder
Debug.Print fldr.Path
Debug.Print FormatNumber(fldr.Size / 1024, 0) & " KB "
Debug.Print fldr.DateCreated & " Folder Created Date "
Debug.Print fldr.DateLastModified & " Folder Last Modified Date "
Debug.Print fldr.DateLastAccessed & " Folder Last Accessed Date "
End Sub

Private Sub Command2_Click()
Dim fso As New FileSystemObject
fso.CreateFolder ( " c:\1 " )
Debug.Print "Basename = " & fso.GetBaseName( " c:\1 " )
fso.DeleteFolder ( " c:\1 " )
End Sub


.
Working with Files
You can work with files in Visual Basic by the new using the new object-oriented FSO object. Such as Copy, Delete, Move, and open As Text Stream; others; or by using the older existing Functions, such as open, close, file Copy, Get Attar, and so on. Note that you can move copy Or delete files regardless of their file type. There are two major a categories of file manipulation.
  • Creating adding, or removing data; and reading files
  • Moving, copying and deleting
Using the File System object to create a file or manipulate the data contained in an Existing is as simple as using the correct method.

Copy File : Copies one or more file from location to another
Delete File : Deletes a specified file
Move File : Move one or more files from one location to another
OpensTextStream : Open a specified file and returns a TextStrem object that can be Used to read from, write to, or append to the file
Read Line : Reads an entire line (up to, but not including, the newline character) From a Tex Stream file and return s the resulting
ReadAll : Reads an entire Text stream file and returns the resulting string
WrightBlankLine : Writes a specified number of newline characters to a text stream file
Write Line : Writes a specified number of newline character to a Texts tram file
Create Text File : Creates a specified filename and returns a Text Stream object that can Be used to read from or write to file.


Option Explicit
'here i am created folder "a" in C:\ drive
'and under folder "a" also created folder "b"

Private Sub Command1_Click()
Dim fso As New FileSystemObject, txtfile As TextStream
Set txtfile = fso.CreateTextFile( "c:\testfile.txt", True )
txtfile.Write ( "This is a test. " )
txtfile.WriteLine ( " Texting 1, 2, 3." )
txtfile.WriteBlankLines (3)
txtfile.Close
End Sub

Private Sub Command2_Click()
Dim fso As New FileSystemObject
Dim fil1 As File, ts As TextStream
Dim s As String
Set fil1 = fso.GetFile( "c:\testfile.txt" )
'
Set ts = fil1.OpenAsTextStream(ForReading)
s = ts.ReadLine
MsgBox s
ts.Close
'
Set ts = fil1.OpenAsTextStream(ForWriting)
ts.Write ("Hello Samrudhi")
ts.WriteLine (s)
ts.Close
'
Set ts = fil1.OpenAsTextStream(ForReading)
s = ts.ReadLine
MsgBox s
ts.Close
End Sub

Private Sub Command3_Click()
Dim fso As New FileSystemObject, ts As TextStream
Dim fil1 As File, fil2 As File
Set fil1 = fso.GetFile( "c:\testfile.txt" )
fil1.Move ( "c:\a\testfile.txt" )
MsgBox "File moving in folder C:\a "
fil1.Copy ( "c:\a\b\testfile.txt" )
MsgBox "File copy in folder C:\a\b "
Set fil1 = fso.GetFile ( "c:\a\testfile.txt" )
Set fil2 = fso.GetFile ( "c:\a\b\testfile.txt" )
fil1.Delete
fil2.Delete
MsgBox " both file is delete"
End Sub

Download Vb Program


.

No comments:

Post a Comment