本文概述
为了完成此任务, 我们将使用Node.js的child_process类。在child_process中, 我们将使用spawn函数, 该函数允许我们在后台执行进程并添加不同的事件侦听器。在这里阅读更多关于spawn函数的信息。
批量
在本文中, 我们假设你对批处理有一定的了解, 但是你需要知道, 要将批处理中的信息发送到我们的Electron App, 你需要使用echo来输出要发送的文本。
所有通过echo打印的文本都将通过child_process的stdout.on(” data”)事件检索。
echo This text will be sent as Uint8Array for being processed later by your Electron App
REM Note that you can send 2 types of data (normal and error)
REM To send normal data
echo Hello world 2>&1
REM To send error data (but don't stop the execution)
echo This text should ouput an error 1>&2
要处理Electron的on(” exit”)事件并批量发送其他退出代码, 请使用以下行(根据你的需要更改数字):
EXIT /B 1
Electron
如前所述, 从stderr和stdout检索的数据将具有Uint8Array格式, 因此我们需要将Uint8Array转换为字符串Javascript(如果需要数组格式, 则不要对其进行转换)。要转换, 请使用以下行
var myReceivedData = ; // This will be the Uint8Array Data
var strData = String.fromCharCode.apply(null, myReceivedData); // The array is now human readable text
注意:上一个代码段适用于普通数据。如果你的数据量很大, 则可能需要更改数据的转换方式, 请在此处详细了解如何将Uint8Array转换为字符串。
现在我们已经了解了基础知识, 让我们执行该批处理!
执行批处理文件
对于此示例, 我们的批处理文件将为以下文件(为Windowselectronexample.bat编写):
@echo off
REM The name of the file that will be created
set filename=electronfileexample.txt
REM the path where the file will be created
set filepath=c:\
REM the content of the file
set content=Hello, this is the content of my file created with batch
REM the full path (path+filename)
set fullpath=%filepath%%filename%
echo Sending a "JSON" String
echo {"filename":"%filename%", "content":"%content%", "fullpath":"%fullpath%"}
REM Text to send to stdout (data)
REM Note that you can append 2>&1 to the normal stdout
REM echo This text will be sent to my Electron Project
REM or
REM echo This text will be sent to my Electron Project 2>&1
REM Text to send to stderr (error) 1>&2
REM echo This text should ouput an error 1>&2
echo An error message passing by, nothing important :) Just ignore me 1>&2
IF EXIST %filepath%%filename% (
echo File already exists
EXIT /B 1
) ELSE (
@echo Creating file in %fullpath%
(
echo %content%
) > %filepath%%filename%
)
REM Check if the file was created, if exists send code 2
REM if the file doesn't exist then send code 3 (error while creating)
IF EXIST %filepath%%filename% (
EXIT /B 2
) ELSE (
EXIT /B 3
)
一个简单的代码片段, 它将检查c:/路径中是否存在文件, 如果存在, 则发送一条消息, 指出”文件已存在”, 否则创建文件并发送消息”在c:/electronfileexample.txt中创建文件”。然后检查它是否存在, 如果存在, 则用代码2完成执行, 如果不存在, 则用代码3退出。
使用Electron(使用Node.js), 你将需要创建一个子进程。与Python不同, node.js是异步的, 这意味着它无需等待script.bat完成。而是调用script.bat打印数据或存在时先前定义的函数。
现在要使用javascript处理批处理, 请使用以下代码:
"use strict";
// The path to the .bat file
var myBatFilePath = "C:\\Users\\SDkCarlos\\Desktop\\electrontest.bat";
const spawn = require('child_process').spawn;
const bat = spawn('cmd.exe', ['/c', myBatFilePath]);
// Handle normal output
bat.stdout.on('data', (data) => {
// As said before, convert the Uint8Array to a readable string.
var str = String.fromCharCode.apply(null, data);
console.info(str);
});
// Handle error output
bat.stderr.on('data', (data) => {
// As said before, convert the Uint8Array to a readable string.
var str = String.fromCharCode.apply(null, data);
console.error(str);
});
// Handle on exit event
bat.on('exit', (code) => {
var preText = `Child exited with code ${code} : `;
switch(code){
case 0:
console.info(preText+"Something unknown happened executing the batch.");
break;
case 1:
console.info(preText+"The file already exists");
break;
case 2:
console.info(preText+"The file doesn't exists and now is created");
break;
case 3:
console.info(preText+"An error ocurred while creating the file");
break;
}
});
控制台的输出应类似于(没有HTML标记, 只能看到右侧的控制台):
你可以在此处的官方ourcodeworldElectron示例存储库” electron-batch-childprocess”中看到之前的示例。玩得开心
评论前必须登录!
注册