FileSystemObject class

FileSystemObject is a class whose members are the procedures of the FileSystemTools module, for code written for the FileSystemObject of the Scripting Runtime and for hosts that need an object and not a module. Each member calls the procedure of the same name in the module, with the same defaults, so the entries of FileSystemTools describe them in full. This page gives each member’s syntax and says where it differs from the module.

Code written in twinBASIC can call the module directly, with no object. The class exists so that a line such as Dim fso As New FileSystemObject and the calls that follow it keep working when the Scripting Runtime is replaced.

Dim fso As New FileSystemObject
Dim work As String
work = fso.BuildPath(Environ$("TEMP"), fso.GetTempName("fst", ""))
fso.CreateFolder work

Dim ts As TextStream
Set ts = fso.CreateTextFile(fso.BuildPath(work, "a.txt"))
ts.WriteLine "hello"
ts.Close

Debug.Print fso.FileExists(fso.BuildPath(work, "a.txt"))     ' True
Debug.Print fso.GetFile(fso.BuildPath(work, "a.txt")).Size   ' 7
Debug.Print fso.Drives.Count > 0                             ' True

fso.DeleteFolder work

Differences from the module

  • Some arguments have the names that the Scripting Runtime gives them, so named arguments in ported code keep working: fileSpec, folderSpec and driveSpec where the module has filePath, folderPath and drivePath; path in CreateFolder; fileName in CreateTextFile, GetFileVersion and OpenTextFile. The syntax lines below show the names of the class.
  • MoveFile, MoveFolder and Rename are procedures that return nothing, as in the Scripting Runtime. The module’s return the full path of the result.
  • Drives is the module’s GetDrives.
  • Nothing else differs.

Porting code from the Scripting Runtime

Replace CreateObject("Scripting.FileSystemObject") with New FileSystemObject, and check these calls. The package differs from the Scripting Runtime in them, and a program compiles with the old code but does not do the same:

  • GetSpecialFolder returns the path as a String, not a Folder. Code that reads .Path from the result does not compile (the sample after this list).
  • The third argument of CreateTextFile and of Folder.CreateTextFile is an encoding. The Scripting Runtime’s Unicode Boolean True is -1 here, which writes UTF-8, and not UTF-16. The default is UTF-8 without a BOM, where the Scripting Runtime writes ANSI.
  • The fourth argument of OpenTextFile and the second of File.OpenAsTextStream are an encoding, not a Tristate. TristateTrue is -1, which is encAutoDetect.
  • The other differences, such as the added members and the changed defaults, are listed under Differences from the Scripting Runtime.
Dim fso As New FileSystemObject
Debug.Print fso.GetSpecialFolder(sfTemporaryFolder).Path

Files and folders

CopyFile

Syntax: object.CopyFile ( source, destination [, overwrite ] )

See CopyFile.

CopyFolder

Syntax: object.CopyFolder ( source, destination [, overwrite ] )

See CopyFolder.

CreateFolder

Syntax: object.CreateFolder ( path [, createIntermediates ] ) As Folder

path is folderPath in the module. See CreateFolder.

DeleteFile

Syntax: object.DeleteFile ( fileSpec [, force [, ignoreMissing ] ] )

fileSpec is filePath in the module. See DeleteFile.

DeleteFolder

Syntax: object.DeleteFolder ( folderSpec [, force [, ignoreMissing ] ] )

folderSpec is folderPath in the module. See DeleteFolder, including its warning about links.

GetFilePaths

Syntax: object.GetFilePaths ( folderPath [, pattern [, includeHidden [, includeSystem [, recursive ] ] ] ] ) As Collection

See GetFilePaths.

MoveFile

Syntax: object.MoveFile ( source, destination )

A procedure, with no result. See MoveFile.

MoveFolder

Syntax: object.MoveFolder ( source, destination )

A procedure, with no result. See MoveFolder.

Rename

Syntax: object.Rename ( source, newName )

A procedure, with no result. See Rename.

Paths

BuildPath

Syntax: object.BuildPath ( path, name ) As String

See BuildPath.

CleanFileName

Syntax: object.CleanFileName ( fileName [, replaceChar ] ) As String

See CleanFileName.

GetAbsolutePathName

Syntax: object.GetAbsolutePathName ( path [, baseDirectory [, useLongPathPrefix ] ] ) As String

See GetAbsolutePathName.

GetBaseName

Syntax: object.GetBaseName ( path ) As String

See GetBaseName.

GetDriveName

Syntax: object.GetDriveName ( path ) As String

See GetDriveName.

GetExtensionName

Syntax: object.GetExtensionName ( path ) As String

See GetExtensionName.

GetFileName

