Sie sind auf Seite 1von 11

FILE AND FILE SYSTEM CONTROLS

INTRODUCTION:

The file system controls helps the user in understanding the methodology of
using them for choosing disk files for his application. It also gives a detailed description
of accessing files in visual basic .A few windows features such as Clipboard is also
dealt with here.

There are three file system controls in visual basic .they are DirListBox,
FileListBox, and DriveListBox. In a application that allows to selecting files from drive
from drives these three controls are sued in combinations. These three controls having
Common properties and Methods:

Property Description
Enabled Specifies whether or not the user is allowed to interact with the
control.
List Array that stores the strings that appear in the control.
ListCount Number of items in the list property array.
ListIndex Index of selected list property item
MousePointer Specifies the shape of the mouse pointer
Visible Specifies whether or not the control is visible.

Methods:-
Drag Starts, Terminates, or Aborts Drag Operations.
Refresh Forces the control to repaint itself.
SetFocus Transfers the focus to the control.
Some FileListBox Properties:-

Property Description
Archive Specifies whether or not archive attributes are displayed
Hidden Specifies whether or not hidden attributes are displayed
MultiSelect Specifies whether or not the user can make multiple selections
Path Specifies the current path
Pattern Specifies the type of files displayed in the FileListBox
ReadOnly Specifies whether or not read-only attributes are displayed
System Specifies whether or not system or not system attributes are
displayed.

Data Hierarchy:-
The “Data” items processed by a computer are reduced to combinations of zeros and
ones. Such data item is called “Bit” such data in lower level means Decimal digits,
letters and characters.
A “Field” is a group of characters that conveys meaning.
Data items processed by computer form a data hierarchy in which data items becomes
more complex in structure as we progress form bits.
A “Record” is a composed of several fields (also called members) in a payroll system. A
Record is a group of related fields.
A “File” is a group of related records.
A “Record Key” identifies a record as belonging to a particular person or entity that is
unique from all other records in the file.
The “Records” which are typically stored in order by the record key field.
A group of related file is sometimes called a “DataBase”
A Collection of programs designed to create and manage databases is called a
“Database Management System”(DBMS).
The Below Example Shows Data Hierarchy.

File System Objects:-


The File System Object (FSO) provide the programmer with the ability to
manipulate files and drives. FSO also allow the programmer to read and write text to
sequence files.
FSO provides the programmer with power and flexibility for working with
files,folders and drives.
FSO’s are Objects in “Microsoft Scripting Runtime Library” which is added to
the current project with “References” dialog. Selecting the “project” menu’s
“References” command displays the modal “References” dialog.
ProjectReferencesSelect Microsoft Scripting Runtime.
There are 5 FSO’s
File System Object
File
Folder
Drive
TextStream
File System Objects (FSOs)
Object Type Description
FileSystemObject, Allows the programmer to interact with Files, Folders and
Drives
File Allows the programmer to Manipulate Files.

Folder Allows the programmer to Manipulate Folders.

Drive Allows the programmer to gather information about Drives.


Drive can be local or remote.
TextStream Allows the programmer to read and write text files.

FileSystemObject:-

“FileSystemObject” allows the programmer to interacts with Files,Folder,and


Drives. The programmer can use FileSystemObject to create directories,move files,
determine whether or not a Drive exists.
FileSystemObject Methods:-

Methods Description
CopyFile Copies an existing “File”
CopyFolder Copies an existing “Folder”
CreateFolder Creates and Returns Folder
CreateTextFile Creates and Returns File
DeleteFile Deletes a File
DeleteFolder Deletes a Folder
DriveExists Tests whether or not a Drive exists.
FileExists Tests whether or not File exists.
FolderExists Tests whether or not Folder exists.
GetAbsolutePathName Returns the absolute path as a String.
GetDrive Returns the specified Drive
GetDriveName Returns the Drive Drivename
GetFile Returns the specified File
GetFileName Returns the file filename
GetFolder Returns the specified Folder
GetParentFolderName Returns a String representing the parent folder name
GetTempName Creates and returns a String representing a file name
MoveFile Moves a File
MoveFolder Moves a Folder
OpenTextFile Opens an existing text File. Returns a TextStream.

Syntax:
Dim fso as New FileSystemObject
Dynamically creates “FileSystemObject” for fso using the keyword “New”.
The object now exists in memory like variable,objects have scope and fso is a module
variable.

File:-
Which allows the programmer to gather information about files, manipulate
files and open files.
File Properties & Methods:-
Properties/Methods Description
Properties:-
DateCreated The date the File was created
DateLastAccessed The date the File was last accessed
DateLastModified The date the File was last modified.
Drive The Drive where the file is located
Name The File Name
ParentFolder The File’s parent folder Name
Path The File’s path
ShortName The File’s Name expressed as a short name
Size The size of the File in bytes
Methods:-

