Skip to content

Instantly share code, notes, and snippets.

@WanderingHogan
Created November 6, 2025 05:20
Show Gist options
  • Select an option

  • Save WanderingHogan/25ee0ae225e67a7716da5cfc9db93c48 to your computer and use it in GitHub Desktop.

Select an option

Save WanderingHogan/25ee0ae225e67a7716da5cfc9db93c48 to your computer and use it in GitHub Desktop.
nodejs rename util
// run this by saving the file to the directory you want to change things in
// eg. node rename.js "text you want to replace" "Text you want it changed to"
// and all matching files will update
const fs = require( 'fs' );
const path = require( 'path' );
const textToReplace = process.argv[2] ?? 'Old.Text2Change';
const replacementText = process.argv[3] ?? 'Old Text to Change';
(async ()=>{
console.log(textToReplace, " -> ", replacementText)
try {
const files = await fs.promises.readdir('.');
for( const file of files ) {
const filePath = path.join( __dirname, file );
// const stat = await fs.promises.stat( filePath );
if(filePath.includes(textToReplace)){
await fs.promises.rename( filePath, filePath.replace(textToReplace, replacementText) );
}
}
}
catch( e ) {
console.log( 'Error renaming file: ', e );
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment