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

Git Commands

There are many commands on git that new developers do not know of. These commands can make working in open source projects, or group projects easier to understand. Of all these commands, I will be going through two that most new developers do not know of. Log While working in large groups, there are many commits that you or others may have done within a repository. Using 'log,' you can view the history of all the changes made. Basic command:    git log  With this basic command, you can view the commit id , author of the commit, date of the revision, and the details the user wrote.     git log --follow filename    This command allows you to follow the commit history of a single file. It is useful for working in big groups as you can follow a file that you contributed to, instead of viewing the whole repository. Simply replace ' filename ' with the directory of the file you want to view.   git log - number    git log...

Lab 3: Potential Projects

In this lab, I will be going through three potential open source projects I would like to contribute to. 1: iD Repository : https://github.com/openstreetmap/iD iD is an easy to use OpenStreetMap editor, which uses Javascript. It supports all of the current modern browsers, and the intention is to do the basic tasks without breaking other people's data. I chose this open source project, as I was always interested in figuring out how to play around with Maps. I am going to go through the issues to see if there is anything I am capable enough to fix, or find issues on my own when going through the code. What I would need to learn is Javascript, how to support all browsers, and how to properly contribute worthwhile issues to the project. 2. Teammates Repository : https://github.com/TEAMMATES/teammates Teammates is a cloud-based open source project that provides users to manage peer evaluations and other feedback for students. It is used by both the educator and the stud...