2

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);
6
  • You can't do that in JavaScript, but in node.js. Commented Aug 2, 2016 at 13:53
  • 1
    What do you mean pass to JS? Do you have a node script? Commented Aug 2, 2016 at 13:53
  • As your folder in on your desktop, I assume you want to get this value from your browser... that's not possible. Commented Aug 2, 2016 at 13:54
  • What do you mean by "pass to JavaScript"? This isn't very clear. Copy and paste the results into a JS file? shrugs Unless those pictures are inside a web, then it's not likely you'd even be able to do this server side. Commented Aug 2, 2016 at 13:55
  • @Pierre C I have a code that runs Batch file from Js and i'm getting amount of pictures from folder i want pass this parametr to js variable, updated code Commented Aug 2, 2016 at 13:58

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

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.