Question

I wrote a script (.js) which should copy all text from one file to another, but it doesn't work (i run it on hard disc):

var fso = new ActiveXObject("Scripting.FileSystemObject");
var myInputTextStream = fso.OpenTextFile("C:\\FILE\\back_log.log", 1, true);
var log = "C:\\Temp\\26_04_2012_16_22_49\\ext.txt";     
    var myOutputTextStream = fso.OpenTextFile(log, 8, true);
    while(myInputTextStream.AtEndOfStream)
        {
      myOutputTextStream.Write(myInputTextStream.ReadAll());
    }
      //myInputTextStream.Close();
      //myOutputTextStream.Close(); 
    WScript.Echo("FINISH!!!");

Could anybody coorrect me (or code=))? Thanks a lot.

Was it helpful?

Solution

myInputTextStream.AtEndOfStream is false until reading reaches the EOF. Hence your while-loop is never executed.

If you use ReadAll(), you don't need the while-loop at all.

You should also never comment out your Close()-methods, you may get troubles, especially when using portable memory devices like SDI-cards etc.

OTHER TIPS

You can copy a file like this:

var fso, f;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.CreateTextFile("c:\\testfile.txt", true);
f.WriteLine("This is a test.");
f.Close();
f = fso.GetFile("c:\\testfile.txt");
f.Copy("c:\\windows\\desktop\\test2.txt");

(This creates a file and then copies it, so just use the useful part, which is contained in the last 2 lines.)

Excerpt taken from here: http://msdn.microsoft.com/en-us/library/6973t06a%28v=VS.85%29.aspx

since javascript cannot access the local "C" drive, not sure where you would use this. SInce you will be running this locally on your machine, why not just use a dos command, vbscript or wscript? javascript is overkill.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top