Copy Copy this File


Delete Delete this File
Move Move this File
OpenAsTextStream Opens an existing File as a text File. Returns TextStream

Ex:-
Dim f as File
Set f= fso.GetFile(“d:\mpcs.txt”)

Folder:-
Folder objects allows the programmer to gather information about folder and to
manipulate folder.
Folder Properties & Methods:-
Properties/Methods Description
Properties:-
Attributes Value corresponding to folder’s attributes
DateCreated The date the Folder was created
DateLastAccessed The date the Folder was last accessed
DateLastModified The date the Folder was last modified.
Drive The Drive where the folder is located
IsRootFolder Indicates whether or not a Folder is the root folder
Name The Folder name
ParentFolder The Folder’s parent folder Name
Path The Folders’s path
ShortName The Folder’s Name expressed as a short name
ShortPath The Folder’s Name expressed as a short path
Size The size of the File in bytes
Type The Folder Type.

Methods:-

Delete Delete this Folder


Move Move this Folder
Copy Copy this Folder

Ex:-
Dim fld as Folder
Set fld= fso.GetFolder(path)

Drive:-
Drive Objects allows the programmer to gather information about drives.
Drive Properties:-
Properties Description
Properties:-
AvailableSpace The Amount of available Drive space in bytes
DriveLatter The Letter assigned the Drive
DriveType The Drive type
FileSystem The File system Drive Description.
FreeSpace Same as AvailableSpace
IsReady Indicates whether or not Drive is ready for use
Path The Drive’s path
RootFolder The Drive’s root folder
SerialNumber The Drive volume name

TextStream:-
Allows the programmer to read and write text files. When working with sequential files
the programmer uses the File System Object “TextStream” to read and write file data. A
Text Stream is a stream of characters that are read from a file or written to a file
TextStream Properties & Methods:-
Properties Description
Properties:-
AtEndOfLine Specifies if the position is at the end of the line
AtEndOfStream Specifies if the position is at the end of the stream
Column The current character’s position in a line
Line The current line Number

Methods:-

Close Closes the TextStream


Read Reads a specified number of charaters from the TextStream
ReadAll Reads the entire TextStream into a String
ReadLine Reads the Line from a TextStreamSkip
SkipLine Skips a specified number of TextStream characters
Write Writes a String to a TextStream
WriteBlankLines Writes a specified number of blank lines to a TextStream
WriteLine Writes a String followed by an end-of-line character to a TextStream

Accessing FILES
A file consists of series of related bytes laocated in a Disk. When an applicatioin
accesses a file, it must assume what the bytes are supposed to represent. Depending on
the kind of data the file contains, we can use the appropriate file access type. There are
three ways of accessing files in visual basic. They are as follows
Sequential access
Random access
Binary access
Creating a Sequential Access File:-
Sequential Access is designed for use with plain text files. Each character in the
file is assumed to represent either a text character or a text formatting sequence, such as
a new line character NL. Data is stored as ANSI characters. From a Sequential access
file, we can read the contents of that file only Sequentially.
Steps for creating Sequential Access File
To Create a Sequential access file , we use TextStreamObject, which is an Object of
FileSystemObjects.
The following program creates a sequential access as shown below.
Reading a Sequential Access File:-
Ex:
Updating a Sequential Access Files:-

Records and Random Access Files

Random Access Files:


A Random File composed of fixed length records. Data Can be inserted in a
random access file without destroying other data in the file. Data stored previously also
can be Updated or Deleted without rewriting the entire file.
A Random Access file has one of 3 Access types associated with it. An Access
type specifies which operation may be performed on a file. In an access type is not
explicitly given the default access type for the file is “ReadWrite”.

Access Type Description


Read Open the file in read-only mode
Write Open the file in write-only mode
ReadWrite Open the file for reading and writing.

Records as User-DefinedTypes :-

Visual Basic provides programmers with the ability to create “User Defined
Types”. User defined types are collections of related variables. Userdefined types may
contain variables of many different data types in constrast to arrays that contain only
elements of the same datatype. Userdefined datatype are commonly used to define
fixed length records to be stored in random access files.

User-Defined Type Definitions:-

