Pass variable from JavaScript to Windows batch file

我的梦境 提交于 2019-12-13 03:45:37

问题


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.


回答1:


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}`);
        }
});

or for extendscript:

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

and the bat file:

echo %2
move "%~1" ..

(mv is unix command but not from windows shell)



来源:https://stackoverflow.com/questions/44686123/pass-variable-from-javascript-to-windows-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!