# FileSystem module

The **FileSystem** module groups together the procedures and statements for working with files and directories on disk. Its members divide cleanly into two camps: *pathname-based* operations that act on something named in the filesystem (creating and deleting files and directories, querying their attributes, iterating over a directory listing), and *file-number-based* operations that act on a handle previously returned by the **Open** statement (reading, positioning, formatting, and tracking the channel).

## Navigating directories

[**ChDrive**](/tB/Modules/FileSystem/ChDrive.md) changes the current drive, [**ChDir**](/tB/Modules/FileSystem/ChDir.md) changes the current directory on a given drive, and [**CurDir**](/tB/Modules/FileSystem/CurDir.md) returns the path of the current drive --- or of any other drive, if one is named. [**MkDir**](/tB/Modules/FileSystem/MkDir.md) and [**RmDir**](/tB/Modules/FileSystem/RmDir.md) create and remove directories.

```tb check_build
ChDrive "D"
ChDir "D:\Projects"
Debug.Print CurDir              ' "D:\Projects"
MkDir "D:\Projects\Output"
```

## Inspecting files and directories

[**Dir**](/tB/Modules/FileSystem/Dir.md) is the wildcard matcher: pass it a pathname containing `*` or `?` and it returns the first matching name, then call it again with no arguments to step through subsequent matches until it returns `""`. [**FileLen**](/tB/Modules/FileSystem/FileLen.md) returns the size of a file in bytes without opening it, and [**FileDateTime**](/tB/Modules/FileSystem/FileDateTime.md) returns its last-modified timestamp. [**GetAttr**](/tB/Modules/FileSystem/GetAttr.md) and [**SetAttr**](/tB/Modules/FileSystem/SetAttr.md) read and write the [**VbFileAttribute**](/tB/Modules/Constants/VbFileAttribute.md) flag bits --- read-only, hidden, system, archive --- and **GetAttr** also reports whether a name refers to a directory by setting the **vbDirectory** bit.

```tb check_build
Dim Name As String
Name = Dir("C:\Logs\*.log")
Do While Name <> ""
    Debug.Print Name & vbTab & FileLen("C:\Logs\" & Name)
    Name = Dir
Loop
```

## Copying and deleting

[**FileCopy**](/tB/Modules/FileSystem/FileCopy.md) copies one file to another, and [**Kill**](/tB/Modules/FileSystem/Kill.md) deletes files matching a wildcard pattern. Both operate by pathname. **Kill** raises a run-time error when asked to delete a file that is open, and **FileCopy** raises one for a file that is open with write access.

```tb check_build
FileCopy "C:\Data\report.xlsx", "C:\Backup\report.xlsx"
Kill "C:\Backup\*.tmp"
```

## Opening and tracking file numbers

The lower-level read/write statements --- **Open**, **Close**, **Get**, **Put**, **Print**, **Write**, **Input**, and **Line Input** --- work in terms of a *file number* in the range 1--511. [**FreeFile**](/tB/Modules/FileSystem/FreeFile.md) returns the next number that isn't currently in use, sparing the caller from picking one by hand and racing other code to it. Once a file is open, [**FileAttr**](/tB/Modules/FileSystem/FileAttr.md) reports the access mode --- **Input**, **Output**, **Random**, **Append**, or **Binary** --- that the file number was opened with. [**Reset**](/tB/Modules/FileSystem/Reset.md) closes every file number currently open and flushes its buffers, and is most useful as a last-ditch cleanup before exit.

```tb check_build
Dim N As Long
N = FreeFile
Open "C:\Data\report.txt" For Input As #N
' ... read ...
Close #N
```

## Position within an open file

For an open file number, [**EOF**](/tB/Modules/FileSystem/EOF.md) returns **True** once a sequential read has run past the last record, [**LOF**](/tB/Modules/FileSystem/LOF.md) returns the file's total length in bytes, and [**Loc**](/tB/Modules/FileSystem/Loc.md) returns the current read/write position. The unit of *position* depends on the open mode --- record number for **Random**, byte offset for **Binary**, and the byte position divided by 128 for sequential modes --- so the per-mode tables on each function's page are the authoritative reference. [**Seek**](/tB/Modules/FileSystem/Seek.md) doubles as a function and a statement: the function returns the position of the **next** read or write (whereas **Loc** reports the position of the *last*), and the statement repositions the file pointer ahead of the next operation.

```tb check_build
Dim N As Long, Line As String
N = FreeFile
Open "C:\Data\big.log" For Input As #N
Do While Not EOF(N)
    Line Input #N, Line
Loop
Close #N
```

## Reading and formatting through open file numbers

[**Input**](/tB/Modules/FileSystem/Input.md) and [**Input$**](/tB/Modules/FileSystem/Input.md) return a fixed number of characters read from a file number opened with **Open**, as a **Variant** or a **String** respectively; [**InputB**](/tB/Modules/FileSystem/InputB.md) and [**InputB$**](/tB/Modules/FileSystem/InputB.md) are their byte-oriented counterparts, counting raw bytes rather than UTF-16 characters. They differ from the **Input #** statement in that they return every character they read --- commas, newlines, quotation marks, leading spaces, and all --- making them the right choice when the bytes on disk are not a stream of comma-delimited values.

[**Width**](/tB/Modules/FileSystem/Width.md) sets the output line width on a sequential output channel: subsequent **Print #** wraps to a new line once the chosen number of characters has been written, or never wraps at all when *Width* is `0`.

## Members

- [ChDir](/tB/Modules/FileSystem/ChDir.md) -- changes the current directory or folder
- [ChDrive](/tB/Modules/FileSystem/ChDrive.md) -- changes the current drive
- [CurDir](/tB/Modules/FileSystem/CurDir.md) -- returns the current path
- [Dir](/tB/Modules/FileSystem/Dir.md) -- returns the name of a file, directory, folder, or volume label that matches a pattern
- [EOF](/tB/Modules/FileSystem/EOF.md) -- returns whether the end of a file opened for **Random** or sequential **Input** has been reached
- [FileAttr](/tB/Modules/FileSystem/FileAttr.md) -- returns the file mode for files opened with the **Open** statement
- [FileCopy](/tB/Modules/FileSystem/FileCopy.md) -- copies a file
- [FileDateTime](/tB/Modules/FileSystem/FileDateTime.md) -- returns the date and time when a file was created or last modified
- [FileLen](/tB/Modules/FileSystem/FileLen.md) -- returns the length of a file in bytes
- [FreeFile](/tB/Modules/FileSystem/FreeFile.md) -- returns the next file number available for use by the **Open** statement
- [GetAttr](/tB/Modules/FileSystem/GetAttr.md) -- returns the attributes of a file or directory
- [Input, Input$](/tB/Modules/FileSystem/Input.md) -- reads a fixed number of characters from an open sequential file
- [InputB, InputB$](/tB/Modules/FileSystem/InputB.md) -- reads a fixed number of bytes from an open sequential file
- [Kill](/tB/Modules/FileSystem/Kill.md) -- deletes files from a disk
- [Loc](/tB/Modules/FileSystem/Loc.md) -- returns the current read/write position within an open file
- [LOF](/tB/Modules/FileSystem/LOF.md) -- returns the size, in bytes, of an open file
- [MkDir](/tB/Modules/FileSystem/MkDir.md) -- creates a new directory or folder
- [Reset](/tB/Modules/FileSystem/Reset.md) -- closes all disk files opened by using the **Open** statement
- [RmDir](/tB/Modules/FileSystem/RmDir.md) -- removes an existing directory or folder
- [Seek](/tB/Modules/FileSystem/Seek.md) -- returns or sets the read/write position within an open file
- [SetAttr](/tB/Modules/FileSystem/SetAttr.md) -- sets attribute information for a file
- [Width](/tB/Modules/FileSystem/Width.md) -- sets the line width for a sequential output file