User defined types are derived datatypes they are constructed using objects of
other types. Consider the following datatype definition.
Ex: Private Type Student
Sno as integer
Sname as String*10
Marks as String
End Sub
Keyword “Type” introduces the user defined definition. Identifier “Student” is
the Typename. The Type name is used to declare variables of the userdefined type.
“Type” definition must be “private” when declaring in form modules,but can be
declared as “public” or “Private” in standard code module or class module
Varibles declared in the “Type” definitions are called the “type’s member” members of
same Type must have unique names.
The preceding “Type” definition does not reserve any space in memory, rather the
definition creates a new datatype that is used to declare variables. “Type” Variables are
declared like variables of other types. The declaration is

Dim S1 as Student  declares one record


Dim S2(99) as Student  declares many records

Manipulating Members of User Defined Types:-


Members of a Type definition are accessed like properties using the dot(.)
Operator with a variable of the user defined type. For example to assign a value to the
“Sno“ member of One record from the previous “Student” declaration,
S1.Sno=100
Similarly S1.Sname=”Manohar” , S1.Marks=80.
Ex: Private Type Student
Sno as integer
Sname as String*10
Marks as String
End Sub
Private sub Form1_Load()
Dim S1 as Student
S1.Sno=10
S1.Sname=”Manohar”
S1.Marks=80
End Sub
Private sub Form1_Click()
Print “Sno=” & S1.Sno
Print “Sname=” & S1.Sname
Print “Marks=” & S1.Marks
End Sub
Creating a Random Access File:-
Random Access File is composed of a set of identical length records. You
can employ user defined types to create records made up of numerous fields each can
have different data types. Data is stored as binary information.
Random access file allows us to create files from which we can read
information randomly. For example if you had stored 10 student records in a file and
wish to read 5th record , we can directly retrive that particular record. This involves
moving to a particular location i.e moving to a byte address and reading a block of bytes
that correspond to a record.
The following statements open a file in random read and write mode

Open “d:\vndc.txt” for Random Access Read As #1 len=LenB(S1)


Open “d:\vndc.txt” for Random Access Wrti#1 len=LenB(S1)
When we opened the file in a required mode we had mentioned number of bytes
required to store a student variable using LenB() Function.
Ex:
Writing Data Randomly to Random Access File:-
Ex:
Reading Data Sequentially from a Random Access File:-
Ex:
Reading Randomly from a Random Access File:-
Ex:
Binary Access File:

Binary access file are accessed byte by byte.once a file is opened for bunary access we
can read from and writes to any location in the file. The ability to access any desired
byte in the file makes it the most flexible one. Before accessing a file in binary mode,
the file should be opened first for binary access.

Example:
A new project is opened and the Form of the saved as BIN.frm and the project file as
BIN.BAT
A ComandButton is added and the following code is entered in the Click event.

Private Sub Command1_Click()


Mystring = “this is a Binary Access file”

Filenum=FreeFile
Open “C:\ BINARY.Dat” For Binary As filenum

Put # filemnu, 100, mystring

End Sub
The application is executed and the click event of the CommandButton creates the
file BINARY.Dat .The Put statement takes three parameters. The first parameter is the
file number, the second is the byte location where the writing starts and the third is the
names of the variables whose contents will be written into the file.
Interface with Windows:

Visual Basic is a Windows programming language and hence let us discuss about a
few Windows Features such as Clipboard and the various keyboard events for detecting
whether the key was pressed or released
Example:

A new Standard EXE project is opened and a TextBox is drawn on the Form.
The MultiLine property of the TextBox is set to True and Its Left and Top properties
to 0(zero).
Menu items are added to the Form
Caption Name
&File mnFile
…. Cu&t muCut
…..Copy muPaste
……&Paste muPaste

The following code entered in the Form_Resize() procedure is executed


automatically when the Form pops up
Private Sub Form _Resize()
Text1.Width = Form1.ScaleWidth
Text2.Height = Form1.ScaleHeight
End sub

The TextBox fills the entire area of the Form. This is done by changing the Width and
Height properties of the TextBox to ScaleWidth and ScaleHeight property of the Form
The following code is entered in the mnuCut_Click ()
Private sub mnuCut_Click()
Clipboard. Clear
Clipboard.SetText Text1.SelText
Text1.SetText = “ “
End sub
The following code is entered in the mnuCopy_Click () procedure.
It copy the highlighted text in to the Clipboard.

Private sunb mnuCopy_Click()


Clipboard. Clear
Clipboard.SelText Text1.SetText
End sub
The following code entered in the mnuPaste_click() procedure replaces the highlighted
area of the TextBox with the contents of the Clipboard.

Private sub mnuPaste_Click()


Text1.SelText = Clipboard.GetText()
End sub
Then the project by press f5 function key . the project is excuted work with the forms
MENUS.
Note: see the example programs in class notes.

Das könnte Ihnen auch gefallen