HTML js实现修改本地文件内容(windows)

工作过程中, 不时的需要修改配置文件的内容,比如 设置访问服务器地址.因此需要逐层打开目录,来进行修改. 如果修改频繁,则会比较麻烦,因此需要一个脚本来进行修改.

但是,

var fso =new ActiveXObject("Scripting.FileSystemObject");
只限于IE浏览器, 所以以下代码只适用于IE浏览器

参考目录: 

<html>
<head>
    
    <title>修改ip</title>
    
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script language="JavaScript" type="text/javascript">

        // 需要修改的文件      var baseReal = "E:\\win32\\src\\common\\GameConfig.lua";
        // 定义一个临时文件
var baseTemp = "E:\\win32\\src\\common\\GameConfig2.lua";
        // 设置被替换的内容
var beanChange1 = "GameConfig.strHallIp = ip1"; var beanChange2 = "GameConfig.strHallIp = ip2"; var beanChange3 = "GameConfig.strHallIp = ip3"; var beanChange4 = "GameConfig.strHallIp = ip4"; var beanChange5 = "GameConfig.strHallIp = ip5";    function setServerIp(num){     var ForReading = 1, ForWriting = 2;      var fso =new ActiveXObject("Scripting.FileSystemObject");             // 创建一个临时文件       var fTemp = fso.CreateTextFile(baseTemp, true);
            // 以写入的方式进行文件打开       
var f233 = fso.OpenTextFile(baseReal, ForReading, true);       var content1;        while(!f233.AtEndOfStream){        content1=f233.ReadLine();        fTemp.WriteLine(content1);        }        f233.Close();        fTemp.Close();        var fdelete3 = fso.GetFile(baseReal);        // 删除文件        fdelete3.Delete();           var fnew = fso.CreateTextFile(baseReal, false);        fTemp = fso.OpenTextFile(baseTemp, ForReading, false);          var content1;          var hasChange = false;          while(!fTemp.AtEndOfStream){          content1 = fTemp.ReadLine();          if(!hasChange && (content1 == beanChange1 ||content1 == beanChange2              ||content1 == beanChange3              ||content1 == beanChange4              ||content1 == beanChange5          )){            fnew.WriteLine(getContent(num));            hasChange = true;          }else{            fnew.WriteLine(content1);          }         }         fnew.Close(); fTemp.Close(); var fdelete2 = fso.GetFile(baseTemp); fdelete2.Delete(); } function getContent(num){ switch(num){ case 1: return beanChange1; case 2: return beanChange2; case 3: return beanChange3; case 4: return beanChange4; case 5: return beanChange5; } } </script> </head> <body>   <div style="display: ;" id= "div1" >     <input id="button1" type="button" onclick="setServerIp(1)" value="IP1">   </div>   <br/>   <div style="display: ;" id= "div2" >     <input id="button1" type="button" onclick="setServerIp(2)" value="IP2">   </div>   <br/>   <div style="display: ;" id= "div3" >     <input id="button1" type="button" onclick="setServerIp(3)" value="IP3">   </div>   <br/>   <div style="display: ;" id= "div4" >     <input id="button1" type="button" onclick="setServerIp(4)" value="IP4">   </div>   <br/>   <div style="display: ;" id= "div5" >     <input id="button1" type="button" onclick="setServerIp(5)" value="IP5">   </div> </body> </html>

 

posted on 2017-02-14 10:41  溪圆圆  阅读(4078)  评论(0编辑  收藏  举报

导航