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.
-
When executing the command
node 03-files-in-folder
in the root directory of the repository, information about files contained directly within03-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.
- Learn to obtain information about files.
Steps to complete the task:
- Import all required modules.
- Read the contents of the secret-folder.
- Retrieve data for each object within the secret-folder.
- Check if the object is a file.
- Display file data in the console.
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.
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!