18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

Get the number of rows from a file

GayathriGayathri Members
edited January 2013 in Sahi - Open Source
I have a .CSV file with data entered. The file is in /userdata/scripts folder.

Please let me know how to get the number of rows (with data) from this file.

Answers

  • gaveyomgaveyom Members
    edited January 2013
    Hi Gayathri,

    hope the below code will help u

    
    
    var $writeData = [];
    var $data_CSV = [];
    
    var $csvFilePath = "D:/csvfile.csv";
    var $data_CSV = _readCSVFile($csvFilePath);
    
    try {
    
    	for(var $cnt_Row=0; $cnt_Row<$data_CSV.length; $cnt_Row++) {
    	
    		for(var $cnt_Clm=0; $cnt_Clm<$data_CSV[$cnt_Row].length; $cnt_Clm++) {
    
    			$writeData[$cnt_Row][$cnt_Clm] = $data_CSV[$cnt_Row][$cnt_Clm] +"append your manipulated content here";
    		}
    	}
    }catch($e) {
    	_logExceptionAsFailure("Exception: "+ $e);
    }
    
    // you can write manipulated array data($writeData) into another csv file by using _writeCSVFile
    _writeCSVFile($writeData, "ManipulatedData.csv", true);
    
    
    
    
  • Thanks. I reused yours with just one loop...

    The below one (with just one 'For' loop) make it much simpler for my requirement.

    var $data_CSV = [];
    var $csvFilePath = "getRowCount.csv";
    var $data_CSV = _readCSVFile($csvFilePath);
    try
    {
    for(var $cnt_Row=0; $cnt_Row<$data_CSV.length; $cnt_Row++)
    {
    //_alert ($cnt_Row);

    }
    _alert ("Total rows in the input file is " +$cnt_Row);
    }
    catch($e)
    {
    _logExceptionAsFailure("Exception: "+ $e);
    }

    Thanks. It helped.
  • Hi Gayathri,

    one more thing i wanna update.

    if u wanna know only Total rows from the file then u can use as

    _alert("Total rows in the input file is: "+ $data_CSV.length); // instead of $cnt_Row

    Below is the modified total code
    
    var $data_CSV = [];
    var $csvFilePath = "getRowCount.csv";
    var $data_CSV = _readCSVFile($csvFilePath);
    _alert ("Total rows in the input file is: "+ $data_CSV.length);
    
    
  • ya. This is much better even without the loop. (I had been assuming that the variable object declared to read the file would read the complete 'contents' of the file - as we use in java.)

    Thanks for the update. It works now.
Sign In or Register to comment.