move a directory in typescript

To move a directory in TypeScript, we can use the fs module which is a built-in module in Node.js for file system operations.

We can move a directory using the rename() method provided by the fs module. Here's an example code snippet:

index.ts
import * as fs from 'fs';

async function moveDirectory(srcPath: string, destPath: string) {
  try {
    await fs.promises.rename(srcPath, destPath);
    console.log('Directory moved successfully');
  } catch (err) {
    console.log(`Error while moving directory: ${err}`);
  }
}

// Usage
moveDirectory('/path/to/source/directory', '/path/to/destination/directory');
368 chars
14 lines

In the above code, we have defined an async function moveDirectory() that takes two arguments srcPath and destPath which are the paths of the source and destination directories respectively.

Inside the function, we call the rename() method of fs.promises with the srcPath and destPath as arguments. This method returns a Promise that resolves when the directory is moved successfully.

We have also wrapped the rename() method in a try-catch block to handle any errors that may occur during the directory move operation.

Once the directory is moved successfully, the console will log "Directory moved successfully".

gistlibby LogSnag