Skip to content

Latest commit

 

History

History

03-files-in-folder

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Displaying Information about Files Stored in a Folder

In the index.js file of the 03-files-in-folder directory, develop a script that outputs information about files contained in the secret-folder to the console.

Requirements

  • When executing the command node 03-files-in-folder in the root directory of the repository, information about files contained directly within 03-files-in-folder/secret-folder should be displayed in the console.
    The data should be presented in the format <file name>-<file extension>-<file size>.
    Example: example - txt - 128.369kb. Note: no rounding for file size is necessary; conversion to kB is optional!

  • Information should only be displayed for files located in 03-files-in-folder/secret-folder. The presence of information about directories is considered an error.

Objectives

  • Learn to obtain information about files.

Description

Steps to complete the task:

  1. Import all required modules.
  2. Read the contents of the secret-folder.
  3. Retrieve data for each object within the secret-folder.
  4. Check if the object is a file.
  5. Display file data in the console.

Tips

Check this article to fill in possible gaps in understanding how git works and to eliminate questions about reading files in subfolders.

To read the contents of a folder, use the readdir function from the fs/promises module. This function allows you to get the names of all files in a specified directory.

After reading the folder's contents, if you set the option {withFileTypes: true}, each object in it will be represented as an instance of the Dirent class. Its methods will help you determine whether the object is a file.

To determine the file extension, you can use the extname method of the path module.

Use stat to obtain information about a file.
You can see the full list of data returned by this function in the documentation. Note that the object returned by this function is an instance of the Stats class, which also has methods to check whether the object is a file.

Useful Links

If you're looking for the information in Russian, please note that translations of documentation into Russian may be outdated and may not contain all the modern features of the modules. For up-to-date information, always use the official documentation!