IMacro Scripting - How to read a local .txt file using javascript

冷暖自知 提交于 2019-12-06 13:19:42

Yes you can do it with imacros, but you need to call it from javascript.js file. load your content as one block, then you can use javascript indexOf method to find the string in the text and perform if statement. Text example (inside your txt file): "hello world!"

var load;
load =  "CODE:";
load +=  "set !extract null" + "\n"; 
load +=  "SET !DATASOURCE text.txt" + "\n"; 
load +=  "SET !DATASOURCE_COLUMNS 1" + "\n"; 
load +=  "SET !DATASOURCE_LINE 1" + "\n"; 
load +=  "SET !extract {{!col1}}" + "\n";
iimPlay(load);
var s=iimGetLastExtract(0);
var index=s.indexOf("w");
if (index>0){
do your code;
}
AMON T

You have to use xml http request as Activex object of file is not supported by any other browser than IE.

This code works perfectly fine while reading local txt or any other file too.

f();
function f()
{
    var allText =[];
    var allTextLines = [];
    var Lines = [];
    var txtFile = new XMLHttpRequest();

    txtFile.open("GET", "file://D:/test.csv", true);
    allText = txtFile.responseText;
    //allTextLines = allText.split(/\r\n|\n/);//splits ur file line by line.

    //alert(allTextLines);
    txtFile.onreadystatechange = function()
    {
        if (txtFile.readyState == 4)
        {
            // Makes sure it's found the file.
            allText = txtFile.responseText;
            allTextLines = allText.split(/\r\n|\n/);

            alert(allText);
        } else { //alert("Didn't work"); 
        }
    }
    txtFile.send(null)
}

I solved it in the old fashioned way - reading line by line:

function read_file(path) {
    var content = '', l = 1, f, res = '';

    do {
        content += res && (res + "\n");
        f = "CODE: "+"\n";
        f += "SET !EXTRACT null" + "\n"; 
        f += "SET !DATASOURCE \""+path+"\" "+"\n";
        f += "SET !DATASOURCE_COLUMNS 1" + "\n"; 
        f += "SET !DATASOURCE_LINE " + l + "\n"; 
        f += "SET !EXTRACT {{!col1}}" + "\n";
        iimPlay(f);
        res = iimGetLastExtract();
        l++;
    } while (res && res != '#EANF#');

    return content;
}

var file_conten = read_file('/home/user/iMacros/templates/some_file.txt');

Hope it'll help future readers ^_^

in Firefox you can read the file directly.

more info at https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O#Line_by_line

to read file line by line use the following

var FileUtils = Components.utils.import("resource://gre/modules/FileUtils.jsm").FileUtils;

FileLocation = "C:\\myFile.txt"

var file   = new FileUtils.File( FileLocation );

// open an input stream from file
var istream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
istream.init(file, 0x01, 0444, 0);
istream.QueryInterface(Components.interfaces.nsILineInputStream);

// read lines into array
var line = {}, lines = [], hasmore;
do {
  hasmore = istream.readLine(line);
  lines.push(line.value); 
} while(hasmore);

istream.close();

// do something with read data
alert(lines);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!