Node.js file copying, folder creation and other related operations

Node.js file copying, folder creation and other related operations

NodeJS copies the files:

Generally, the copy operation of small files uses the stream pipeline transport operation.

First you need to load the imported file: var fs = require('fs');

1. Create folders synchronously

fs.mkdirSync(yourfileDirPath);

Create folder asynchronously

fs.mkdir(yourfileDirPath);

2. Determine whether the folder exists - Synchronize

fs.existsSync(dirpath);

asynchronous

fs.exists(dirpath);

Using the above related operations, we can directly write a method to determine the creation of a folder.

function mkdir(dirpath,dirname) {
  //Judge whether the second parameter is normal to avoid passing in wrong parameters when calling if (dirname !== path.dirname(dirpath)) {
   mkdir(dirpath);
   return;
  }
  if (fs.existsSync(dirname)) {
   fs.mkdirSync(dirpath)
  } else {
   mkdir(dirname, path.dirname(dirname));
   fs.mkdirSync(dirpath);
  }
}

Pay attention to synchronous and asynchronous creation, reading and other issues, otherwise the folder will not exist when copied. It is recommended to use synchronous creation Sync method, which is generally in the form of fs.xxxSync.

Summarize

This is the end of this article about copying node.js files, creating folders and other related operations. For more related node.js file copying and folder creation content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • In-depth analysis of Node.js single-threaded model
  • Understanding what Node.js is is so easy
  • Specific use of node.js global variables
  • The whole process of node.js using express to automatically build the project
  • Detailed explanation of node.js installation and HbuilderX configuration
  • Node.js sends emails based on STMP protocol and EWS protocol
  • Detailed explanation of asynchronous generators and asynchronous iterations in Node.js
  • Detailed explanation of the steps to create a web server with node.js
  • How to collect and parse command line arguments in Node.js
  • Detailed explanation of Alibaba Node.js technical documentation process module learning guide
  • Complete steps to develop cli using node.js
  • How to connect MySQL to Node.js through Sequelize
  • How to install the latest version of Node.js on CentOS 8.2 server
  • How to remove the BOM header of Node.js text file
  • Appium+python automated configuration (adk, jdk, node.js)
  • How to convert a callback in Node.js to a Promise
  • Node.js path module, get the file suffix operation
  • First experience with node.js crawler framework node-crawler
  • Nodejs Exploration: In-depth understanding of the principle of single-threaded high concurrency

<<:  MySQL initialization password operation under Mac

>>:  MySQL 5.7.17 installation and configuration method graphic tutorial under win7

Recommend

Deleting files with spaces in Linux (not directories)

In our daily work, we often come into contact wit...

Today I will share some rare but useful JS techniques

1. Back button Use history.back() to create a bro...

The difference and usage of Vue2 and Vue3 brother component communication bus

Table of contents vue2.x vue3.x tiny-emitter plug...

How to configure path alias for react scaffolding

The react version when writing this article is 16...

How to deploy springcloud project with Docker

Table of contents Docker image download Start mys...

Vue custom directive details

Table of contents 1. Background 2. Local custom i...

Issues and precautions about setting maxPostSize for Tomcat

1. Why set maxPostSize? The tomcat container has ...

A brief discussion on the fun of :focus-within in CSS

I believe some people have seen this picture of c...

A simple example of MySQL joint table query

MySql uses joined table queries, which may be dif...

Detailed explanation of Xshell common problems and related configurations

This article introduces common problems of Xshell...

How to install MySql in CentOS 8 and allow remote connections

Download and install. First check whether there i...

Docker binding fixed IP/cross-host container mutual access operation

Preface Previously, static IPs assigned using pip...