Skip to content

Instantly share code, notes, and snippets.

@kigold
Created July 15, 2017 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kigold/73fb30989eb92896b6bc1109d683b1a9 to your computer and use it in GitHub Desktop.
Save kigold/73fb30989eb92896b6bc1109d683b1a9 to your computer and use it in GitHub Desktop.
Renames all files in a folder with Node/Javascript
var fs = require( 'fs' );
var path = require( 'path' );
// In newer Node.js versions where process is already global this isn't necessary.
var process = require( "process" );
var folder = path.join(__dirname,"/folder_name");
console.log(folder);
// Loop through all the files in the temp directory
fs.readdir( folder, function( err, files ) {
if( err ) {
console.error( "Could not list the directory.", err );
process.exit( 1 );
}
console.log("testing");
files.forEach( function( file, index ) {
// Make one pass and make the file complete
//var temp= file.split("_");
var t = file.toLowerCase();
//console.log(file);
//console.log(t.join("_") + "\n");
var oldName = path.join(folder, file);
var newName = path.join(folder,t);
fs.rename(oldName, newName, function(error){
if (error) {
console.log("ERRRRRRRRRROOO");
}
else {
console.log(t + " success");
}
});
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment