# FileSystemObject class
{: .no_toc }

**FileSystemObject** is a class whose members are the procedures of the [**FileSystemTools**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md) 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**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md) 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.

```tb check_build check_run
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**](#movefile), [**MoveFolder**](#movefolder) and [**Rename**](#rename) are procedures that return nothing, as in the Scripting Runtime. The module's return the full path of the result.
- [**Drives**](#drives) is the module's [**GetDrives**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#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:

- The third argument of [**CreateTextFile**](#createtextfile) and of [**Folder.CreateTextFile**](/tB/Packages/Contributed/tbFileSysTools/Folder.md#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**](#opentextfile) and the second of [**File.OpenAsTextStream**](/tB/Packages/Contributed/tbFileSysTools/File.md#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](/tB/Packages/Contributed/tbFileSysTools/index.md#differences-from-the-scripting-runtime).

## Files and folders

### CopyFile
{: .no_toc }

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

See [**CopyFile**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#copyfile).

### CopyFolder
{: .no_toc }

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

See [**CopyFolder**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#copyfolder).

### CreateFolder
{: .no_toc }

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

*path* is *folderPath* in the module. See [**CreateFolder**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#createfolder).

### DeleteFile
{: .no_toc }

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

*fileSpec* is *filePath* in the module. See [**DeleteFile**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#deletefile).

### DeleteFolder
{: .no_toc }

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

*folderSpec* is *folderPath* in the module. See [**DeleteFolder**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#deletefolder), including its note about links.

### GetFilePaths
{: .no_toc }

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

See [**GetFilePaths**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getfilepaths).

### MoveFile
{: .no_toc }

Syntax: *object*.**MoveFile** ( *source*, *destination* )

A procedure, with no result. See [**MoveFile**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#movefile).

### MoveFolder
{: .no_toc }

Syntax: *object*.**MoveFolder** ( *source*, *destination* )

A procedure, with no result. See [**MoveFolder**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#movefolder).

### Rename
{: .no_toc }

Syntax: *object*.**Rename** ( *source*, *newName* )

A procedure, with no result. See [**Rename**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#rename).

## Paths

### BuildPath
{: .no_toc }

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

See [**BuildPath**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#buildpath).

### CleanFileName
{: .no_toc }

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

See [**CleanFileName**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#cleanfilename).

### GetAbsolutePathName
{: .no_toc }

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

See [**GetAbsolutePathName**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getabsolutepathname).

### GetBaseName
{: .no_toc }

Syntax: *object*.**GetBaseName** ( *path* ) **As String**

See [**GetBaseName**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getbasename).

### GetDriveName
{: .no_toc }

Syntax: *object*.**GetDriveName** ( *path* ) **As String**

See [**GetDriveName**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getdrivename).

### GetExtensionName
{: .no_toc }

Syntax: *object*.**GetExtensionName** ( *path* ) **As String**

See [**GetExtensionName**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getextensionname).

### GetFileName
{: .no_toc }

Syntax: *object*.**GetFileName** ( *path* ) **As String**

See [**GetFileName**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getfilename).

### GetParentFolderName
{: .no_toc }

Syntax: *object*.**GetParentFolderName** ( *path* ) **As String**

See [**GetParentFolderName**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getparentfoldername).

### GetRelativePath
{: .no_toc }

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

See [**GetRelativePath**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getrelativepath).

### GetTempName
{: .no_toc }

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

See [**GetTempName**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#gettempname).

## Existence

### DriveExists
{: .no_toc }

Syntax: *object*.**DriveExists** ( *driveSpec* ) **As Boolean**

*driveSpec* is *drivePath* in the module. See [**DriveExists**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#driveexists).

### FileExists
{: .no_toc }

Syntax: *object*.**FileExists** ( *fileSpec* ) **As Boolean**

*fileSpec* is *filePath* in the module. See [**FileExists**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#fileexists).

### FolderExists
{: .no_toc }

Syntax: *object*.**FolderExists** ( *folderSpec* ) **As Boolean**

*folderSpec* is *folderPath* in the module. See [**FolderExists**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#folderexists).

## Current directory and special folders

### GetCurrentDir
{: .no_toc }

Syntax: *object*.**GetCurrentDir** ( ) **As String**

See [**GetCurrentDir**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getcurrentdir).

### GetSpecialFolder
{: .no_toc }

Syntax: *object*.**GetSpecialFolder** ( *specialFolder* ) **As Folder**

Returns a [**Folder**](/tB/Packages/Contributed/tbFileSysTools/Folder.md) object, as the Scripting Runtime does. See [**GetSpecialFolder**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getspecialfolder).

### GetSpecialFolderPath
{: .no_toc }

Syntax: *object*.**GetSpecialFolderPath** ( *specialFolder* ) **As String**

Returns the path as a **String**, for a folder that may not exist. The Scripting Runtime has no such member. See [**GetSpecialFolderPath**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getspecialfolderpath).

### SetCurrentDir
{: .no_toc }

Syntax: *object*.**SetCurrentDir** ( *folderPath* )

See [**SetCurrentDir**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#setcurrentdir).

## File information

### GetFileEncoding
{: .no_toc }

Syntax: *object*.**GetFileEncoding** ( *filePath* ) **As TextEncoding**

See [**GetFileEncoding**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getfileencoding).

### GetFileLineEnding
{: .no_toc }

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

See [**GetFileLineEnding**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getfilelineending).

### GetFileType
{: .no_toc }

Syntax: *object*.**GetFileType** ( *filePath* ) **As String**

See [**GetFileType**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getfiletype).

### GetFileVersion
{: .no_toc }

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

*fileName* is *filePath* in the module. See [**GetFileVersion**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getfileversion).

### IsFileLocked
{: .no_toc }

Syntax: *object*.**IsFileLocked** ( *filePath* ) **As Boolean**

See [**IsFileLocked**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#isfilelocked).

## Objects

### Drives
{: .no_toc }

Syntax: *object*.**Drives** ( ) **As Drives**

Returns the [**Drives**](/tB/Packages/Contributed/tbFileSysTools/Drives.md) 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**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getdrives).

### GetDrive
{: .no_toc }

Syntax: *object*.**GetDrive** ( *driveSpec* ) **As Drive**

*driveSpec* is *drivePath* in the module. See [**GetDrive**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getdrive).

### GetFile
{: .no_toc }

Syntax: *object*.**GetFile** ( *filePath* ) **As File**

See [**GetFile**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getfile).

### GetFolder
{: .no_toc }

Syntax: *object*.**GetFolder** ( *folderPath* ) **As Folder**

See [**GetFolder**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getfolder).

## Whole files

### ArrayToTextFile
{: .no_toc }

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

See [**ArrayToTextFile**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#arraytotextfile).

### MergeTextFiles
{: .no_toc }

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

See [**MergeTextFiles**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#mergetextfiles).

### NormalizeTextFile
{: .no_toc }

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

See [**NormalizeTextFile**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#normalizetextfile).

### ReadStream
{: .no_toc }

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

See [**ReadStream**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#readstream).

### StringToTextFile
{: .no_toc }

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

See [**StringToTextFile**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#stringtotextfile).

### TextFileToArray
{: .no_toc }

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

See [**TextFileToArray**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#textfiletoarray).

### TextFileToString
{: .no_toc }

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

See [**TextFileToString**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#textfiletostring).

### WriteStream
{: .no_toc }

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

See [**WriteStream**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#writestream).

## Searching text

```tb check_build check_run
Dim fso As New FileSystemObject
Dim work As String, hits As Collection
work = fso.BuildPath(Environ$("TEMP"), fso.GetTempName("fst", ""))
fso.CreateFolder work
fso.StringToTextFile "find me", fso.BuildPath(work, "a.txt")
fso.StringToTextFile "other", fso.BuildPath(work, "b.txt")

Debug.Print fso.FileContainsText(fso.BuildPath(work, "a.txt"), "FIND")   ' True
Set hits = fso.FindInTextFiles(work, "find")
Debug.Print hits.Count                                                   ' 1
Debug.Print fso.IsFileLocked(fso.BuildPath(work, "a.txt"))               ' False

fso.DeleteFolder work
```

### FileContainsText
{: .no_toc }

Syntax: *object*.**FileContainsText** ( *filePath*, *searchText* [, *ignoreCase* ] ) **As Boolean**

See [**FileContainsText**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#filecontainstext).

### FindInTextFiles
{: .no_toc }

Syntax: *object*.**FindInTextFiles** ( *folderPath*, *searchText* [, *filePattern* [, *recursive* [, *ignoreCase* [, *fileSizeCapInMb* ] ] ] ] ) **As Collection**

See [**FindInTextFiles**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#findintextfiles).

## Text streams

### CreateTextFile
{: .no_toc }

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**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#createtextfile).

### GetStandardStream
{: .no_toc }

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

See [**GetStandardStream**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#getstandardstream).

### OpenTextFile
{: .no_toc }

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**](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md#opentextfile).

## See Also

- [FileSystemTools](/tB/Packages/Contributed/tbFileSysTools/FileSystemTools.md) module -- the procedures in full, and the enumerations
- [File](/tB/Packages/Contributed/tbFileSysTools/File.md) class, [Folder](/tB/Packages/Contributed/tbFileSysTools/Folder.md) class, [Drive](/tB/Packages/Contributed/tbFileSysTools/Drive.md) class and [TextStream](/tB/Packages/Contributed/tbFileSysTools/TextStream.md) class -- the objects that the members return
- [tbFileSysTools](/tB/Packages/Contributed/tbFileSysTools/index.md) package -- how the package differs from the Scripting Runtime's **FileSystemObject**
