1 (изменено: ReVeRSeR, 2016-03-17 17:20:37)

Тема: CMD/BAT: Как сделапть изменение данных в строке XML-файла

Добрый день.
Не могу понять как написать скрипт по изменению строки в файле.
У меня есть файл Web.config в котором некоторый текст:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    <section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <rewriter configSource="WebRewriter.config" />
  <connectionStrings configSource="WebConnections.config" />
  <log4net configSource="WebLogging.config" />
  <appSettings configSource="WebSettings.config" />
  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/service_config/Account/Login.aspx" timeout="2880">
        <credentials passwordFormat="Clear">
          <user name="admin" password="admin" />
        </credentials>
      </forms>
    </authentication>
    <compilation debug="true" targetFramework="4.5">
    </compilation>
    <httpModules>
      <add type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" name="UrlRewriter" />
    </httpModules>
    <pages controlRenderingCompatibilityVersion="4.0" />
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <!--test-->
          <add name="10seconds" duration="10" varyByHeader="Accept" varyByParam="guid" />
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
  </system.web>
</configuration>

Из всего этого кода мне надо найти строку

<user name="admin" password="admin" />

И заменить её на

<user name="admin" password="smenapass" />

Как решить данную задачу не могу понять.

Заранее спасибо за помощь.

2

Re: CMD/BAT: Как сделапть изменение данных в строке XML-файла

Можно из бата вызвать PowerShell:

@powershell /nop /ex bypass "&{[Regex]::Replace([IO.File]::ReadAllText(file_full_path), '(?<=admin.*)admin', 'changepass')}"

или sed, если есть Cygwin:

sed 's/admin/changepass/2' file

и перенаправить вывод в файл. На чистом командном языке реализовывать сие слишком утомительно.

3

Re: CMD/BAT: Как сделапть изменение данных в строке XML-файла

gsar.exe -spassword="""admin -rpassword="""smenapass -o Web.config
Post's attachments

gsar.7z 9.45 kb, 5 downloads since 2016-03-17 

You don't have the permssions to download the attachments of this post.

4

Re: CMD/BAT: Как сделапть изменение данных в строке XML-файла

Flasher пишет:

gsar.exe -spassword="""admin -rpassword="""smenapass -o Web.config

Красиво.

5

Re: CMD/BAT: Как сделапть изменение данных в строке XML-файла

Можно ещё красивей:

TxtReplace.exe Web.config password="admin password="smenapass

А ещё есть far (меньше gsar), но кавычки экранировать не хочет.
А вообще для работы с xml оптимальной тулзой считается Tidy.

Post's attachments

far.7z 28.4 kb, 3 downloads since 2016-03-17 

You don't have the permssions to download the attachments of this post.