Node.js scripts can be a real time-saver when you want to automate common tasks. In this post, you'll learn how to write your own script to rename files using fs.
Creating and running your script
I have a couple of scripts that I use for my blog, which I keep inside of the scripts
folder in the blog's repository.
Here I've created a file named renameFile.js
:
// scripts/renameFile.js
#!/usr/bin/env node
const main = () => {
// code goes here
}
main();
The first line is known as a shebang line and makes sure that the script is executed with Node.js.
You can either run the script directly:
node ./scripts/renameFile.js
Or if you're storing it in your repository, you can add a shortcut to run it in your package.json
file:
"scripts": {
"rename": "./scripts/renameFile.js"
},
Depending on whether youβre using Yarn or npm, you can run this script with one of these commands:
yarn rename
# or
npm run rename
Make sure to install dependencies
In the sections below, weβll be making use of a library called fs. Make sure to install your dependencies before you run the script!
yarn add --dev fs
# or
npm install -D fs
How to rename a file with fs rename
fs provides a lot of useful functions for modifying your files.
The rename
function will asynchronously rename a file:
const { rename } = require('fs');
const callback = () => {};
const oldFileName = '/Users/emma/src/emgoto.com/old.txt';
const newFileName = '/Users/emma/src/emgoto.com/new.txt';
rename(oldFileName, newFileName, callback);
The file name needs to contain the full path of the file. We can use process.cwd()
to get the file path of the directory that the script was run from.
So instead of this:
'/Users/emma/src/emgoto.com/old.txt'
We can do this:
`${process.cwd()}/old.txt`
Youβll also notice that weβre passing in a callback function as the third argument. This callback will get executed once the rename is successful.
Even if you donβt want a callback executed, this argument is not optional. You can pass in
() => {}
if you donβt want anything to happen.
Alternatively, you can also synchronously rename a file using renameSync
:
const { renameSync } = require('fs');
renameSync(oldFileName, newFileName);
This means while the file is being renamed, the rest of the script won't execute.
rename vs renameSync: which should I use?
If youβre writing a complex script, you may be able to improve performance by using asynchronous methods. In my case, since the scripts I write are simple, it doesnβt make too much of a difference.
The main thing to keep in mind is that, if you are renaming a file asynchronously and then directly after you try and modify the new file:
rename(oldFileName, newFileName, callback); // <- async
doStuff(newFileName);
You could end up in a situation where you're executing doStuff
on a file that doesn't exist yet!
In this scenario, make sure to either do it synchronously with renameSync
:
renameSync(oldFileName, newFileName);
doStuff(newFileName);
Or, make sure you modify the new file inside of the callback:
const callback = () => doStuff(newFileName);
rename(oldFileName, newFileName, callback);
How to rename a folder with fs rename
You can also use rename
and renameSync
to rename folders.
How to find files with glob
In the above example, we are renaming a specific file. In real-life, you'll probably want to find a list of files that match a certain pattern.
Letβs say you were looking for any zip files inside of your appβs src
folder:
const { join } = require('path');
const glob = require('glob');
const files = glob.sync(
join(process.cwd(), 'src', '**', '*.zip'),
);
Breaking down the code above:
-
process.cwd()
gets the current directory (from where you ran the script) -
join()
will join all the strings you pass in with/
to make a path -
glob.sync()
does a pattern-matching search to try and find any files that match the string you provided. (There is also an async version,glob()
).
This will return an array of files that end in .zip
. If you were only expecting one file, youβll be able to grab it from the 0th index of the array (files[0]
).
How to pass in arguments to the script
You might want to pass in an argument to your script, like the name of the file you wish to rename.
You can pass in as many arguments as you like:
yarn rename argOne argTwo
# or
npm run rename argOne argTwo
And then you can get them out using the process.argv
array:
process.argv[2] // argOne
process.argv[3] // argTwo
(The 0th and 1st spots in the array will contain node
and the name of your script).
Conclusion
Automating the process of file renaming can speed up an otherwise tedious process. With this post, you should now feel comfortable to write your own Node.js scripts!
I would encourage you to check out the fs documentation to see other useful methods for file deletion and modification.
Top comments (6)
I'm not a node person - why would you need to regularly rename files like that? Enough to put it in a script? Is there something like packages that have the "wrong" names in npm that you're trying to fix?
So this was more of an example, but for me personally, I write my posts in Ulysses, and I've written scripts to convert it to the MDX format I need to publish my posts. In the process I need to do some file renaming and moving as well.
The "fs" library does not have to be installed, it is part of the Node API, just requiring it in your JavaScript file and executing it with Node is all. In fact you don't need a package.json either, just the JavaScript file.
Ah, thanks for that! I'll keep in the part about the package.json (since I think that can still be useful) but will also add the instructions to run it directly :)
I really needed this.
Glad to hear it π