The FSO (File System Object) model enables VB to process drives and create, alter, move, delete, and detect folders and files.
In VB there are 3 basic ways to process drives and create, alter, move, delete, and detect folders and files:
This section will focus on the FSO from the VB6 and VBS perspective.
FSO can perform powerful tasks usually done via the OS (operating system):
FSO is contained in the Scripting type library (Scrrun.Dll), so VB actually uses the
library of VBS! Only 1 instance of the FSO can exist at a time.
Different ways of instantiating an FSO object.
Dim fso As New FileSystemObject
'For VB only.
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
'For VB or VBS.
var fso;
fso = Server.CreateObject("Scripting.FileSystemObject")
//For JavaScript or JScript.
var fso;
fso = new ActiveXObject("Scripting.FileSystemObject")
//For JavaScript or JScript.
Regarding drives. If you use the CurDir() function, the ChDrive and ChDir
statements, or the Path property (App.Path), be aware that they may
return a UNC path (EG: \\Server\Share) rather than a drive path (EG:
E:\Folder).Here are common tasks done with folders:
| Task | Method or Property |
|---|---|
| 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 if 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 folders. | FileSystemObject.GetSpecialFolder() |
3 ways to create an empty text file (aka a text stream):
Set f1 = fso.CreateTextFile("c:\testfile.txt", True) 'VB/VBS
f1 = fso.CreateTextFile("c:\\testfile.txt", true); //JS
'VB/VBS Const ForWriting = 2 Set ts = fso.OpenTextFile("c:\test.txt", ForWriting, True) //JS var ForWriting= 2; ts = fso.OpenTextFile("c:\\test.txt", ForWriting, true);
'VB/VBS Const ForWriting = 2 fso.CreateTextFile ("c:\test1.txt") Set f1 = fso.GetFile("c:\test1.txt") Set ts = f1.OpenAsTextStream(ForWriting, True) //JS var ForWriting = 2; fso.CreateTextFile ("c:\\test1.txt"); f1 = fso.GetFile("c:\\test1.txt"); ts = f1.OpenAsTextStream(ForWriting, true);
How to add data to a file.
'VB/VBS ts.WriteLine("Testing 1, 2, 3.") ts.WriteBlankLines(3) ts.Write ("This is a test.") ts.Close //JS ts.WriteLine("Testing 1, 2, 3.") ; ts.WriteBlankLines(3) ; ts.Write ("This is a test."); ts.Close();
Read files. Methods of textstream objects are used: Read(nChars), ReadLine(),
ReadAll(), Skip(nChars), SkipLine().
'VB/VBS Const ForReading = 1 Set ts = fso.OpenTextFile("c:\testfile.txt", ForReading) s = ts.ReadLine ts.Close //JS var ForReading = 1; ts = fso.OpenTextFile("c:\\testfile.txt", ForReading); s = ts.ReadLine(); ts.Close();
Move, copy, or delete files. The following methods are available:
| Task | Method |
|---|---|
| Move a file | File.Move() or FileSystemObject.MoveFile() |
| Copy a file | File.Copy() or FileSystemObject.CopyFile() |
| Delete a file | File.Delete() or FileSystemObject.DeleteFile() |
Get parent directory and filename. Oddly enough this can't be done using only FSO -- you have to use ASP too.
//VB/VBS PathFldrFile = Server.MapPath(Request.ServerVariables("PATH_INFO")) PathFldr = fso.GetParentFolderName(PathFldrFile) File = fso.GetFileName(PathFldrFile) //JS PathFldrFile = Server.MapPath(Request.ServerVariables("PATH_INFO")); PathFldr = fso.GetParentFolderName(PathFldrFile); File = fso.GetFileName(PathFldrFile);
Here are the properties, methods, etc. of the objects in the FSO object model.
There are 3 collections: Drives, Folders, and Files. They
all have the Count and Item properties but Folders also has
the Add() method.
| FileSystemObject | |
|---|---|
| Member | Member |
| Drives | GetAbsolutePathName(pathspec) |
| BuildPath(path,strName) | GetBaseName(path) |
| CopyFile(strSource, strDestination[, blnOverwrite]) | GetDrive(drivespec) |
| CopyFolder(strSource, strDestination[, blnOverwrite]) | GetDriveName(path) |
| CreateFolder(strFoldername) | GetExtensionName(path) |
| CreateTextFile(strFilename[, blnOverwrite[, blnUnicode]]) | GetFile(filespec) |
| DeleteFile(filespec[, blnForce]) | GetFileName(pathspec) |
| DeleteFolder(filespec[, blnForce]) | GetFileVersion(pathspec) |
| DriveExists(drivespec) | GetFolder(folderspec) |
| FileExists(filespec) | GetParentFolderName(path) |
| FolderExists(folderspec) | GetSpecialFolder(folderspec) |
| MoveFile(strSource, strDestination) | GetStandardStream(standardStreamType [, blnUnicode]) |
| MoveFolder(strSource, strDestination) | GetTempName() |
| OpenTextFile(strFilename[, iomode[, blnCreate[, format]]]) | |
| Drive | |
|---|---|
| Member | Member |
| AvailableSpace | Path |
| DriveLetter | RootFolder |
| DriveType | SerialNumber |
| FileSystem | ShareName |
| FreeSpace | TotalSize |
| IsReady | VolumeName |
| Folder | |
|---|---|
| Member | Member |
| Attributes | Move(destination) |
| IsRootFolder | Name |
| Files | ParentFolder |
| Drive | Path |
| Delete(force) | ShortName |
| DateLastModified | ShortPath |
| DateLastAccessed | Size |
| DateCreated | SubFolders |
| Copy(destination[, blnOverwrite]) | Type |
| CreateTextFile(strFilename[, blnOverwrite[, blnUnicode]]) | |
| File | |
|---|---|
| Member | Member |
| Attributes | Name |
| Copy(destination[, blnOverwrite]) | OpenAsTextStream([iomode, [format]]) |
| DateCreated | ParentFolder |
| DateLastAccessed | Path |
| DateLastModified | ShortName |
| Delete(force) | ShortPath |
| Drive | Size |
| Move(destination) | Type |
| TextStream | |
|---|---|
| Member | Member |
| AtEndOfLine | Skip(intCharacters) |
| AtEndOfStream | SkipLine() |
| Close() | Write(string) |
| Column | WriteBlankLines(intLines) |
| Line | WriteLine([string]) |
| Read(intCharacters) | |
| ReadAll() | |
| ReadLine() | |
Here are links that lead to off-site pages about the FSO and VB.
Page Modified: (Hand noted: 2007-10-03 18:23:07Z) (Auto noted: 2009-12-02 16:27:05Z)