Created
November 6, 2025 05:20
-
-
Save WanderingHogan/25ee0ae225e67a7716da5cfc9db93c48 to your computer and use it in GitHub Desktop.
nodejs rename util
This file contains hidden or 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
| // 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