Forum Discussion

whitedaisy's avatar
14 years ago

Help on aqTextFile.SetPosition method

Hi all,



My purpose is that write the result into the end of each row in CSV file.

And I write a below script.


function test()


test()



    var Driver, path;


    path=Files["FileNameByName"]("test_csv");


 


    Driver = DDT["CSVDriver"](path);


    while (! Driver["EOF"]() )


    {


        WriteCSV(Project["Variables"]["line"],"T",path);


        Project["Variables"]["line"]++;


        Driver["Next"](); 

    }


 


    DDT["CloseDriver"](Driver["Name"]);


}



function WriteCSV(line,result,filepath)


WriteCSV(line,result,filepath)

{

    var myFile = aqFile["OpenTextFile"](filepath, aqFile.faReadWrite, aqFile.ctANSI);

    var lLength = myFile.LineLength(line)+1;

    if (myFile["SetPosition"](line,lLength))

        myFile["Write"](result); 

    myFile["Close"]();


}



The original of Project["Variables"]["line"] is 1.

The problem is that the result of statement "if (myFile["SetPosition"](line,lLength))" is always False

This means I can't move the current cursor to a specific position



Could anybody help me to explain about this and correct it?



Please help me to give another solution if I'm using a incorrect way.



The attachment is file which I want to add the result into the end of each row



Thanks so much.

Hoa.

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    What you might want to do is look at using a combination of several methods.  



    First, you're already using aqFile.OpenTextFile.  The resulting object has a method called "ReadLine" which will read an entire line from the file into a variable.  Because ReadLine automatically moves the cursor to the next line in the file, you won't need to use a DDT.CSVDriver.  Instead, you would use a while loop to loop on "IsEndOfFile".  



    So, now you have a loop that loops through all the rows of the file and reads each line out in turn.  Because each line is a CSV record, you essentially have a delimited string list.  This means you can use methods like GetListLength to get the number of items in the list and then use the method AddListItem to add an item to the end of the delimited list.



    Combining all these methods together into JScript code would look something like this:

    function AppendToEndOfLine()





    {

        var NewText = "MyNewText"

        var InputFile = aqFile.OpenTextFile("C:\\Temp\\Test.CSV", aqFile.faRead, aqFile.ctANSI, false);

        var OutputFile = aqFile.OpenTextFile("C:\\Temp\\NewTest.CSV", aqFile.faWrite, aqFile.ctANSI, true);

        var StringList;

        aqString.ListSeparator = ",";  

        // The next three lines assume that the first row of the file contains the column headers and we need to add a new header to the end of the line

        StringList = InputFile.ReadLine();

        StringList = aqString.AddListItem(StringList, "Column5", -1)

        OutputFile.WriteLine(StringList)

        //If the first row of your file is not the column headers, you don't need the above three lines and you can simply go to the while loop

        while (!InputFile.IsEndOfFile())

        {

            StringList = InputFile.ReadLine();

            StringList = aqString.AddListItem(StringList, aqString.Quote(NewText), -1);

            OutputFile.WriteLine(StringList);

        }

        InputFile.Close();

        OutputFile.Close();

        //The next two lines simply replace the old file with the new one so that the end result of the function is your new file with your new data.

        aqFile.Delete("C:\\Temp\\Test.CSV");

        aqFile.Rename("C:\\Temp\\NewTest.CSV", "C:\\Temp\\Test.CSV", true);

    }




    You'd have to make some modifications, obviously, for your file names and so on but this should give you what you're looking for.


  • Hi,

     

    Robert is right. In your case, it is better to use the approach Robert recommended.


    As for your original problem with the aqTextFile.SetPosition method, the method returns False as you try to set position to the non-existent element - the element with indexes (line, aqTextFile.LineLenght(line)+1):




    var lLength = myFile.LineLength(line) + 1;

    if (myFile["SetPosition"](line, lLength))

      …


    By using the SetPosition method, you can only move the cursor position to the existing element in the document, that is, the element with indexes (Line, Column), where Line can be specified from the range 0…aqTextFile.LinesCount-1, Column - from the range 0…aqTextFile.LineLength(line)-1.