0

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.

4
  • stackoverflow.com/questions/32807609/… I think this answers the question. Commented Jun 21, 2017 at 20:57
  • yes, > doit.bat 123 will set %1 to 123 inside of doit.bat Commented Jun 21, 2017 at 21:17
  • What is the mv command you are using in the batch file? do you mean move instead? Commented Jun 21, 2017 at 22:25
  • All the answer assume node.js or the browser environment. Are you some adobe application? Would be good to add this to your question. Commented Jun 22, 2017 at 3:24

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.