如何将变量从批处理传递给Javascript

时间:2016-08-02 13:51:13

标签: javascript batch-file imacros

我有一个批处理文件,用于检查文件夹中的图片数量。 如何将此值返回给调用批处理文件的JavaScript?

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

以下是从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);

3 个答案:

答案 0 :(得分:2)

当您只能使用浏览器和imacros时,我不知道为什么需要这个批处理文件.js

在浏览器中打开文件夹:file:///C:/Users/Viktor/Desktop/Images/

然后在iMacros中运行此.js:

var img = new Array();

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

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

alert(img.length-1);

答案 1 :(得分:1)

您可以使用以下语法将 dir 命令的结果重定向到新的HTML文件中:

structure(list(SAMPN = c("  827", "  827", " 1133", " 1133", 
" 1133", " 1133", " 1133", " 1133", " 1857", " 1857", " 1857"
), HHVEH = c(3, 3, 2, 2, 2, 2, 2, 2, 3, 3, 3), PERNO = structure(c(2L, 
4L, 4L, 3L, 3L, 5L, 1L, 1L, 3L, 2L, 3L), .Label = c("1", "2", 
"3", "4", "5", "6", "7"), class = "factor")), row.names = c(NA, 
-11L), groups = structure(list(SAMPN = c("  827", " 1133", " 1857"
), .rows = list(1:2, 3:8, 9:11)), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))


  SAMPN   HHVEH PERNO      mode.car
   <chr>   <dbl> <fct>
 1 "  827"     3 2            2
 2 "  827"     3 4            2
 3 " 1133"     2 4            2
 4 " 1133"     2 3            2 
 5 " 1133"     2 3            NA
 6 " 1133"     2 5            NA
 7 " 1133"     2 1            NA
 8 " 1133"     2 1            NA
 9 " 1857"     3 3            2
10 " 1857"     3 2            2
11 " 1857"     3 3            2

这会将.bat结果写入您的html。

答案 2 :(得分:0)

要将变量值返回给JavaScript,请尝试从找到的变量值作为参数重新启动该批处理的JavaScript文件。在JavaScript文件中添加一个开关,以便在定义了该参数后再次绕过调用批处理。