Search code examples
copyjscriptfso

Copying text from one file to another using Javascript


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.


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.