Andrew
Andrew

Reputation: 1575

How to pass variable from batch to Javascript

I have a batch file which checks how many picture are in a folder. How can I return this value back to the JavaScript which calls the batch file?

dir /B /A-D /S "C:\Users\Viktor\Desktop\Images\*.jpg" | find /N /C /V ""
pause

Here is the code which runs the batch file from JavaScript:

var file        = null;
var process     = null;
var args        = [""];

// create an nsIFile for the executable
file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsIFile);
file.initWithPath("C:\\Users\\Viktor\\Desktop\\file.bat");

// create an nsIProcess
process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
process.init(file);

// Launch the process
process.run(false , args, args.length);

Upvotes: 2

Views: 1629

Answers (3)

Ihuda Bunzl
Ihuda Bunzl

Reputation: 7

You can redirect the result of the dir command into a new HTML file using the following syntax:

dir /B /A-D /S "C:\Users\Viktor\Desktop\Images\*.jpg" | find /N /C /V "">>yourFile.html

That will write the .bat result into your html.

Upvotes: 1

Poker Joker
Poker Joker

Reputation: 382

I don't know why you need this batch file when you can use only your browser and imacros .js

Open the folder in your browser: file:///C:/Users/Viktor/Desktop/Images/

then run this .js in iMacros:

var img = new Array();

iimPlayCode("TAG POS=1 TYPE=BODY ATTR=* EXTRACT=TXT");

img = iimGetExtract().split(".jpg");

alert(img.length-1);

Upvotes: 2

sambul35
sambul35

Reputation: 1098

To return a variable value back to JavaScript, try re-starting your JavaScript file from that batch with found variable value as an argument. In the JavaScript file add a switch to bypass calling the batch again if that argument is defined.

Upvotes: 0

Related Questions