ReferenceError: file is not defined

Hi,

Following is my code,

var assert = require('assert');
     const path = require("path");
     const fs = require("fs");
//const  F5 = artifacts.require("F5")
//const ST5= artifacts.require("ST5")

module.exports = async function(callback) {
try {
     var ctr =0;
     const keyWordStr = [];
     const files = new Array("A1.sol");
     const keyWords = new Array("string1",  "string2", "string3","string4", "string5", "string6", "string7");
     const str = 0;
     for (let i = 0; i < files.length; i++) {   
        const contractPath = path.resolve('/home/zulfi/Truffle_programs/search_opcode/','contracts',files[i]);
        console.log(file[0]);
        const contractCode2 = fs.readFileSync(contractPath, "utf8");
        let lines =   contractCode2.split(/\r\n|\n/); 
        for(var line = 0; line < lines.length-1; line++){
           keyWordStr[ctr] = (lines[line].search(keyWords[ctr] >= 0) ? lines[line] : false);
           ctr++;
           console.log("ctr=" + ctr+ "line =" + line + " --> "+ keyWordStr[ctr]);
        }//for (var...)   
       }//for(let..)     
}//try
  catch(error) {
    console.log(error)
  }

  callback()
}

==
I am getting the following error message:

$ truffle exec tool4.js
Using network ‘development’.

ReferenceError: file is not defined
at module.exports (/home/zulfi/Truffle_programs/search_opcode/tool4.js:16:21)
at Object.exec (/home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/packages/require/require.js:127:1)
at Promise (internal/util.js:274:30)
at new Promise ()
at bound exec (internal/util.js:273:12)
at Object.run (/home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/exec.js:80:1)
at process._tickCallback (internal/process/next_tick.js:68:7)

Line#16 is: console.log(file[0]);

In the above line, I want to print the name of file, please guide me how to correct this line:

$ ls /home/zulfi/Truffle_programs/search_opcode/contracts

A1.sol A2.sol A3.sol Migrations.sol tx_origin1.sol

Thus, I want to print “A1.sol” using the command: console.log. Somebody please guide me how to achieve this.

Zulfi.

Hi,
It should be : console.log(files[0]);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.