Syntax: object.GetFileName ( path ) As String

See GetFileName.

GetParentFolderName

Syntax: object.GetParentFolderName ( path ) As String

See GetParentFolderName.

GetRelativePath

Syntax: object.GetRelativePath ( basePath, targetPath [, basePathIsFolder [, trimLeadingDotSlash ] ] ) As String

See GetRelativePath.

GetTempName

Syntax: object.GetTempName ( [ prefix [, extension [, inFolder ] ] ] ) As String

See GetTempName.

Existence

DriveExists

Syntax: object.DriveExists ( driveSpec ) As Boolean

driveSpec is drivePath in the module. See DriveExists.

FileExists

Syntax: object.FileExists ( fileSpec ) As Boolean

fileSpec is filePath in the module. See FileExists.

FolderExists

Syntax: object.FolderExists ( folderSpec ) As Boolean

folderSpec is folderPath in the module. See FolderExists.

Current directory and special folders

GetCurrentDir

Syntax: object.GetCurrentDir ( ) As String

See GetCurrentDir.

GetSpecialFolder

Syntax: object.GetSpecialFolder ( specialFolder ) As String

Returns a path, not a Folder object as the Scripting Runtime does. See GetSpecialFolder.

SetCurrentDir

Syntax: object.SetCurrentDir ( folderPath )

See SetCurrentDir.

File information

GetFileEncoding

Syntax: object.GetFileEncoding ( filePath [, sampleBytes ] ) As TextEncoding

See GetFileEncoding.

GetFileLineEnding

Syntax: object.GetFileLineEnding ( filePath [, encoding ] ) As NewlineType

See GetFileLineEnding.

GetFileType

Syntax: object.GetFileType ( filePath ) As String

See GetFileType.

GetFileVersion

Syntax: object.GetFileVersion ( fileName [, source ] ) As String

fileName is filePath in the module. See GetFileVersion.

Objects

Drives

Syntax: object.Drives ( ) As Drives

Returns the Drives collection of all drives. It is a function, called with or without parentheses: fso.Drives.Count and For Each d In fso.Drives both work. In the module it is GetDrives.

GetDrive

Syntax: object.GetDrive ( driveSpec ) As Drive

driveSpec is drivePath in the module. See GetDrive.

GetFile

Syntax: object.GetFile ( filePath ) As File

See GetFile.

GetFolder

Syntax: object.GetFolder ( folderPath ) As Folder

See GetFolder.

Whole files

ArrayToTextFile

Syntax: object.ArrayToTextFile ( lines(), filePath [, encoding [, lineEnding ] ] )

See ArrayToTextFile.

MergeTextFiles

Syntax: object.MergeTextFiles ( firstFilePath, secondFilePath [, outputFilePath [, insertBlankLineBetween [, firstFileEncoding [, secondFileEncoding ] ] ] ] )

See MergeTextFiles.

NormalizeTextFile

Syntax: object.NormalizeTextFile ( filePath [, toEncoding [, toNewLine [, fromEncoding [, toAttributes [, skipIfUnchanged ] ] ] ] ] ) As Boolean

See NormalizeTextFile.

ReadStream

Syntax: object.ReadStream ( filePath, stream() [, maxBytesToRead ] )

See ReadStream.

StringToTextFile

Syntax: object.StringToTextFile ( text, filePath [, encoding [, outLineEnding ] ] )

See StringToTextFile.

TextFileToArray

Syntax: object.TextFileToArray ( filePath [, encoding ] ) As String()

See TextFileToArray.

TextFileToString

Syntax: object.TextFileToString ( filePath [, encoding [, outLineEnding ] ] ) As String

See TextFileToString.

WriteStream

Syntax: object.WriteStream ( stream(), filePath )

See WriteStream.

Text streams

CreateTextFile

Syntax: object.CreateTextFile ( fileName [, overwrite [, encoding [, lineEnding ] ] ] ) As TextStream

fileName is filePath in the module. The third argument is an encoding, not the Scripting Runtime’s Unicode Boolean. See CreateTextFile.

GetStandardStream

Syntax: object.GetStandardStream ( standardStreamType [, unicode ] ) As TextStream

See GetStandardStream.

OpenTextFile

Syntax: object.OpenTextFile ( fileName [, ioMode [, create [, encoding [, writeModeLineEnding ] ] ] ] ) As TextStream

fileName is filePath in the module. The fourth argument is an encoding, not a Tristate. See OpenTextFile.

See Also

  • FileSystemTools module – the procedures in full, and the enumerations
  • File class, Folder class, Drive class and TextStream class – the objects that the members return
  • tbFileSysTools package – how the package differs from the Scripting Runtime’s FileSystemObject