Hướng dẫn rename file nodejs - đổi tên tập tin nodejs

Node.js vận chuyển với một mô -đun hệ thống tệp cho phép bạn tương tác với các tệp trên đĩa cứng cục bộ. Mô -đun hệ thống tệp FS cung cấp hai phương thức cho phép bạn đổi tên các tệp:

const Fs = require('fs')  
const Path = require('path')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

Fs.renameSync(oldPath, newPath)  
3 và
const Fs = require('fs')  
const Path = require('path')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

Fs.renameSync(oldPath, newPath)  
4.

Hướng dẫn này chỉ cho bạn cách đổi tên một tệp đồng bộ và không đồng bộ bằng cách sử dụng mô -đun cốt lõi của Node.js.

Tổng quan về chuỗi Node.js

  • Node.js
  • Dây
  • Dòng suối
  • Ngày giờ
  • Mảng
  • Hứa hẹn
  • Json
  • Trình lặp
  • Các lớp học
  • Số
  • Các đối tượng
  • Hệ thống tập tin
  • Bản đồ
  • Quá trình
  • Biểu tượng
  • Nền tảng/HĐH
  • HTTPS
  • Băm

Đổi tên các tệp không đồng bộ trong Node.js

Mô-đun lõi Node.js ____

const Fs = require('fs')  
const Path = require('path')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

Fs.renameSync(oldPath, newPath)  
5 cung cấp phương thức
const Fs = require('fs')  
const Path = require('path')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

Fs.renameSync(oldPath, newPath)  
3 như một cách không đồng bộ/không chặn để đổi tên các tệp trên đĩa. Không chặn trong trường hợp này có nghĩa là Node.js xử lý các hoạt động khác trong khi chờ đĩa cứng phản hồi.

const Fs = require('fs')  
const Path = require('path')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

Fs.renameSync(oldPath, newPath)  
8 này hỗ trợ các cuộc gọi lại theo mặc định. Hỗ trợ gọi lại đến từ những ngày đầu của Node.js nơi các hoạt động không đồng bộ được xử lý bằng các cuộc gọi lại.

Sau đó, Node.js đã thêm hỗ trợ cho lời hứa và async/chờ đợi. Hướng dẫn này giả định rằng bạn đang sử dụng Async/đang chờ kiểm soát dòng chảy của mã của bạn. Đó là lý do Lý do ví dụ sử dụng xuất khẩu

const Fs = require('fs')  
const Path = require('path')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

Fs.renameSync(oldPath, newPath)  
9 có sẵn trong Node.js v10.0.0 trở lên. Các phương thức hệ thống tệp dựa trên lời hứa này có thể sử dụng được với Async/đang chờ đợi.

Phương pháp

const Fs = require('fs')  
const Path = require('path')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

Fs.renameSync(oldPath, newPath)  
3 mong đợi hai đối số:

  1. Đường dẫn tệp cũ
  2. Đường dẫn tệp mới

Lúc đầu, bạn có thể mong đợi một đường dẫn tệp và tên tệp mới thay vì hai đường dẫn tệp làm đối số. Chà, đổi tên một tập tin về cơ bản là di chuyển một tệp từ nơi này sang nơi khác. Ngay cả khi không thay đổi thư mục. Hoạt động cơ bản của việc đổi tên một tập tin là di chuyển từ tên từ tên trước sang tên mới.

Dưới đây, một ví dụ về cách đổi tên một tệp trong node.js bằng mô-đun

const Fs = require('fs')  
const Path = require('path')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

Fs.renameSync(oldPath, newPath)  
5 tích hợp:

const Path = require('path')  
const { promises: Fs } = require('fs')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

await Fs.rename(oldPath, newPath)  

Xin lưu ý: Phương thức này ghi đè bất kỳ tệp hiện có tại

const Path = require('path')  
const Fs = require('@supercharge/filesystem')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

await Fs.rename(oldPath, newPath)  
2. Trong trường hợp
const Path = require('path')  
const Fs = require('@supercharge/filesystem')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

await Fs.rename(oldPath, newPath)  
2 trỏ đến một thư mục hiện có, phương pháp này đã gây ra lỗi.
this method overwrites any existing file at
const Path = require('path')  
const Fs = require('@supercharge/filesystem')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

await Fs.rename(oldPath, newPath)  
2. In case the
const Path = require('path')  
const Fs = require('@supercharge/filesystem')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

await Fs.rename(oldPath, newPath)  
2 points to an existing directory, this method throws an error.

Đổi tên các tập tin đồng bộ

Node.js cũng đi kèm với một phương thức

const Fs = require('fs')  
const Path = require('path')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

Fs.renameSync(oldPath, newPath)  
4. Phương thức này chặn vòng lặp sự kiện Node.js cho các hoạt động khác (vì nó chờ đĩa cứng) trong khi đổi tên một tệp. Dưới đây, một ví dụ cho bạn biết cách đổi tên tệp đồng bộ:

const Fs = require('fs')  
const Path = require('path')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

Fs.renameSync(oldPath, newPath)  

Sử dụng gói @SuperCharge/FileSystem

Tôi là người bảo trì gói @SuperChared/FileSystem cung cấp các tiện ích hệ thống tệp thuận tiện. Các phương thức trong gói

const Path = require('path')  
const Fs = require('@supercharge/filesystem')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

await Fs.rename(oldPath, newPath)  
5 theo mặc định và không chặn vòng lặp sự kiện.

Bạn có thể sử dụng phương thức

