大众计算机学习网

专门提供各种电脑教程

JS操作客户端文件夹与文件

Admin | 2015-4-7 11:04:04 | 被阅次数 | 2001

  一、JS创建客户端文件夹

  var file;
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        if(!fso.FolderExists("c:\\trace"))
  {
     fso.CreateFolder("c:\\trace");
  }

  二、JS创建客户端文件对象

  var d=new Date();
  var fname = "c:\\trace\\MyFile"+d.toLocaleDateString()+".txt";
        if(fso.FileExists(fname)) {
            file = fso.OpenTextFile(fname,8);
        }else{
            file = fso.CreateTextFile(fname);
        }

  知识拓展:完整的例子看下文。

  function trace(orderNumber, msg){
        var file;
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        if(!fso.FolderExists("c:\\trace")) {
                fso.CreateFolder("c:\\trace");
        }
        var d=new Date();
        var fname = "c:\\trace\\MyFile"+d.toLocaleDateString()+".txt";
        if(fso.FileExists(fname)) {
            file = fso.OpenTextFile(fname,8);
        }else{
            file = fso.CreateTextFile(fname);
        }
        file.Write(orderNumber + "["+d.toLocaleTimeString()+"]"+msg+"\r\n");
        file.Close();
  }
  </script>