Forum Discussion
tristaanogre
14 years agoEsteemed 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:
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.
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.
Related Content
- 2 months ago
- 4 years ago
- 9 years ago
- 7 years ago
Recent Discussions
- 3 days ago
- 3 days ago
- 7 days ago