Skip to main content

A CLOSER LOOK INTO RENAMING FILES IN NODE.JS

When programming, using modules and libraries within our code is a must. As the libraries are written by others, beginners will be confused on what each function and the arguments mean. Throughout this post, we will go through renaming files with Node.js.

First, we must grasp the concept of opening a file, so the program knows what file to search for. The following is a simple set of code that will open a file.

Open()

const fs = require('fs');
fs.open('/open/some/file.txt', 'r', (err, fd) => {
     if (err) { 
          throw err;
     }
     fs.close(fd, (err) => {
          if (err) {
               throw err;
          }
     });
});
Let's go through what each part of the code represents. 
  • fs is a module that allows access to a physical file system. 
The fs.open function is called when you want to open a file. The first argument should be the path to the file. The second argument refers to whether you want to only allow read access, or write access. In this example, we will use the read option. 
  • fd represents the file descriptor, which is used for accessing newly opened files.
The error is thrown if the file is not found/has errors, and you close the file when you are finished using it.

Rename()

Now that you understand how to open a simple file, let's get into how to rename a file. In Node.js, there is a built in module that allows you to do this easily:
fs.rename(oldPath, newPath, callback){
Here is an example of how the code can be used.
fs.rename('oldFile.txt', 'newFile.txt', (err) => {
     if (err) {
          throw err;
     }
     console.log('Rename complete!');
});
Let's go through what each part of the function represents. 
  • oldPath represents the current name of the file/directory
  • newPath represents the name of the file you want to rename it to
  • callback can be used to throw an error, if there is one
You can see that this module uses fs in front of the rename() function. This goes back to the first part, opening a file. Remember that fs is the prefix before using any function that uses the physical file system, as it is already declared.

Comments

Popular posts from this blog

Release 0.3 - Final

For this release (0.3), I worked on an RPG game that uses the JavaScript React component. The goal was to implement a feature which tracked stats into global variables. PR:  https://github.com/ASteinheiser/react-rpg.com/pull/72 This task was actually harder than I anticipated, because of the tedious task of searching through the code to find what you are looking for. For example: It took a long time in order to find the monster names that are being killed, because I don't know where the original developer had placed those variables. Figuring out how to store a global variable and be able to change those values were a medium-level challenge, as it was the first time I stored all of the global variables in a different file. I was confused on how to change those values, and use it in other files.

Release 0.4 Update

For this last Release (0.4), I have found an issue for a rock-paper-scissors game. As JavaScript is my favourite language, I have decided to find another issue that uses the language. For this issue, the problem is that there is currently no Refresh button for this game, so the stats remain the same throughout. Issue:  https://github.com/golemheavy/RPS-Multiplayer/issues/14

Release 0.4 Final

This month had been extremely busy for all of us, especially since BTS630 (Major Project Implementation) required a lot of our focus as well. For this release, I had to create a restart / refresh button that resets the stats of the game (wins, losses, ties). Comparatively to Release 0.3, I did not manage to find a very big issue. But I still found an issue that was bigger than the issues of Release 0.2, and big enough for this Release (0.4). For this release, I implemented the Refresh button, and managed to fix a few of the bugs as well. The Refresh button now changes all the stats back to 0. The bug I found was that the player choice was always 'null,' and the computer choice did not change. Now, the player choice is changed so that it correctly shows the player input, and the computer choice changes randomly every time a new choice has been made. This course was overall very fun and different to the other required courses of the BSD program. I enjoyed it quite well and ho...