const Fs = require('fs')  
const Path = require('path')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

Fs.renameSync(oldPath, newPath)  
8 hỗ trợ các đối số tương tự như phương thức tích hợp Node.js. Trong trường hợp này, gói
const Path = require('path')  
const Fs = require('@supercharge/filesystem')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

await Fs.rename(oldPath, newPath)  
5 chỉ chuyển các cuộc gọi qua mô -đun cốt lõi của Node.js mà không cần thêm xử lý thêm:

const Path = require('path')  
const Fs = require('@supercharge/filesystem')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

await Fs.rename(oldPath, newPath)  

Enjoy!


Tài nguyên đã đề cập

  • Node.js Docs cho mô -đun FS
  • @SuperChared/Filesystem Kho lưu trữ trên GitHub

Code ví dụ Node.js Đọc/Ghi/Sửa/Xóa file.

Module File System (fs)

Node.js hoạt động giống như 1 file server

Với module file system, ta có thể làm việc với các file trên hệ thống (đọc, tạo, sửa, xóa và đổi tên file)

Để include module file systerm ta sử dụng lệnh sau:

var fs = require('fs');

(module file system có sẵn khi cài node.js nên bạn không cần phải tải/cài đặt nó nữa)

Để đọc file ta sử dụng method 

const Path = require('path')  
const Fs = require('@supercharge/filesystem')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

await Fs.rename(oldPath, newPath)  
8

Ví dụ mình có 1 file

const Path = require('path')  
const Fs = require('@supercharge/filesystem')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

await Fs.rename(oldPath, newPath)  
9 như sau:


  
    

Node.js File Systerm module

stackjava.com

Bây giờ mình sẽ đọc nó và in ra:

var fs = require('fs');

fs.readFile('hello.html', 'utf8', function(err, data) {
  console.log(data);
});

Kết quả:

Hướng dẫn rename file nodejs - đổi tên tập tin nodejs

Lưu ý: trong method readFile có tham số thứ 2 dùng để chỉ rõ encoding, ở đây mình để là ‘ut8’, nếu bạn không chỉ rõ encoding thì nó sẽ trả về  1 raw buffer. Ví dụ trong trường hợp trên mình bỏ qua tham số encoding thì kết quả như sau: trong method readFile có tham số thứ 2 dùng để chỉ rõ encoding, ở đây mình để là ‘ut8’, nếu bạn không chỉ rõ encoding thì nó sẽ trả về  1 raw buffer. Ví dụ trong trường hợp trên mình bỏ qua tham số encoding thì kết quả như sau:

var fs = require('fs');

fs.readFile('hello.html', function(err, data) {
  console.log(data);
});

Hướng dẫn rename file nodejs - đổi tên tập tin nodejs

Việc đọc file còn áp dụng để hiển thị đọc các file html và hiển thị chúng lên web, ví dụ:

var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
  fs.readFile('hello.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    res.end();
  });
  console.log('server started!')
}).listen(8080);

Kết quả:

Hướng dẫn rename file nodejs - đổi tên tập tin nodejs

Node.js tạo file

Để tạo file trong node.js ta có 3 method sau:

  • var fs = require('fs');
    0
  • var fs = require('fs');
    1
  • var fs = require('fs');
    2

Ví dụ:

var fs = require('fs');

fs.open('demo2.txt', 'w', function (err, file) {
  if (err) throw err;
  console.log('Saved!');
});

tham số thứ 2 trong method

var fs = require('fs');
3 là ‘w’ tức là đánh dấu mở file để ghi, nếu file chưa có thì sẽ được tạo mới.

2 method fs.appendFile() và fs.writeFile() dùng cho việc ghi file nhưng trong trường hợp file được ghi chưa có thì nó sẽ tự động tạo ra file mới.

Node.js ghi file / sửa file

Để ghi file trong node.js ta sử dụng 2 method sau:

  • var fs = require('fs');
    0
  • var fs = require('fs');
    2

Method

var fs = require('fs');
6 sẽ ghi thêm vào cuối file trong khi method
var fs = require('fs');
7 sẽ ghi đè lên nội dung cũ

Ví dụ:

var fs = require('fs');

fs.appendFile('demo.txt', 'Hello content!', function (err) {
  if (err) throw err;
  console.log('Saved!');
});
const Fs = require('fs')  
const Path = require('path')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

Fs.renameSync(oldPath, newPath)  
0

tham số thứ 2 trong method var fs = require('fs');3 là ‘w’ tức là đánh dấu mở file để ghi, nếu file chưa có thì sẽ được tạo mới.

2 method fs.appendFile() và fs.writeFile() dùng cho việc ghi file nhưng trong trường hợp file được ghi chưa có thì nó sẽ tự động tạo ra file mới.

const Fs = require('fs')  
const Path = require('path')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

Fs.renameSync(oldPath, newPath)  
1

Node.js ghi file / sửa file

Để ghi file trong node.js ta sử dụng 2 method sau:

const Fs = require('fs')  
const Path = require('path')

const oldPath = Path.join(__dirname, "oldFile.txt")  
const newPath = Path.join(__dirname, "newFile.txt")

Fs.renameSync(oldPath, newPath)  
2

var fs = require('fs');2

Method

var fs = require('fs');
6 sẽ ghi thêm vào cuối file trong khi method
var fs = require('fs');
7 sẽ ghi đè lên nội dung cũ

Node.js xóa file

References:

https://www.w3schools.com/nodejs/nodejs_filesystem.asp