Created
October 8, 2023 00:59
-
-
Save jonluca/ba2b52f3e09927d0a65b6a2fc6702b40 to your computer and use it in GitHub Desktop.
Copy Files Examples - Bun/Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs/promises'; | |
import path from 'path'; | |
const source = '/Volumes/Untitled/DCIM/101MSDCF' | |
const dest = '/Volumes/JonLuca\'s\ Pics/Pics/San\ Francisco,\ CA\ \(Fleetweek\)/' | |
const sourceFiles = await fs.readdir(source) | |
const destFiles = await fs.readdir(dest) | |
const filesToCopy = sourceFiles.filter(f => !destFiles.includes(f)) | |
console.log(`Copying ${filesToCopy.length} files`) | |
let done = 0; | |
let totalTime = 0; // to keep track of the total time taken | |
for(const file of filesToCopy){ | |
const start = performance.now(); | |
await fs.cp(path.join(source, file), path.join(dest,file)) | |
const elapsed = performance.now() - start; | |
totalTime += elapsed; | |
++done; | |
if(done %10 === 0){ | |
const avgTime = totalTime / done | |
console.log(`Completed ${done} - ${avgTime.toFixed(2)}`) | |
} | |
} | |
const avgTime = totalTime / filesToCopy.length; | |
console.log(`Done. Average time to copy each file: ${avgTime.toFixed(2)}ms`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment