Ghoul Fool
Ghoul Fool

Reputation: 6937

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 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.

Upvotes: 0

Views: 2584

Answers (1)

npocmaka
npocmaka

Reputation: 57252

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)

Upvotes: 1

Related Questions