Is it possible to edit .Config file using a batch script

自作多情 提交于 2019-12-01 09:11:16

问题


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>

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!


回答1:


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"

Output:

<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>



回答2:


Here is my FindandReplace.cmd script. It uses VBScript to overcome the limitations that Batch imposes. Call it like this FindAndReplace <searchstring> <replacestring> <filename or Wildcard> it is case sensitive. After you make the batch file, you would call it like this:

   FindAndReplace some_name some_new_name *.config
   FindAndReplace DB_name DB_new_name *.config


::FindAndReplace.cmd
@echo off
for /f "tokens=*" %%a in ('dir %~3 /b /a-d /on') do (
 pushd %~dp0
 if not exist _.vbs goto :MakeReplace
 <%%a cscript /nologo _.vbs "%~1" "%~2" "%~3">new.txt
 move /Y new.txt %%~nxa > NUL
 popd
)
del _.vbs
goto :eof

:MakeReplace
 >_.vbs echo Wscript.StdOut.Write Replace(WScript.StdIn.ReadAll,Wscript.arguments(0), Wscript.arguments(1))


来源:https://stackoverflow.com/questions/16465051/is-it-possible-to-edit-config-file-using-a-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!