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.ts368 chars14 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