1. 程式人生 > >Node.js-檔案模組之其他操作

Node.js-檔案模組之其他操作

9. 其他操作

(1)絕對路徑

  • Fs.realpath()
  • Fs.realpathSync()

例子:

// 匯入檔案系統
const fs = require('fs');
fs.realpath('file/4.txt',function (err,path) {
    if (err) throw  err;
    console.log(path);
});

(2)子程序執行作業系統命令

例子:

// 匯入子程序模組
const cp = require('child_process');
// 操作cmd命令,新建資料夾
cp.exec('mkdir zhang',function () {
    console.log('檔案建立成功');
});
// window不支援該命令-刪除,Linux系統支援
cp.exec('rm -rf d');