Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read line from file, append to line, display new string/line? 1

Status
Not open for further replies.

qjade

Programmer
Jun 7, 2004
42
US
Hello friends,
I am attempting to open a .txt file and read in its contents. With every line from the file, I would like to append a comma and single-quote to the beggining, and a single quote at the end of the string/line.
example:
Original file:
Apple
Orange
Plum

Expected result:
,'Apple'
,'Orange'
,'Plum'

Please take a look at my code and let me know what I am doing wrong. Thanks a milion.


<html xmlns:javaScriptDownload>
<head>
<title>Appending quotations and commas to Elite folder Id</title>
<script type="text/javascript">

// set the hidden field's value to the contents of the text file
// we store the text here after the download.

function setDlProc(obj) {
document.all.download.value=obj;
}

// parse over the contents, since we want to have each fruit on it's own line
// we replace all newline characters(\n) with an HTML newline(<BR>)

function writeToDoc(){
var re = new RegExp ("\n","g");

str = document.getElementById("download").value.replace(re,"'<br>,'");
// str = str.replace(re,"<br>");
document.getElementById('textFileCOntentsOnPage').innerHTML = str;
}


function runTest(){
// invoke the behavior on the namespace specified download page object


document.getElementById('fileDlBehavior').startDownload('test.txt',setDlProc);

// set the contents of the display area to show that we're getting the file
document.getElementById('textFileCOntentsOnPage').innerHTML = "Downloading

the text file...";

// write the contents of the text file to the page display area
setTimeout("writeToDoc()", 1500);
}
</script>


</head>
<body>

<!- the the button you click to invoke the test -->
<input type='button' value='Press To Run Program' onclick="runTest()"><hr>

<!- the namespace specified download page object -->
<javaScriptDownload:download id="fileDlBehavior"

style="behavior:url(#default#download);" />

<!- the hidden field that stores the text file contents after download -->
<input type='hidden' id="download" name='download' style='visibility:hidden'>

<!- The display area for the test results -->
<span id='textFileCOntentsOnPage' name='textFileCOntentsOnPage'></span>
</body>
</html>
 
i think instead of using replace, you want to split the field.

Code:
var reg = /\n/g;
y = document.getElementById("download").value
var str;
var y = x.split(reg);
for(var i = 0; i < y.length; i++)
{
	str += ",'" + y[i] + "'<br>"
}
document.getElementById('textFileCOntentsOnPage').innerHTML = str;

something along those lines


aschuster@sftsolutions.com
 
i mean
Code:
var x = document.getElementById("download").value

aschuster@sftsolutions.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top