Interface IFs

interface IFs {
    copyFile: (
        fromPath: string | URL,
        toPath: string | URL,
        options?: CopyFileOptions,
    ) => Promise<void>;
    create: (
        path: string | URL,
        options?: CreateOptions,
    ) => Promise<FileHandle>;
    exists: (path: string | URL, options?: ExistsOptions) => Promise<boolean>;
    fileSearch: (
        searchParams: Omit<
            {
                created_after?: number;
                created_before?: number;
                depth?: number;
                ext?: string;
                file_size_equal?: number;
                file_size_greater?: number;
                file_size_smaller?: number;
                hidden: boolean;
                ignore_case: boolean;
                limit?: number;
                locations: string[];
                modified_after?: number;
                modified_before?: number;
                query?: string;
            },
            "hidden"
            | "ignore_case",
        > & { hidden?: boolean; ignore_case?: boolean },
    ) => Promise<string[]>;
    lstat: (path: string | URL, options?: StatOptions) => Promise<FileInfo>;
    mkdir: (path: string | URL, options?: MkdirOptions) => Promise<void>;
    readDir: (
        path: string | URL,
        options?: ReadDirOptions,
    ) => Promise<DirEntry[]>;
    readFile: (
        path: string | URL,
        options?: ReadFileOptions,
    ) => Promise<Uint8Array>;
    readTextFile: (
        path: string | URL,
        options?: ReadFileOptions,
    ) => Promise<string>;
    remove: (path: string | URL, options?: RemoveOptions) => Promise<void>;
    rename: (
        oldPath: string | URL,
        newPath: string | URL,
        options?: RenameOptions,
    ) => Promise<void>;
    stat: (path: string | URL, options?: StatOptions) => Promise<FileInfo>;
    truncate: (
        path: string | URL,
        len?: number,
        options?: TruncateOptions,
    ) => Promise<void>;
    writeFile: (
        path: string | URL,
        data: Uint8Array | ReadableStream<Uint8Array>,
        options?: WriteFileOptions,
    ) => Promise<void>;
    writeTextFile: (
        path: string | URL,
        data: string,
        options?: WriteFileOptions,
    ) => Promise<void>;
}

Properties

copyFile: (
    fromPath: string | URL,
    toPath: string | URL,
    options?: CopyFileOptions,
) => Promise<void>

Type declaration

    • (
          fromPath: string | URL,
          toPath: string | URL,
          options?: CopyFileOptions,
      ): Promise<void>
    • Copies the contents and permissions of one file to another specified path, by default creating a new file if needed, else overwriting.

      Parameters

      • fromPath: string | URL
      • toPath: string | URL
      • Optionaloptions: CopyFileOptions

      Returns Promise<void>

      import { copyFile, BaseDirectory } from '@tauri-apps/plugin-fs';
      await copyFile('app.conf', 'app.conf.bk', { fromPathBaseDir: BaseDirectory.AppConfig, toPathBaseDir: BaseDirectory.AppConfig });

      2.0.0

create: (path: string | URL, options?: CreateOptions) => Promise<FileHandle>

Type declaration

    • (path: string | URL, options?: CreateOptions): Promise<FileHandle>
    • Creates a file if none exists or truncates an existing file and resolves to an instance of FileHandle.

      Parameters

      • path: string | URL
      • Optionaloptions: CreateOptions

      Returns Promise<FileHandle>

      import { create, BaseDirectory } from "@tauri-apps/plugin-fs"
      const file = await create("foo/bar.txt", { baseDir: BaseDirectory.AppConfig });
      await file.write(new TextEncoder().encode("Hello world"));
      await file.close();

      2.0.0

exists: (path: string | URL, options?: ExistsOptions) => Promise<boolean>

Type declaration

    • (path: string | URL, options?: ExistsOptions): Promise<boolean>
    • Check if a path exists.

      Parameters

      • path: string | URL
      • Optionaloptions: ExistsOptions

      Returns Promise<boolean>

      import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';
      // Check if the `$APPDATA/avatar.png` file exists
      await exists('avatar.png', { baseDir: BaseDirectory.AppData });

      2.0.0

fileSearch: (
    searchParams: Omit<
        {
            created_after?: number;
            created_before?: number;
            depth?: number;
            ext?: string;
            file_size_equal?: number;
            file_size_greater?: number;
            file_size_smaller?: number;
            hidden: boolean;
            ignore_case: boolean;
            limit?: number;
            locations: string[];
            modified_after?: number;
            modified_before?: number;
            query?: string;
        },
        "hidden"
        | "ignore_case",
    > & { hidden?: boolean; ignore_case?: boolean },
) => Promise<string[]>
lstat: (path: string | URL, options?: StatOptions) => Promise<FileInfo>

Type declaration

    • (path: string | URL, options?: StatOptions): 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.

      Parameters

      • path: string | URL
      • Optionaloptions: StatOptions

      Returns Promise<FileInfo>

      import { lstat, BaseDirectory } from '@tauri-apps/plugin-fs';
      const fileInfo = await lstat("hello.txt", { baseDir: BaseDirectory.AppLocalData });
      console.log(fileInfo.isFile); // true

      2.0.0

mkdir: (path: string | URL, options?: MkdirOptions) => Promise<void>

Type declaration

    • (path: string | URL, options?: MkdirOptions): Promise<void>
    • Creates a new directory with the specified path.

      Parameters

      • path: string | URL
      • Optionaloptions: MkdirOptions

      Returns Promise<void>

      import { mkdir, BaseDirectory } from '@tauri-apps/plugin-fs';
      await mkdir('users', { baseDir: BaseDirectory.AppLocalData });

      2.0.0

readDir: (path: string | URL, options?: ReadDirOptions) => Promise<DirEntry[]>

Type declaration

    • (path: string | URL, options?: ReadDirOptions): Promise<DirEntry[]>
    • Reads the directory given by path and returns an array of DirEntry.

      Parameters

      • path: string | URL
      • Optionaloptions: ReadDirOptions

      Returns Promise<DirEntry[]>

      import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';
      import { join } from '@tauri-apps/api/path';
      const dir = "users"
      const entries = await readDir('users', { baseDir: BaseDirectory.AppLocalData });
      processEntriesRecursively(dir, entries);
      async function processEntriesRecursively(parent, entries) {
      for (const entry of entries) {
      console.log(`Entry: ${entry.name}`);
      if (entry.isDirectory) {
      const dir = await join(parent, entry.name);
      processEntriesRecursively(dir, await readDir(dir, { baseDir: BaseDirectory.AppLocalData }))
      }
      }
      }

      2.0.0

readFile: (path: string | URL, options?: ReadFileOptions) => Promise<Uint8Array>

Type declaration

    • (path: string | URL, options?: ReadFileOptions): 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.

      Parameters

      • path: string | URL
      • Optionaloptions: ReadFileOptions

      Returns Promise<Uint8Array>

      import { readFile, BaseDirectory } from '@tauri-apps/plugin-fs';
      const contents = await readFile('avatar.png', { baseDir: BaseDirectory.Resource });

      2.0.0

readTextFile: (path: string | URL, options?: ReadFileOptions) => Promise<string>

Type declaration

    • (path: string | URL, options?: ReadFileOptions): Promise<string>
    • Reads and returns the entire contents of a file as UTF-8 string.

      Parameters

      • path: string | URL
      • Optionaloptions: ReadFileOptions

      Returns Promise<string>

      import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
      const contents = await readTextFile('app.conf', { baseDir: BaseDirectory.AppConfig });

      2.0.0

remove: (path: string | URL, options?: RemoveOptions) => Promise<void>

Type declaration

    • (path: string | URL, options?: RemoveOptions): 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.

      Parameters

      • path: string | URL
      • Optionaloptions: RemoveOptions

      Returns Promise<void>

      import { remove, BaseDirectory } from '@tauri-apps/plugin-fs';
      await remove('users/file.txt', { baseDir: BaseDirectory.AppLocalData });
      await remove('users', { baseDir: BaseDirectory.AppLocalData });

      2.0.0

rename: (
    oldPath: string | URL,
    newPath: string | URL,
    options?: RenameOptions,
) => Promise<void>

Type declaration

    • (
          oldPath: string | URL,
          newPath: string | URL,
          options?: RenameOptions,
      ): 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.

      Parameters

      • oldPath: string | URL
      • newPath: string | URL
      • Optionaloptions: RenameOptions

      Returns Promise<void>

      import { rename, BaseDirectory } from '@tauri-apps/plugin-fs';
      await rename('avatar.png', 'deleted.png', { oldPathBaseDir: BaseDirectory.App, newPathBaseDir: BaseDirectory.AppLocalData });

      2.0.0

stat: (path: string | URL, options?: StatOptions) => Promise<FileInfo>

Type declaration

    • (path: string | URL, options?: StatOptions): 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.

      Parameters

      • path: string | URL
      • Optionaloptions: StatOptions

      Returns Promise<FileInfo>

      import { stat, BaseDirectory } from '@tauri-apps/plugin-fs';
      const fileInfo = await stat("hello.txt", { baseDir: BaseDirectory.AppLocalData });
      console.log(fileInfo.isFile); // true

      2.0.0

truncate: (
    path: string | URL,
    len?: number,
    options?: TruncateOptions,
) => Promise<void>

Type declaration

    • (path: string | URL, len?: number, options?: TruncateOptions): 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.

      Parameters

      • path: string | URL
      • Optionallen: number
      • Optionaloptions: TruncateOptions

      Returns Promise<void>

      import { truncate, readTextFile, writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
      // truncate the entire file
      await truncate("my_file.txt", 0, { baseDir: BaseDirectory.AppLocalData });

      // truncate part of the file
      const filePath = "file.txt";
      await writeTextFile(filePath, "Hello World", { baseDir: BaseDirectory.AppLocalData });
      await truncate(filePath, 7, { baseDir: BaseDirectory.AppLocalData });
      const data = await readTextFile(filePath, { baseDir: BaseDirectory.AppLocalData });
      console.log(data); // "Hello W"

      2.0.0

writeFile: (
    path: string | URL,
    data: Uint8Array | ReadableStream<Uint8Array>,
    options?: WriteFileOptions,
) => Promise<void>

Type declaration

    • (
          path: string | URL,
          data: Uint8Array | ReadableStream<Uint8Array>,
          options?: WriteFileOptions,
      ): Promise<void>
    • Write data to the given path, by default creating a new file if needed, else overwriting.

      Parameters

      • path: string | URL
      • data: Uint8Array | ReadableStream<Uint8Array>
      • Optionaloptions: WriteFileOptions

      Returns Promise<void>

      import { writeFile, BaseDirectory } from '@tauri-apps/plugin-fs';

      let encoder = new TextEncoder();
      let data = encoder.encode("Hello World");
      await writeFile('file.txt', data, { baseDir: BaseDirectory.AppLocalData });

      2.0.0

writeTextFile: (
    path: string | URL,
    data: string,
    options?: WriteFileOptions,
) => Promise<void>

Type declaration

    • (path: string | URL, data: string, options?: WriteFileOptions): Promise<void>
    • Writes UTF-8 string data to the given path, by default creating a new file if needed, else overwriting.

      Parameters

      • path: string | URL
      • data: string
      • Optionaloptions: WriteFileOptions

      Returns Promise<void>

      import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';

      await writeTextFile('file.txt', "Hello world", { baseDir: BaseDirectory.AppLocalData });

      2.0.0