Created
April 5, 2022 18:18
-
-
Save tadeaspetak/01aa58d1f6552ce11d7b065628f3614e to your computer and use it in GitHub Desktop.
Copy files and update their timestamps so that KIA Ceed (2011) plays them in the correct order.
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
const fs = require("fs"); | |
const path = require("path"); | |
const isValidFile = (filename) => !filename.startsWith("."); | |
const updateLastModifiedAt = async (filename) => { | |
const time = new Date(); | |
try { | |
fs.utimesSync(filename, time, time); | |
} catch (err) { | |
fs.closeSync(fs.openSync(filename, "w")); | |
} finally { | |
await new Promise((res) => setTimeout(() => res(), 1000)); // wait a sec to make sure the differences are large enough | |
console.log(filename, "updated"); | |
} | |
}; | |
(async () => { | |
const from = "/Users/tadeas/Books"; | |
const to = "/Volumes/4GB"; | |
const dirs = fs.readdirSync(from); | |
for (const dir of dirs) { | |
if (!isValidFile(dir)) continue; | |
if (!fs.existsSync(path.join(to, dir))) fs.mkdirSync(path.join(to, dir)); | |
await updateLastModifiedAt(path.join(to, dir)); | |
const files = fs.readdirSync(path.join(from, dir)); | |
for (const file of files) { | |
if (!isValidFile(file)) continue; | |
if (fs.lstatSync(path.join(from, dir, file)).isDirectory()) continue; | |
fs.copyFileSync(path.join(from, dir, file), path.join(to, dir, file)); | |
await updateLastModifiedAt(path.join(to, dir, file)); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment