1

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.

Teemu
  • 22,918
  • 7
  • 53
  • 106
May12
  • 2,420
  • 12
  • 63
  • 99
  • This is something that should normally be handled server side. – Event_Horizon Apr 26 '12 at 13:27
  • Why not simply use a batch one-liner for this? `copy C:\Temp\26_04_2012_16_22_49\ext.txt+C:\FILE\back_log.log C:\Temp\26_04_2012_16_22_49\ext.txt` or `type C:\FILE\back_log.log >> C:\Temp\26_04_2012_16_22_49\ext.txt` – Helen Apr 26 '12 at 14:02
  • @Event_Horizon: The OP means JScript used in Windows Script Host (similar to VBScript, PowerShell or batch files), not JavaScript in browsers. – Helen Apr 26 '12 at 14:46
  • Interesting, did not know you could do batch file stuff with js. – Event_Horizon Apr 26 '12 at 15:28

3 Answers3

1

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.

Teemu
  • 22,918
  • 7
  • 53
  • 106
0

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

Marc
  • 11,403
  • 2
  • 35
  • 45
0

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.

Clem
  • 69
  • 1
  • 8