将变量从JavaScript传递到Windows批处理文件 [英] Pass variable from JavaScript to Windows batch file

查看:73
本文介绍了将变量从JavaScript传递到Windows批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在JavaScript中创建变量并将其传递给批处理文件?就像一个简单的测试回显一个变量并将文件上移目录一样.

Is it possible to create a variable in JavaScript and pass it to a batch file? Just as a simple test echo a variable and move a file up a directory.

JavaScript.js

var s = "Gwen Stefani";
var myFile = "C:\\temp\\myfile.txt"
myBat.execute();

myBat.bat

echo s 
move myFile ..

一种替代方法是创建一个保存为批处理文件的字符串,然后执行该字符串,但是我想知道是否可以直接完成该字符串.

An alternative is to create a string which is saved out as a batch file and then executed, but I was wondering if if it could be done directly.

推荐答案

您可以使用命令行参数(因为您正在使用exec,我想这是node.js):

You can use command line arguments (as you are using exec I suppose this is node.js):

var s = "Gwen Stefani";
var myFile = "C:\\temp\\myfile.txt"
const exec = require('child_process').exec;
const child = exec('cmd /c myBat.bat '+ myFile+' '+s,
    (error, stdout, stderr) => {
        console.log(`stdout: ${stdout}`);
        console.log(`stderr: ${stderr}`);
        if (error !== null) {
            console.log(`exec error: ${error}`);
        }
});

或扩展脚本:

var s = "Gwen Stefani";
var myFile = "C:\\temp\\myfile.txt";
system.callSystem('cmd /c myBat.bat '+ myFile+' '+s');

和bat文件:

echo %2
move "%~1" ..

(mv是unix命令,但不是Windows shell的内容)

(mv is unix command but not from windows shell)

这篇关于将变量从JavaScript传递到Windows批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