Note that if src
is a directory it will copy everything inside of this directory,
not the entire directory itself (see issue #537).
Note that if src
is a file, dest
cannot be a directory
(see issue #323).
Optional
options: CopyOptionsimport * as fs from 'fs-extra'
// With a callback:
fs.copy('/tmp/myfile', '/tmp/mynewfile', err => {
if (err) return console.error(err)
console.log('success!')
}) // copies file
fs.copy('/tmp/mydir', '/tmp/mynewdir', err => {
if (err) return console.error(err)
console.log('success!')
}) // copies directory, even if it has subdirectories or files
// With Promises:
fs.copy('/tmp/myfile', '/tmp/mynewfile')
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function asyncAwait () {
try {
await fs.copy('/tmp/myfile', '/tmp/mynewfile')
console.log('success!')
} catch (err) {
console.error(err)
}
}
asyncAwait()
// Using filter function
fs.copy(
'/tmp/mydir',
'/tmp/mynewdir',
{
filter(src, dest) {
// your logic here
// it will be copied if return true
}
},
err => {
if (err) return console.error(err)
console.log('success!')
}
)
Generated using TypeDoc
Copy a file or directory. The directory can have contents.