by d1rtyw0rm (2 Submissions)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (6 Votes)
Here a description about FSO(File System Object) and howto use it
Active X: Microsoft Scripting RunTime
FSO is there to help coder to work on file and directory.
The FSO model got 5 object
Drive - Folder - File - FileSystemObject - TextStream
Drive Object
Total Size : Disk Dimension
AvailableSizeFreeSpace : To get Free space of a disk
DriveLetter : Give the drive letter
DriveType : Give the type of a drive(fix,net,CD-Rom,etc...)
SerialNumber : give the serial number of a disk
FileSystem : NTFS,Fat,Fat32 etc.
IsReady : return True if drive is ready
VolumeName : ...
Exemple of use
Dim fso as new filesystemobject
dim d as drive
set d = fso.getdrive("c:") 'Creation of the drive object
msgbox d.totalsize
msgbox d.filesystem
Exemple of what the code will return 13gb, NTFS
FileSystemObject Object
CreateFolder : Create a directory
FolderExist : Return True if folder exist
GetParentFolder : Return parent name directory
Exemple of use
Dim fso as New FileSystemObject
fso.CreateFolder("C:\test\x")
msgbox fso.FolderExist("C:\test")
msgbox fso.GetParentFolderName("C:\test\bbb")
This example will first create the folder C:\test\x, after the first msgbox will return true cause we previously create the directory, the last msgbox will return 'C:\test'.
Folder Object
Allows to manage the repertories
Delete : Delete a folder
Move : Move a folder
Copy : Copy a folder
Name : Name of the folder
Exemple of use
dim fso as new filesystemobject
dim r as folder
set r = fso.GetFolder("c:\odsource")
msgbox r.name
r.copy "D:\"
r.delete
First we create the folder object, the msgbox return "odsource", after it copy the folder on D:\ drive, and delete the folder on c:\odsource.
File Object
Allows to obtain information on a file
Attributes : Return the file attribute (read only,hidden etc.)
Copy : Copy a file
DataCreated : Return creation date
DateLAstModified : Return date of the last modification
Delete : Delete a file
Drive : return the drive where the file is
Move :allow to move the file
Name : return the file name
ParentFolder : return the parent folder name of the file
Path : Return the full file path(include the file name)
ShortNAme : return the short name of the file
Size : File dimention
Type : return file type
Exemple of use
dim fsoas new FileSystemObject
Dim f as File
Set f = fso.GetFile
("c:\d1rtyw0rm\visualbasicforum.com")
msgBox f.type
msgbox f.DateCreated
msgbox f.Path
The first msgbox return "COM", second "11-08-2002", third "C:\d1rtyw0rm\visualbasicforum.com"
TextStream Object
Allow to manage a text file. Binary file cannot be managed by FSO.
Write : Write a text line without crlf
Write Line : Write a text line with crlf
WriteBlankLines : Write a number of crlf
Close : Close the file
Read : Reads a specific number of character
ReadLine : Read a complete line
ReadAll : Read complete file
Exemple of use
Dim fso As New FileSystemObject
Dim ts as TextStream
Dim s As String
'Creation of the text file'
Set ts = fso.OpenTextFile("C:\vbf\dirtyworm.txt", ForWriting,True)
ts.WriteLine "d1rtyw0rm rul'Z"
ts.Write "http:\\www.d1rtyw"
ts.Write "0rm.ca.tc"
ts.BlankLines(4)
ts.WriteLine "odsource.com"
ts.Close
'Open for adding'
Set ts = fso.OpenTextFile("c:\odsource\dirtyworm.txt",ForAppending)
ts.WriteLine "This line will be under odsource.com"
ts.close
'File Reading'
Set ts = fso.OpenTextFile("C:\odsource\dirtyworm.txt", ForReading)
s = ts.Read(12)
MsgBox s 'Will print d1rtyw0rm ru'
s = ts.ReadLine 'Read the rest of the line'
s = ts.ReadAll
MsgBox s
ts.Close
-d1rtyw0rm