Skip to content

Instantly share code, notes, and snippets.

@mrocha98
Created April 12, 2021 15:07
Show Gist options
  • Save mrocha98/93734faafb28988f6749c0cd76fac319 to your computer and use it in GitHub Desktop.
Save mrocha98/93734faafb28988f6749c0cd76fac319 to your computer and use it in GitHub Desktop.
A Node.js script that remove spaces from filenames and replace for dashes
const path = require('path')
const { promises: fs } = require('fs')
const DIR = '.'
const spacesRegex = /\s/g
;(async () => {
try {
const files = await fs.readdir(DIR)
for (const file of files) {
const stat = await fs.stat(file)
const IS_NOT_FILE = !stat.isFile()
if (IS_NOT_FILE) continue
const oldPath = path.join(DIR, file)
const newPath = path.join(DIR, file.replace(spacesRegex, '-'))
await fs.rename(oldPath, newPath)
}
} catch (err) {
console.log(err)
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment