@tauri-apps/plugin-fs
Access the file system.
Security
This module prevents path traversal, not allowing absolute paths or parent dir components (i.e. “/usr/path/to/file” or “../path/to/file” paths are not allowed). Paths accessed with this API must be relative to one of the base directories so if you need access to arbitrary filesystem paths, you must write such logic on the core layer instead.
The API has a scope configuration that forces you to restrict the paths that can be accessed using glob patterns.
The scope configuration is an array of glob patterns describing folder paths that are allowed.
For instance, this scope configuration only allows accessing files on the
databases folder of the $APPDATA
directory:
{ "plugins": { "fs": { "scope": ["$APPDATA/databases/*"] } }}
Notice the use of the $APPDATA
variable. The value is injected at runtime, resolving to the app data directory.
The available variables are:
$APPCONFIG
,
$APPDATA
,
$APPLOCALDATA
,
$APPCACHE
,
$APPLOG
,
$AUDIO
,
$CACHE
,
$CONFIG
,
$DATA
,
$LOCALDATA
,
$DESKTOP
,
$DOCUMENT
,
$DOWNLOAD
,
$EXE
,
$FONT
,
$HOME
,
$PICTURE
,
$PUBLIC
,
$RUNTIME
,
$TEMPLATE
,
$VIDEO
,
$RESOURCE
,
$TEMP
.
Trying to execute any API with a URL not configured on the scope results in a promise rejection due to denied access.
Note that this scope applies to all APIs on this module.
Enumerations
BaseDirectory
Since
2.0.0
Enumeration Members
AppCache
AppCache: 16;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:20
AppConfig
AppConfig: 13;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:17
AppData
AppData: 14;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:18
AppLocalData
AppLocalData: 15;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:19
AppLog
AppLog: 17;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:21
Audio
Audio: 1;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:5
Cache
Cache: 2;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:6
Config
Config: 3;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:7
Data
Data: 4;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:8
Desktop
Desktop: 18;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:22
Document
Document: 6;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:10
Download
Download: 7;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:11
Executable
Executable: 19;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:23
Font
Font: 20;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:24
Home
Home: 21;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:25
LocalData
LocalData: 5;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:9
Picture
Picture: 8;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:12
Public
Public: 9;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:13
Resource
Resource: 11;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:15
Runtime
Runtime: 22;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:26
Temp
Temp: 12;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:16
Template
Template: 23;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:27
Video
Video: 10;
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/path.d.ts:14
SeekMode
Enumeration Members
Current
Current: 1;
Source: plugins/fs/guest-js/index.ts:69
End
End: 2;
Source: plugins/fs/guest-js/index.ts:70
Start
Start: 0;
Source: plugins/fs/guest-js/index.ts:68
Classes
FileHandle
The Tauri abstraction for reading and writing files.
Since
2.0.0
Extends
Resource
Constructors
constructor()
new FileHandle(rid): FileHandle
Parameters
Parameter | Type |
---|---|
rid | number |
Returns
Overrides
Resource.constructor
Source: plugins/fs/guest-js/index.ts:252
Properties
Property | Type |
---|---|
private #private | any |
Accessors
rid
get rid(): number
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/core.d.ts:123
Inherited from
Resource.rid
Methods
close()
close(): Promise< void >
Destroys and cleans up this resource from memory. You should not call any method on this object anymore and should drop any reference to it.
Returns
Promise
< void
>
Inherited from
Resource.close
Source: node_modules/.pnpm/@tauri-apps+api@2.0.0-alpha.13/node_modules/@tauri-apps/api/core.d.ts:129
read()
read(buffer): Promise< null | number >
Reads up to p.byteLength
bytes into p
. It resolves to the number of
bytes read (0
< n
<= p.byteLength
) and rejects if any error
encountered. Even if read()
resolves to n
< p.byteLength
, it may
use all of p
as scratch space during the call. If some data is
available but not p.byteLength
bytes, read()
conventionally resolves
to what is available instead of waiting for more.
When read()
encounters end-of-file condition, it resolves to EOF
(null
).
When read()
encounters an error, it rejects with an error.
Callers should always process the n
> 0
bytes returned before
considering the EOF (null
). Doing so correctly handles I/O errors that
happen after reading some bytes and also both of the allowed EOF
behaviors.
Example
import { open, read, close, BaseDirectory } from '@tauri-apps/plugin-fs';// if "$APP/foo/bar.txt" contains the text "hello world":const file = await open('foo/bar.txt', { dir: BaseDirectory.App });const buf = new Uint8Array(100);const numberOfBytesRead = await file.read(buf); // 11 bytesconst text = new TextDecoder().decode(buf); // "hello world"await close(file.rid);
Since
2.0.0
Parameters
Parameter | Type |
---|---|
buffer | Uint8Array |
Returns
Promise
< null
| number
>
Source: plugins/fs/guest-js/index.ts:287
seek()
seek(offset, whence): Promise< number >
Seek sets the offset for the next read()
or write()
to offset,
interpreted according to whence
: Start
means relative to the
start of the file, Current
means relative to the current offset,
and End
means relative to the end. Seek resolves to the new offset
relative to the start of the file.
Seeking to an offset before the start of the file is an error. Seeking to any positive offset is legal, but the behavior of subsequent I/O operations on the underlying object is implementation-dependent. It returns the number of cursor position.
Example
import { open, seek, write, SeekMode, BaseDirectory } from '@tauri-apps/plugin-fs';
// Given hello.txt pointing to file with "Hello world", which is 11 bytes long:const file = await open('hello.txt', { read: true, write: true, truncate: true, create: true, dir: BaseDirectory.App,});await file.write(new TextEncoder().encode('Hello world'), { dir: BaseDirectory.App });
// Seek 6 bytes from the start of the fileconsole.log(await file.seek(6, SeekMode.Start)); // "6"// Seek 2 more bytes from the current positionconsole.log(await file.seek(2, SeekMode.Current)); // "8"// Seek backwards 2 bytes from the end of the fileconsole.log(await file.seek(-2, SeekMode.End)); // "9" (e.g. 11-2)
Since
2.0.0
Parameters
Parameter | Type |
---|---|
offset | number |
whence | SeekMode |
Returns
Promise
< number
>
Source: plugins/fs/guest-js/index.ts:332
stat()
stat(): Promise< FileInfo >
Returns a FileInfo
for this file.
Example
import { open, fstat, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await open('file.txt', { read: true, dir: BaseDirectory.App });const fileInfo = await fstat(file.rid);console.log(fileInfo.isFile); // true
Since
2.0.0
Returns
Source: plugins/fs/guest-js/index.ts:353
truncate()
truncate(len?): Promise< void >
Truncates or extends this file, to reach the specified len
.
If len
is not specified then the entire file contents are truncated.
Example
import { ftruncate, open, write, read, BaseDirectory } from '@tauri-apps/plugin-fs';
// truncate the entire fileconst file = await open('my_file.txt', { read: true, write: true, create: true, dir: BaseDirectory.App,});await ftruncate(file.rid);
// truncate part of the fileconst file = await open('my_file.txt', { read: true, write: true, create: true, dir: BaseDirectory.App,});await write(file.rid, new TextEncoder().encode('Hello World'));await ftruncate(file.rid, 7);const data = new Uint8Array(32);await read(file.rid, data);console.log(new TextDecoder().decode(data)); // Hello W
Since
2.0.0
Parameters
Parameter | Type |
---|---|
len ? | number |
Returns
Promise
< void
>
Source: plugins/fs/guest-js/index.ts:384
write()
write(data): Promise< number >
Writes p.byteLength
bytes from p
to the underlying data stream. It
resolves to the number of bytes written from p
(0
<= n
<=
p.byteLength
) or reject with the error encountered that caused the
write to stop early. write()
must reject with a non-null error if
would resolve to n
< p.byteLength
. write()
must not modify the
slice data, even temporarily.
Example
import { open, write, close, BaseDirectory } from '@tauri-apps/plugin-fs';const encoder = new TextEncoder();const data = encoder.encode('Hello world');const file = await open('bar.txt', { write: true, dir: BaseDirectory.App });const bytesWritten = await write(file.rid, data); // 11await close(file.rid);
Since
2.0.0
Parameters
Parameter | Type |
---|---|
data | Uint8Array |
Returns
Promise
< number
>
Source: plugins/fs/guest-js/index.ts:411
Interfaces
CopyFileOptions
Since
2.0.0
Properties
Property | Type | Description |
---|---|---|
fromPathBaseDir ? | BaseDirectory | Base directory for fromPath . |
toPathBaseDir ? | BaseDirectory | Base directory for toPath . |
CreateOptions
Since
2.0.0
Properties
Property | Type | Description |
---|---|---|
baseDir ? | BaseDirectory | Base directory for path |
DebouncedWatchOptions
Since
2.0.0
Extends
Properties
Property | Type | Description |
---|---|---|
baseDir ? | BaseDirectory | Base directory for path |
delayMs ? | number | Debounce delay |
recursive ? | boolean | Watch a directory recursively |
DirEntry
A disk entry which is either a file, a directory or a symlink.
This is the result of the readDir
.
Since
2.0.0
Properties
ExistsOptions
Since
2.0.0
Properties
Property | Type | Description |
---|---|---|
baseDir ? | BaseDirectory | Base directory for path . |
FileInfo
A FileInfo describes a file and is returned by stat
, lstat
or fstat
.
Since
2.0.0
Properties
Property | Type | Description |
---|---|---|
atime | null | Date | The last access time of the file. This corresponds to the atime field from stat on Unix and ftLastAccessTime on Windows. This may notbe available on all platforms. |
birthtime | null | Date | The creation time of the file. This corresponds to the birthtime field from stat on Mac/BSD and ftCreationTime on Windows. This maynot be available on all platforms. |
blksize | null | number | Blocksize for filesystem I/O. #### Platform-specific - Windows: Unsupported. |
blocks | null | number | Number of blocks allocated to the file, in 512-byte units. #### Platform-specific - Windows: Unsupported. |
dev | null | number | ID of the device containing the file. #### Platform-specific - Windows: Unsupported. |
fileAttributes | null | number | This field contains the file system attribute information for a file or directory. For possible values and their descriptions, see File Attribute Constants in the Windows Dev Center #### Platform-specific - macOS / Linux / Android / iOS: Unsupported. |
gid | null | number | Group ID of the owner of this file. #### Platform-specific - Windows: Unsupported. |
ino | null | number | Inode number. #### Platform-specific - Windows: Unsupported. |
isDirectory | boolean | True if this is info for a regular directory. Mutually exclusive toFileInfo.isFile and FileInfo.isSymlink . |
isFile | boolean | True if this is info for a regular file. Mutually exclusive toFileInfo.isDirectory and FileInfo.isSymlink . |
isSymlink | boolean | True if this is info for a symlink. Mutually exclusive toFileInfo.isFile and FileInfo.isDirectory . |
mode | null | number | The underlying raw st_mode bits that contain the standard Unixpermissions for this file/directory. #### Platform-specific - Windows: Unsupported. |
mtime | null | Date | The last modification time of the file. This corresponds to the mtime field from stat on Linux/Mac OS and ftLastWriteTime on Windows. Thismay not be available on all platforms. |
nlink | null | number | Number of hard links pointing to this file. #### Platform-specific - Windows: Unsupported. |
rdev | null | number | Device ID of this file. #### Platform-specific - Windows: Unsupported. |
readonly | boolean | Whether this is a readonly (unwritable) file. |
size | number | The size of the file, in bytes. |
uid | null | number | User ID of the owner of this file. #### Platform-specific - Windows: Unsupported. |
MkdirOptions
Since
2.0.0
Properties
Property | Type | Description |
---|---|---|
baseDir ? | BaseDirectory | Base directory for path |
mode ? | number | Permissions to use when creating the directory (defaults to 0o777 , before the process’s umask). Ignored on Windows. |
recursive ? | boolean | Defaults to false . If set to true , means that any intermediate directories will also be created (as with the shell command mkdir -p ). |
OpenOptions
Since
2.0.0
Properties
Property | Type | Description |
---|---|---|
append ? | boolean | Sets the option for the append mode. This option, when true , means thatwrites will append to a file instead of overwriting previous contents. Note that setting { write: true, append: true } has the same effect assetting only { append: true } . |
baseDir ? | BaseDirectory | Base directory for path |
create ? | boolean | Sets the option to allow creating a new file, if one doesn’t already exist at the specified path. Requires write or append access to be used. |
createNew ? | boolean | Defaults to false . If set to true , no file, directory, or symlink isallowed to exist at the target location. Requires write or append access to be used. When createNew is set to true , create and truncateare ignored. |
mode ? | number | Permissions to use if creating the file (defaults to 0o666 , beforethe process’s umask). Ignored on Windows. |
read ? | boolean | Sets the option for read access. This option, when true , means that thefile should be read-able if opened. |
truncate ? | boolean | Sets the option for truncating a previous file. If a file is successfully opened with this option set it will truncate the file to 0 size if it already exists. The file must be opened with write access for truncate to work. |
write ? | boolean | Sets the option for write access. This option, when true , means thatthe file should be write-able if opened. If the file already exists, any write calls on it will overwrite its contents, by default without truncating it. |
ReadDirOptions
Since
2.0.0
Properties
Property | Type | Description |
---|---|---|
baseDir ? | BaseDirectory | Base directory for path |
ReadFileOptions
Since
2.0.0
Properties
Property | Type | Description |
---|---|---|
baseDir ? | BaseDirectory | Base directory for path |
RemoveOptions
Since
2.0.0
Properties
Property | Type | Description |
---|---|---|
baseDir ? | BaseDirectory | Base directory for path |
recursive ? | boolean | Defaults to false . If set to true , path will be removed even if it’s a non-empty directory. |
RenameOptions
Since
2.0.0
Properties
Property | Type | Description |
---|---|---|
newPathBaseDir ? | BaseDirectory | Base directory for newPath . |
oldPathBaseDir ? | BaseDirectory | Base directory for oldPath . |
StatOptions
Since
2.0.0
Properties
Property | Type | Description |
---|---|---|
baseDir ? | BaseDirectory | Base directory for path . |
TruncateOptions
Since
2.0.0
Properties
Property | Type | Description |
---|---|---|
baseDir ? | BaseDirectory | Base directory for path . |
WatchOptions
Since
2.0.0
Extended By
Properties
Property | Type | Description |
---|---|---|
baseDir ? | BaseDirectory | Base directory for path |
recursive ? | boolean | Watch a directory recursively |
WriteFileOptions
Since
2.0.0
Properties
Property | Type | Description |
---|---|---|
append ? | boolean | Defaults to false . If set to true , will append to a file instead of overwriting previous contents. |
baseDir ? | BaseDirectory | Base directory for path |
create ? | boolean | Sets the option to allow creating a new file, if one doesn’t already exist at the specified path (defaults to true ). |
createNew ? | boolean | Sets the option to create a new file, failing if it already exists. |
mode ? | number | File permissions. Ignored on Windows. |
Type Aliases
UnwatchFn
UnwatchFn: () => void
Since
2.0.0
Returns
void
Source: plugins/fs/guest-js/index.ts:1168
WatchEvent
WatchEvent: object;
Since
2.0.0
Type declaration
Member | Type |
---|---|
attrs | unknown |
paths | string [] |
type | WatchEventKind |
Source: plugins/fs/guest-js/index.ts:1101
WatchEventKind
WatchEventKind: "any" | {access: WatchEventKindAccess;} | {create: WatchEventKindCreate;} | {modify: WatchEventKindModify;} | {remove: WatchEventKindRemove;} | "other"
Since
2.0.0
Source: plugins/fs/guest-js/index.ts:1110
WatchEventKindAccess
WatchEventKindAccess: {kind: "any";} | {kind: "close"; mode: "any" | "execute" | "read" | "write" | "other";} | {kind: "open"; mode: "any" | "execute" | "read" | "write" | "other";} | {kind: "other";}
Since
2.0.0
Source: plugins/fs/guest-js/index.ts:1121
WatchEventKindCreate
WatchEventKindCreate: {kind: "any";} | {kind: "file";} | {kind: "folder";} | {kind: "other";}
Since
2.0.0
Source: plugins/fs/guest-js/index.ts:1130
WatchEventKindModify
WatchEventKindModify: {kind: "any";} | {kind: "data"; mode: "any" | "size" | "content" | "other";} | {kind: "metadata"; mode: "any" | "access-time" | "write-time" | "permissions" | "ownership" | "extended" | "other";} | {kind: "name"; mode: "any" | "to" | "from" | "both" | "other";} | {kind: "other";}
Since
2.0.0
Source: plugins/fs/guest-js/index.ts:1139
WatchEventKindRemove
WatchEventKindRemove: {kind: "any";} | {kind: "file";} | {kind: "folder";} | {kind: "other";}
Since
2.0.0
Source: plugins/fs/guest-js/index.ts:1159
Functions
copyFile()
copyFile( fromPath, toPath, options?): Promise< void >
Copies the contents and permissions of one file to another specified path, by default creating a new file if needed, else overwriting.
Example
import { copyFile, BaseDirectory } from '@tauri-apps/plugin-fs';await copyFile('app.conf', 'app.conf.bk', { dir: BaseDirectory.App });
Since
2.0.0
Parameters
Parameter | Type |
---|---|
fromPath | string | URL |
toPath | string | URL |
options ? | CopyFileOptions |
Returns
Promise
< void
>
Source: plugins/fs/guest-js/index.ts:560
create()
create(path, options?): Promise< FileHandle >
Creates a file if none exists or truncates an existing file and resolves to
an instance of FileHandle
.
Example
import { create, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await create('foo/bar.txt', { dir: BaseDirectory.App });
Since
2.0.0
Parameters
Parameter | Type |
---|---|
path | string | URL |
options ? | CreateOptions |
Returns
Source: plugins/fs/guest-js/index.ts:439
exists()
exists(path, options?): Promise< boolean >
Check if a path exists.
Example
import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';// Check if the `$APPDATA/avatar.png` file existsawait exists('avatar.png', { dir: BaseDirectory.AppData });
Since
2.0.0
Parameters
Parameter | Type |
---|---|
path | string | URL |
options ? | ExistsOptions |
Returns
Promise
< boolean
>
Source: plugins/fs/guest-js/index.ts:1066
lstat()
lstat(path, options?): Promise< FileInfo >
Resolves to a FileInfo
for the specified path
. If path
is a
symlink, information for the symlink will be returned instead of what it
points to.
Example
import { lstat, BaseDirectory } from '@tauri-apps/plugin-fs';const fileInfo = await lstat('hello.txt', { dir: BaseDirectory.App });console.log(fileInfo.isFile); // true
Since
2.0.0
Parameters
Parameter | Type |
---|---|
path | string | URL |
options ? | StatOptions |
Returns
Source: plugins/fs/guest-js/index.ts:919
mkdir()
mkdir(path, options?): Promise< void >
Creates a new directory with the specified path.
Example
import { mkdir, BaseDirectory } from '@tauri-apps/plugin-fs';await mkdir('users', { dir: BaseDirectory.App });
Since
2.0.0
Parameters
Parameter | Type |
---|---|
path | string | URL |
options ? | MkdirOptions |
Returns
Promise
< void
>
Source: plugins/fs/guest-js/index.ts:603
open()
open(path, options?): Promise< FileHandle >
Open a file and resolve to an instance of FileHandle
. The
file does not need to previously exist if using the create
or createNew
open options. It is the callers responsibility to close the file when finished
with it.
Example
import { open, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await open('foo/bar.txt', { read: true, write: true, dir: BaseDirectory.App });// Do work with fileawait close(file.rid);
Since
2.0.0
Parameters
Parameter | Type |
---|---|
path | string | URL |
options ? | OpenOptions |
Returns
Source: plugins/fs/guest-js/index.ts:524
readDir()
readDir(path, options?): Promise< DirEntry[] >
Reads the directory given by path and returns an array of DirEntry
.
Example
import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';const dir = 'users';const entries = await readDir('users', { dir: BaseDirectory.App });processEntriesRecursive(dir, entries);async function processEntriesRecursive(parent, entries) { for (const entry of entries) { console.log(`Entry: ${entry.name}`); if (entry.isDirectory) { const dir = parent + entry.name; processEntriesRecursive(dir, await readDir(dir, { dir: BaseDirectory.App })); } }}
Since
2.0.0
Parameters
Parameter | Type |
---|---|
path | string | URL |
options ? | ReadDirOptions |
Returns
Source: plugins/fs/guest-js/index.ts:664
readFile()
readFile(path, options?): Promise< Uint8Array >
Reads and resolves to the entire contents of a file as an array of bytes. TextDecoder can be used to transform the bytes to string if required.
Example
import { readFile, BaseDirectory } from '@tauri-apps/plugin-fs';const contents = await readFile('avatar.png', { dir: BaseDirectory.Resource });
Since
2.0.0
Parameters
Parameter | Type |
---|---|
path | string | URL |
options ? | ReadFileOptions |
Returns
Source: plugins/fs/guest-js/index.ts:697
readTextFile()
readTextFile(path, options?): Promise< string >
Reads and returns the entire contents of a file as UTF-8 string.
Example
import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';const contents = await readTextFile('app.conf', { dir: BaseDirectory.App });
Since
2.0.0
Parameters
Parameter | Type |
---|---|
path | string | URL |
options ? | ReadFileOptions |
Returns
Promise
< string
>
Source: plugins/fs/guest-js/index.ts:723
readTextFileLines()
readTextFileLines(path, options?): Promise< AsyncIterableIterator< string > >
Returns an async AsyncIterableIterator over the lines of a file as UTF-8 string.
Example
import { readTextFileLines, BaseDirectory } from '@tauri-apps/plugin-fs';const lines = await readTextFileLines('app.conf', { dir: BaseDirectory.App });for await (const line of lines) { console.log(line);}
You could also call AsyncIterableIterator.next to advance the iterator so you can lazily read the next line whenever you want.
Since
2.0.0
Parameters
Parameter | Type |
---|---|
path | string | URL |
options ? | ReadFileOptions |
Returns
Promise
< AsyncIterableIterator
< string
> >
Source: plugins/fs/guest-js/index.ts:752
remove()
remove(path, options?): Promise< void >
Removes the named file or directory.
If the directory is not empty and the recursive
option isn’t set to true, the promise will be rejected.
Example
import { remove, BaseDirectory } from '@tauri-apps/plugin-fs';await remove('users/file.txt', { dir: BaseDirectory.App });await remove('users', { dir: BaseDirectory.App });
Since
2.0.0
Parameters
Parameter | Type |
---|---|
path | string | URL |
options ? | RemoveOptions |
Returns
Promise
< void
>
Source: plugins/fs/guest-js/index.ts:814
rename()
rename( oldPath, newPath, options): Promise< void >
Renames (moves) oldpath to newpath. Paths may be files or directories. If newpath already exists and is not a directory, rename() replaces it. OS-specific restrictions may apply when oldpath and newpath are in different directories.
On Unix, this operation does not follow symlinks at either path.
Example
import { rename, BaseDirectory } from '@tauri-apps/plugin-fs';await rename('avatar.png', 'deleted.png', { dir: BaseDirectory.App });
Since
2.0.0
Parameters
Parameter | Type |
---|---|
oldPath | string | URL |
newPath | string | URL |
options | RenameOptions |
Returns
Promise
< void
>
Source: plugins/fs/guest-js/index.ts:853
stat()
stat(path, options?): Promise< FileInfo >
Resolves to a FileInfo
for the specified path
. Will always
follow symlinks but will reject if the symlink points to a path outside of the scope.
Example
import { stat, BaseDirectory } from '@tauri-apps/plugin-fs';const fileInfo = await stat('hello.txt', { dir: BaseDirectory.App });console.log(fileInfo.isFile); // true
Since
2.0.0
Parameters
Parameter | Type |
---|---|
path | string | URL |
options ? | StatOptions |
Returns
Source: plugins/fs/guest-js/index.ts:893
truncate()
truncate( path, len?, options?): Promise< void >
Truncates or extends the specified file, to reach the specified len
.
If len
is 0
or not specified, then the entire file contents are truncated.
Example
import { truncate, readFile, writeFile, BaseDirectory } from '@tauri-apps/plugin-fs';// truncate the entire fileawait truncate('my_file.txt', 0, { dir: BaseDirectory.App });
// truncate part of the filelet file = 'file.txt';await writeFile(file, new TextEncoder().encode('Hello World'), { dir: BaseDirectory.App });await truncate(file, 7);const data = await readFile(file, { dir: BaseDirectory.App });console.log(new TextDecoder().decode(data)); // "Hello W"
Since
2.0.0
Parameters
Parameter | Type |
---|---|
path | string | URL |
len ? | number |
options ? | TruncateOptions |
Returns
Promise
< void
>
Source: plugins/fs/guest-js/index.ts:959
watch()
watch( paths, cb, options?): Promise< UnwatchFn >
Watch changes (after a delay) on files or directories.
Since
2.0.0
Parameters
Parameter | Type |
---|---|
paths | string | URL | string [] | URL [] |
cb | (event ) => void |
options ? | DebouncedWatchOptions |
Returns
Source: plugins/fs/guest-js/index.ts:1179
watchImmediate()
watchImmediate( paths, cb, options?): Promise< UnwatchFn >
Watch changes on files or directories.
Since
2.0.0
Parameters
Parameter | Type |
---|---|
paths | string | URL | string [] | URL [] |
cb | (event ) => void |
options ? | WatchOptions |
Returns
Source: plugins/fs/guest-js/index.ts:1217
writeFile()
writeFile( path, data, options?): Promise< void >
Write data
to the given path
, by default creating a new file if needed, else overwriting.
Example
import { writeFile, BaseDirectory } from '@tauri-apps/plugin-fs';
let encoder = new TextEncoder();let data = encoder.encode('Hello World');await writeFile('file.txt', data, { dir: BaseDirectory.App });
Since
2.0.0
Parameters
Parameter | Type |
---|---|
path | string | URL |
data | Uint8Array |
options ? | WriteFileOptions |
Returns
Promise
< void
>
Source: plugins/fs/guest-js/index.ts:1004
writeTextFile()
writeTextFile( path, data, options?): Promise< void >
Writes UTF-8 string data
to the given path
, by default creating a new file if needed, else overwriting.
Example
import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
await writeTextFile('file.txt', 'Hello world', { dir: BaseDirectory.App });
Since
2.0.0
Parameters
Parameter | Type |
---|---|
path | string | URL |
data | string |
options ? | WriteFileOptions |
Returns
Promise
< void
>
© 2024 Tauri Contributors. CC-BY / MIT