是否有可能使用批处理脚本编辑config文件 [英] Is it possible to edit .Config file using a batch script

查看:245
本文介绍了是否有可能使用批处理脚本编辑config文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的config文件的某些部分,我有:

Here some part of the .config file that I have:

    <cs.components>
          <clear/>
          <cs.component name="Security"/>
            <cs.configitems>
              <cs.configitem name="sql.server.name" value="some_name" type="String" description=""/>
              <cs.configitem name="sql.server.database" value="DB_name" type="String" description=""/>
              <cs.configitem name="sql.user" value="user_name" type="String" description=""/>
              <cs.configitem name="sql.pass" value="pass" type="String" description=""/>
    </cs.components>

所以基本上我想有相应的名称编辑该文件*
* some_name =服务器1; DB_NAME =和服务器1等一个真正的数据库...
任何帮助将AP preciate!

So basically i want to edit the file with corresponding names* *some_name = server 1; DB_name=A real database on Server 1 and so on... Any help will be appreciate!

推荐答案

下面的批处理文件是一个程序的preliminary版本实现你想要的。输入和输出文件的名称是在节目中硬codeD,但是这是可以改变给他们的参数或自动处理通过外卡选择的所有文件。该程序删除空行,如果该文件包含感叹号失败,但如果需要的话,这些限制可能是固定的。

The Batch file below is a preliminary version of a program that achieve what you want. The names of input and output files are hardcoded in the program, but this can be changed to give them in parameters or to automatically process all files selected by a wild-card. The program delete empty lines and fail if the file contain exclamation marks, but these limitations may be fixed if needed.

@echo off
setlocal EnableDelayedExpansion

rem Save replacement strings
set i=0
:nextParam
   if "%~1" equ "" goto endParams
   set /A i+=1
   set "replace[!i!]=%~1"
   shift
goto nextParam
:endParams

rem Process the file
(for /F "delims=" %%a in (input.txt) do (
   set "line=%%a"
   for /L %%i in (1,1,%i%) do (
      for /F "delims=" %%r in ("!replace[%%i]!") do (
         set "line=!line:%%r!"
      )
   )
   echo !line!
)) > output.txt

要使用这个程序,给引号括起来的参数替换字符串。例如:

To use this program, give the replacement strings enclosed in quotes as parameters. For example:

replace.bat "some_name=server 1" "DB_name=A real database on Server 1"

输出:

<cs.components>
          <clear/>
          <cs.component name="Security"/>
            <cs.configitems>
              <cs.configitem name="sql.server.name" value="server 1" type="String" description=""/>
              <cs.configitem name="sql.server.database" value="A real database on Server 1" type="String" description=""/>
              <cs.configitem name="sql.user" value="user_name" type="String" description=""/>
              <cs.configitem name="sql.pass" value="pass" type="String" description=""/>
    </cs.components>

这篇关于是否有可能使用批处理脚本编辑config文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