Re: Stupid question

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Alex Mieles" <AlexMieles@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:815F99D3-C51F-4F15-8073-4A1439F682B7@xxxxxxxxxxxxxxxx
Works great. Thank you very much for the assistance, that proved a bit
complicated for me. Enjoy your day very much.

Alex

Here's just the script but with more comments added.

<script type="text/javascript">

// Declare variables

var bool = false; // Does file end with a CrLf?

var CrLf = "%0D%0A"; // CrLf after escape()

var Repl = "Usermaster,"; // Replacement string

var Folder = "C:\\Temp\\"; // Path to files

var ForReading = 1;
var ForWriting = 2;

var fs = new ActiveXObject("Scripting.FileSystemObject");

// Read file

var df = fs.OpenTextFile(Folder + "stupid.csv", ForReading);

var ra = df.readAll(); // Read the entire file into a variable

df.Close(); // Close the file

// Prepend each line with value of "var Repl"

var re = escape(ra); // escape() the value of the variable

re = CrLf + re; // prepend CrLf to the value of the variable

// Determine if the last line of the file is CrLf
if (re.substr(re.length-CrLf.length) == CrLf) bool = true;

// Replace each instabce of CrLf with CrLf plus the Replacement string
re = re.replace(/\%0D\%0A/g,CrLf + Repl);

// Remove the CrLf that was prepended
re = re.substr(CrLf.length);

// If the last line of the file was CrLf then
// remove the Replacement string that was added to it
if (bool) {
re = re.substring(0,re.length-Repl.length);
}

// unescape() the value
re = unescape(re);

// Write file

var of = fs.OpenTextFile(Folder + "stupid.txt", ForWriting, true);
of.writeLine(re);
of.Close();

// Before and After

alert(ra + "\n\n" + re)

</script>

Basically, each CrLf is replaced with
"CrLf plus the Replacement string" and
because we want this done on the first line too
a CrLf is prepended (inserted before) the
value of the contents of the file. Afterwards
it is removed. Also, it the last line is just a
CrLf then the Replacement string isn't wanted after it.

Does this explanation help you understand?


.