Skip to content Skip to sidebar Skip to footer

Pass Variable From Javascript To Windows Batch File

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 Ste

Solution 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)

Post a Comment for "Pass Variable From Javascript To Windows Batch File"