Beruflich Dokumente
Kultur Dokumente
Ch−¬ng 5
Xö lý tÖp tin
Trong ch−¬ng nµy chóng ta sÏ xem xÐt mét sè thao t¸c c¬ b¶n víi c¸c
tÖp tin trong VB, nh− c¸c thao t¸c: ghi néi dung tÖp lªn ®Üa, ®äc tÖp tõ ®Üa,
xem danh s¸ch tÖp ...
NÕu ch−a cã s½n tham chiÕu ®Õn ®èi t−îng FSO, tõ menu Project chän
Refrence, hép tho¹i Refrence xuÊt hiÖn h·y ®¸nh dÊu vµo môc Microsort
Scripting Runtime, råi chän OK.
5.1.1. T¹o ®èi t−îng FileSystemObject
Cã thÓ t¹o b»ng hai c¸ch:
• Khai b¸o:
Dim fso as New FileSystemObject
• Dïng ph−¬ng thøc CreateObject
Set fso = CreateObject(“Scripting.FileSystemObject”)
46
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
47
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
SerialNumber Sè serial
FileSystem FAT, FAT32, NTFS ...
IsReady æ cã sö dông ®−îc kh«ng.
ShareName, Tªn chia sÎ trong m¹ng
VolumeName
Path, Th− môc gèc
RootFolder
VÝ dô:
Dim fso As New FileSystemObject, drv As Drive
Set drv = fso.GetDrive(fso.GetDriveName)
MsgBox “Total Size = “ & str(drv.TotalSize)
48
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
VÝ dô:
Dim fso As New FileSystemObject, fld As Folder
Set fld = fso.GetFolder(“C:”)
Print “Drive is: “ & fld.Drive
Print “Name folder is: “&fld.Name
Thªm d÷ liÖu vµo tÖp: Sau khi ®· t¹o tÖp, ®Ó thªm d÷ liÖu vµo tÖp ta lµm
theo c¸c b−íc sau:
• Më tÖp cÇn thªm d÷ liÖu: OpenTextFile
• Ghi d÷ liÖu: Write hoÆc WriteLine hoÆc WriteBlankLines
• §ãng tÖp: Close
49
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
VÝ dô:
50
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
51
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
Width = 1335
End
Begin VB.CommandButton cmdExit
Caption = "&Exit"
Height = 495
Left = 4200
TabIndex = 3
Top = 2520
Width = 1335
End
Begin VB.CommandButton cmDoc_File
Caption = "&Read File"
Height = 495
Left = 4200
TabIndex = 2
Top = 1320
Width = 1335
End
Begin VB.CommandButton cmdNhap_DL
Caption = "&Input Data"
Height = 495
Left = 4200
TabIndex = 1
Top = 720
Width = 1335
End
Begin VB.CommandButton cmdTao_File
Caption = "&Create File"
Height = 495
Left = 4200
TabIndex = 0
Top = 120
Width = 1335
End
Begin VB.Label Label3
AutoSize = -1 'True
Caption = "FSO"
BeginProperty Font
Name = "Arial"
Size = 36
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF00FF&
52
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
Height = 840
Left = 1440
TabIndex = 6
Top = 1800
Width = 1470
End
Begin VB.Label Label2
AutoSize = -1 'True
Caption = "FileSystemObject"
BeginProperty Font
Name = ".VnAristote"
Size = 27.75
Charset = 0
Weight = 500
Underline = 0 'False
Italic = -1 'True
Strikethrough = 0 'False
EndProperty
ForeColor = &H000000FF&
Height = 555
Left = 240
TabIndex = 5
Top = 1080
Width = 3735
End
Begin VB.Label Label1
AutoSize = -1 'True
BackColor = &H00C0C0C0&
Caption = "M« h×nh"
BeginProperty Font
Name = ".VnAristote"
Size = 27.75
Charset = 0
Weight = 500
Underline = 0 'False
Italic = -1 'True
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 555
Left = 1200
TabIndex = 4
Top = 360
Width = 1815
End
End
Attribute VB_Name = "frmFSO"
53
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
54
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
55
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
Put X X
Type ... End Type X
Write X
56
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
57
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
End
End
Attribute VB_Name = "frmFile"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim FileNum As Integer
58
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
Type HOSO
Ho_ten As String*25
Tuoi As Integer
Que_quan As String*30
He_so_luong As Double
End Type
Dim Nguoi As HOSO
VÝ dô:
Giao diÖn
59
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
VERSION 5.00
Begin VB.Form FrmPhone
ClientHeight = 2790
ClientLeft = 60
ClientTop = 345
ClientWidth = 6420
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 2790
ScaleWidth = 6420
StartUpPosition = 2 'CenterScreen
Begin VB.CommandButton cmdExit
Caption = "Exit"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 5040
TabIndex = 6
ToolTipText = "Tho¸t ra"
Top = 2280
Width = 1215
End
Begin VB.CommandButton cmdPrevious
Caption = "Previous"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
60
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 5040
TabIndex = 5
ToolTipText = "Trë l¹i b¶n ghi tr−íc"
Top = 1680
Width = 1215
End
Begin VB.CommandButton cmdNext
Caption = "Next"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 5040
TabIndex = 4
ToolTipText = "Sang b¶n ghi tiÕp"
Top = 1080
Width = 1215
End
Begin VB.CommandButton cmdNew
Caption = "New"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 5040
TabIndex = 3
ToolTipText = "Thªm b¶n ghi míi"
Top = 480
Width = 1215
End
61
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
62
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
Strikethrough = 0 'False
EndProperty
Height = 285
Left = 840
MaxLength = 40
TabIndex = 0
Top = 240
Width = 3735
End
Begin VB.Label lblName
Caption = "Name:"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 120
TabIndex = 9
Top = 240
Width = 615
End
Begin VB.Label lblPhone
Caption = "Phone:"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 120
TabIndex = 8
Top = 720
Width = 615
End
Begin VB.Label lblComment
Caption = "Comments:"
BeginProperty Font
Name = "MS Sans Serif"
63
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 120
TabIndex = 7
Top = 1200
Width = 1215
End
End
Attribute VB_Name = "FrmPhone"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
64
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
65
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
If LastRecord = 0 Then
LastRecord = 1
End If
ShowCurrentRecord
End Sub
Chó ý: Kh«ng thÓ truy cËp ngÉu nhiªn víi tÖp nhÞ ph©n, mµ ph¶i truy
nhËp tuÇn tù theo chiÒu dµi cña mçi mÈu tin.
66
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
Chóng ta cã thÓ kÕt hîp c¸c ®iÒu khiÓn nµy ®Ó t¹o ra nh÷ng hép tho¹i
®iÒu khiÓn tÖp tin thuËn tiÖn cho ng−êi sö dông.
VÝ dô:
67
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
Sau ®©y lµ vÝ dô chän mét tÖp tin cho biÕt dung l−¬ng cña nã, theo thiÕt
kÕ m« t¶ ë trªn.
M· tr×nh:
Option Explicit
Dim i, n As Long
68
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
size = Str(FileLen(dd))
MsgBox "Size of the File " & Text2.Text & " is "
& size & " Bytes.", , "Open"
Else
MsgBox "You not select a Filename !", , "Open"
End If
End Sub
69
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
70
§¹i häc Th¸i Nguyªn – Tr−êng §HSP – Khoa To¸n -Tin
LËp tr×nh trªn Windows víi Visual Basic
71