UXP Toolkit
    Preparing search index...

    Represents a file on a file system. Provides methods for reading from and writing to the file. You'll never instantiate a File directly; instead you'll get access via a storage.FileSystemProvider.

    Keep in mind that File as such doesn't need a require() statement, however a localFileSystem will need it.

    // Get the object of a File instance
    const fs = require('uxp').storage.localFileSystem;
    const file = await fs.createEntryWithUrl("file:/Users/user/Documents/tmp"); // Gets a File instance
    console.log(file.isFile); // returns true

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    isEntry: boolean

    Indicates that this instance is an Entry. Useful for type-checking.

    if (something.isEntry) {
    return something.getMetadata();
    }
    isFile: true

    Indicates that this instance is a file.

    if (anEntry.isFile) {
    await anEntry.read();
    }
    isFolder: boolean

    Indicates that this instance is not a folder. Useful for type-checking.

    if (!anEntry.isFolder) {
    return "This entry is not a folder.";
    }

    Indicates whether this file is read-only or read-write. See readOnly and readWrite.

    if (aFile.mode === modes.readOnly) {
    throw new Error("Can't write to a file opened as read-only.");
    }
    name: string

    The name of this entry. Read-only.

    console.log(anEntry.name);
    
    nativePath: string

    The platform native file-system path of this entry. Read-only

    console.log(anEntry.nativePath);
    

    The associated provider that services this entry. Read-only.

    if (entryOne.provider !== entryTwo.provider) {
    throw new Error("Providers are not the same");
    }
    url: string

    The url of this entry. You can use this url as input to other entities of the extension system like for eg: set as src attribute of a Image widget in UI. Read-only.

    console.log(anEntry.url);
    

    Methods

    • Copies this entry to the specified folder.

      Parameters

      • folder: Folder

        The folder to which to copy this entry.

      • Optionaloptions: { allowFolderCopy?: boolean; overwrite?: boolean }

        Options for the copy operation (all properties are optional)

        • OptionalallowFolderCopy?: boolean

          If true, allows copying the folder.

        • Optionaloverwrite?: boolean

          If true, allows overwriting existing entries.

      Returns Promise<Folder | File>

      EntryExists If the attempt would overwrite an entry and overwrite is false.

      PermissionDenied If the underlying file system rejects the attempt.

      OutOfSpace If the file system is out of storage space.

      await someFile.copyTo(someFolder);
      
      await someFile.copyTo(someFolder, {overwrite: true});
      
      await someFolder.copyTo(anotherFolder, {overwrite: true, allowFolderCopy: true});
      
    • Moves this entry to the target folder, optionally specifying a new name.

      Parameters

      • folder: Folder

        The folder to which to move this entry.

      • Optionaloptions: { newName?: string; overwrite?: boolean }

        Options for the move operation (all properties are optional)

        • OptionalnewName?: string

          If specified, the entry is renamed to this name.

        • Optionaloverwrite?: boolean

          If true allows the move to overwrite existing files.

      Returns Promise<void>

      await someFile.moveTo(someFolder);
      
      await someFile.moveTo(someFolder, {overwrite: true});
      
      await someFolder.moveTo(anotherFolder, {overwrite: true});
      
      await someFile.moveTo(someFolder, {newName: 'masterpiece.txt'})
      
      await someFile.moveTo(someFolder, {newName: 'novel.txt', {overwrite: true})
      
    • Returns the details of the given entry like name, type and native path in a readable string format.

      Returns string

    • Writes data to a file, appending if desired. The format of the file is controlled via the format option, and defaults to UTF8.

      Parameters

      • data: string | ArrayBuffer

        The data to write to the file.

      • Optionaloptions: { append?: boolean; format?: FormatSymbol }

        Options for the write operation (all properties are optional)

        • Optionalappend?: boolean

          If true, the data is written to the end of the file.

        • Optionalformat?: FormatSymbol

          The format of the file; see utf8 and binary.

      Returns Promise<number>

      The length of the contents written to the file.

      FileIsReadOnly If writing to a read-only file.

      OutOfSpace If writing to the file causes the file system to exceed the available space (or quota).

      await myNovel.write("It was a dark and stormy night.\n");
      await myNovel.write("Cliches and tropes aside, it really was.", {append: true});
      const data = new ArrayBuffer();
      await aDataFile.write(data, {format: formats.binary});
    • Determines if the entry is a file or not. This is safe to use even if the entry is null or undefined.

      Parameters

      • entry: Entry

        The entry to check.

      Returns boolean

      If true, the entry is a file.