在 Node.js 中修剪了 powershell 命令行

Trimmed powershell command lines in Node.js

我需要使用 Node.js 使用 Powershell 读取一些数据。我尝试了几个不同的包来使用 Node.js 中的 powershell 命令并从控制台获取数据,但每次我从 Powershell 获取数据时,数据都会被修剪。

比如我的命令是:

Get-ChildItem -Exclude *procesy | sort CreationTime -desc

Powershell window 中的正确结果是:

PS C:\Data> Get-ChildItem -Exclude *procesy | sort CreationTime -desc                  

Directory: C:\Users\Misiek\Desktop\Skrypty\!Backup


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        01.01.2021     03:22            966 01.01.2021 03-22-52 - katalogi Explorer.txt
-a----        01.01.2021     03:22           5784 01.01.2021 03-22-52 - otwarte procesy.txt
-a----        01.01.2021     03:32           1010 01.01.2021 03-32-54 - katalogi Explorer.txt
-a----        01.01.2021     03:32           5478 01.01.2021 03-32-54 - otwarte procesy.txt
-a----        01.01.2021     03:42           1064 01.01.2021 03-42-55 - katalogi Explorer.txt
-a----        01.01.2021     03:42           5820 01.01.2021 03-42-55 - otwarte procesy.txt
-a----        01.01.2021     03:52           1064 01.01.2021 03-52-57 - katalogi Explorer.txt
-a----        01.01.2021     03:52           5460 01.01.2021 03-52-57 - otwarte procesy.txt
-a----        01.01.2021     04:02           1064 01.01.2021 04-02-58 - katalogi Explorer.txt
-a----        01.01.2021     04:02           5462 01.01.2021 04-02-58 - otwarte procesy.txt
-a----        01.01.2021     04:13           1064 01.01.2021 04-13-00 - katalogi Explorer.txt
-a----        01.01.2021     04:13           5464 01.01.2021 04-13-00 - otwarte procesy.txt
-a----        01.01.2021     04:23           1064 01.01.2021 04-23-01 - katalogi Explorer.txt
-a----        01.01.2021     04:23           5462 01.01.2021 04-23-01 - otwarte procesy.txt
-a----        01.01.2021     04:33           1064 01.01.2021 04-33-03 - katalogi Explorer.txt
-a----        01.01.2021     04:33           5462 01.01.2021 04-33-03 - otwarte procesy.txt
-a----        01.01.2021     04:43           1064 01.01.2021 04-43-05 - katalogi Explorer.txt
-a----        01.01.2021     04:43           5462 01.01.2021 04-43-05 - otwarte procesy.txt

但我在 Node.js 控制台 window:

中得到了修剪后的结果
C:\!Temp\ExplorerFolderManager>npm run start

> ExplorerFolderManager@1.0.0 start C:\!Temp\ExplorerFolderManager
> electron .



    Directory: C:\Users\Misiek\Desktop\Skr
    ypty\!Backup


Mode                 LastWriteTime  Length
----                 -------------  ------
-a----        01.01.2021     03:22     966
-a----        01.01.2021     03:22    5784
-a----        01.01.2021     03:32    1010
-a----        01.01.2021     03:32    5478
-a----        01.01.2021     03:42    1064
-a----        01.01.2021     03:42    5820
-a----        01.01.2021     03:52    1064
-a----        01.01.2021     03:52    5460
-a----        01.01.2021     04:02    1064
-a----        01.01.2021     04:02    5462
-a----        01.01.2021     04:13    1064
-a----        01.01.2021     04:13    5464
-a----        01.01.2021     04:23    1064
-a----        01.01.2021     04:23    5462
-a----        01.01.2021     04:33    1064
-a----        01.01.2021     04:33    5462
-a----        01.01.2021     04:43    1064
-a----        01.01.2021     04:43    5462

我尝试了一个 node-cmd 包(来自 powershell 控制台的修剪数据)。 使用 child_process 我也得到修剪数据:

const xxxx = spawn('powershell.exe', ['Get-ChildItem', '-Path', dir], {
});

xxxx.stdout.on('data', (data) => {
    onePrintStringData = data.toString();
  console.log(onePrintStringData);
});

exec('powershell "Get-ChildItem -Path ' + dir + ' -Exclude *procesy.txt"', (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
});

同样使用 node-powershell 包 - 修剪数据:/

const ps = new Shell({
  executionPolicy: 'Bypass',
  noProfile: true
});

ps.addCommand('powershell "Get-ChildItem -Path ' + dir + ' -Exclude *procesy.txt"');
ps.invoke()
.then(output => {
  console.log(output);
})
.catch(err => {
  console.log(err);
});

I would like to use a Powershell because I already have a command to get files with sorting using modify date. Using Node.js it is some harder to do :/

不必更难。您可以使用 fs.readdir 获取文件名,然后使用 fs.stat 获取修改日期,然后按修改日期排序,然后提取文件名,如下所示:

const fs = require('fs');
const path = require('path');

const dir = '.';

const files = fs.readdirSync(dir)
  .map(fileName => ({
    name: fileName,
    time: fs.statSync(path.join(dir, fileName)).mtime.getTime()
  }))
  .sort((a, b) => a.time - b.time)
  .map(v => v.name);

console.log(files);