Skip to content

Instantly share code, notes, and snippets.

@mkatychev
Created July 23, 2018 10:24
Show Gist options
  • Save mkatychev/18978512cd67f15ec0e0fd653b37dfeb to your computer and use it in GitHub Desktop.
Save mkatychev/18978512cd67f15ec0e0fd653b37dfeb to your computer and use it in GitHub Desktop.
Simple script to import files as variables from a predetermined filepath (quite useful for media folders with tons of pictures and the like)
import fs
const fs = require('fs');
function importFiles(x,y) {
fs.readdir(x, (err, files) => {
files.forEach(file => {
var doc = file
var char = /.+(?=\.\w+)/.exec(file);
if (char !== null) {
eval(y + char + " = " + "'" + String(x + doc) + "'");
}
});
})
}
importFiles('../public/img/', 'img');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment