Deno - Rename File & Directory Examples

How to rename a file or a directory in Deno? Find out in this tutorial.

If there is a file or a directory and you want to change the name programmatically using Deno, it can be done easily. Below are the functions you can use along with the examples.

Rename File/Directory

To use the below functions, you need to add --allow-read and --allow-write flag when running deno run command because Deno needs to perform both read and write operations.

Using Deno.rename

Deno.rename is used to rename a path into another path. It can be the path of a file or a directory.

  function Deno.rename(oldpath: string, newpath: string): Promise<void>

Both oldpath and newpath can be an absolute path or a relative path. If you use a relative path, it must be relative to the working directory which defaults to the directory where you execute deno run command. However, you can change the working directory using Deno.chdir command.

Below is the usage example.

  await Deno.rename('hello.txt', 'hello1.txt');

If the newpath already exists and not a directory, it will be replaced with the rename file.

Besides renaming the file, it can also be used to move a file to another directory. However, OS-specific restrictions may apply.

  await Deno.rename('hello.txt', 'files/hello.txt');

Renaming a directory is similar to renaming a file.

  await Deno.rename('files', 'data');

Using Deno.renameSync

Deno.renameSync is similar to Deno.rename, but it does the operation synchronously.

  function Deno.renameSync(oldpath: string, newpath: string): void

Below is the usage example.

  Deno.renameSync('hello.txt', 'hello1.txt');

Error Handling

There can be an error while trying to rename a path into another path. For example, if the old path is not found.

  error: Uncaught (in promise) NotFound: No such file or directory (os error 2)
      at processResponse (core.js:223:11)
      at Object.jsonOpAsync (core.js:240:12)
      at async Object.rename (deno:cli/rt/30_fs.js:171:5)
      at async file:///home/ivan/Projects/deno/src/examples/file_rename.ts:1:1

You can also get an error if the application doesn't have sufficient permission to perform the operation.

  Check file:///home/ivan/Projects/deno/src/examples/file_rename.ts
  error: Uncaught (in promise) PermissionDenied: Permission denied (os error 13)
      at processResponse (core.js:223:11)
      at Object.jsonOpAsync (core.js:240:12)
      at async Object.rename (deno:cli/rt/30_fs.js:171:5)
      at async file:///home/ivan/Projects/deno/src/examples/file_rename.ts:1:1

Passing a non-empty directory as newpath also causes an error.

  Check file:///home/ivan/Projects/deno/src/examples/file_rename.ts
  error: Uncaught (in promise) Error: Is a directory (os error 21)
      at processResponse (core.js:223:11)
      at Object.jsonOpAsync (core.js:240:12)
      at async Object.rename (deno:cli/rt/30_fs.js:171:5)
      at async file:///home/ivan/Projects/deno/src/examples/file_rename.ts:4:1

To handle an error that may occur, you can wrap the code inside a try-catch block.

  try {
    await Deno.rename('hello.txt', 'hello1.txt');
  } catch (err) {
    console.error(err);
  }

 

Related Posts: