[英]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 package (從 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}`);
});
使用相同的節點-powershell package - 修剪數據:/
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);
});
我想使用 Powershell 因為我已經有一個命令來獲取使用修改日期排序的文件。 使用 Node.js 更難做到:/
它不必更難。 您可以使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.