使用文件名作为属性值(Powershell或批处理文件)搜索和替换XML属性 [英] Search and Replace XML attribute using file name as the attribute value (Powershell or Batch File)

查看:92
本文介绍了使用文件名作为属性值(Powershell或批处理文件)搜索和替换XML属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助来查找和替换文件夹中每个XML文件中具有"00000000-0000-0000-0000-0000-000000000000"的属性"parentid".每个不带扩展名的文件的实际文件名应替换XML中的"parentid"属性. (PowerShell或批处理文件)

例如:C:\ Folder \

4de6d1b4-b014-4cb3-bb61-649737a8217b.xml
4d2345b4-b415-5656-12dv-832jnm89234n.xml
...

XML内容:

   <comment id="4de6d1b4-b014-4cb3-bb61-649737a8217b" parentid="00000000-0000-0000-0000-000000000000" approved="True">
      <date>2014-12-20 22:23:40</date>
      <author>Joe Doe</author>
      <email>test@example.com</email>
      <country />
      <ip>0.0.0.0</ip>
      <avatar />
      <content>Test</content>
    </comment>

我感谢大家的帮助!

到目前为止,这是我所拥有的,但是它代替了整行而不是属性的实际值.

@echo off &setlocal
setlocal enabledelayedexpansion

set "search=00000000-0000-0000-0000-000000000000"
set "replace=%%~nP"
set "textfile=%%~nxP"
set "newfile=%%~nP"
for /r %%P in (*) do (
(for /f "delims=" %%i in (%textfile%) do (
    set "line=%%i"
    set "line=!line:%search%=%replace%!"
    echo(!line!))>"%newfile%"
)
)
endlocal


@ECHO ON

解决方案

基本问题是,您引用的是循环范围之外的for变量,即%%P,我不建议这样做./p>

使用纯批处理脚本,我会将您的代码更改为此:

 @echo off
setlocal EnableExtensions DisableDelayedExpansion

set "SEARCH=00000000-0000-0000-0000-000000000000"
for %%P in ("*.xml") do (
    for /F "delims=" %%I in ('
        findstr /N /R "^" "%%~P" ^& ^> "%%~fP" break
    ') do (
        set "LINE=%%I"
        setlocal EnableDelayedExpansion
        set "LINE=!LINE:*:=!"
        if defined LINE set "LINE=!LINE:%SEARCH%=%%~nP!"
        >> "%%~fP" echo(!LINE!
        endlocal
    )
)
endlocal
exit /B
 

这能够直接在当前目录中修改枚举的*.xml文件,因此不需要临时文件即可替换属性parentid.

每个文件都由findstr读取,该文件在每行之前都有一个行号和一个冒号,以免丢失空行(请记住,for /F会忽略此行).命令行set "LINE=!LINE:*:=!"之后会删除前缀.请注意,在for /F中切换了延迟扩展,以免丢失文件中出现的任何感叹号.这些措施确保了*.xml原始文件的内容除了被替换的部分之外不会被更改.

I need help to find and replace an attribute "parentid" that has "00000000-0000-0000-0000-000000000000" in each XML file in a folder. The actual filename of each file without the extension should replace the "parentid" attribute in XML. (Powershell or Batch file)

For example: C:\Folder\

4de6d1b4-b014-4cb3-bb61-649737a8217b.xml
4d2345b4-b415-5656-12dv-832jnm89234n.xml
...

Content of the XML:

   <comment id="4de6d1b4-b014-4cb3-bb61-649737a8217b" parentid="00000000-0000-0000-0000-000000000000" approved="True">
      <date>2014-12-20 22:23:40</date>
      <author>Joe Doe</author>
      <email>test@example.com</email>
      <country />
      <ip>0.0.0.0</ip>
      <avatar />
      <content>Test</content>
    </comment>

I appreciate everyone's help!

This is what I have so far however it replaces the whole line not the actual value of the attribute.

@echo off &setlocal
setlocal enabledelayedexpansion

set "search=00000000-0000-0000-0000-000000000000"
set "replace=%%~nP"
set "textfile=%%~nxP"
set "newfile=%%~nP"
for /r %%P in (*) do (
(for /f "delims=" %%i in (%textfile%) do (
    set "line=%%i"
    set "line=!line:%search%=%replace%!"
    echo(!line!))>"%newfile%"
)
)
endlocal


@ECHO ON

解决方案

The basic problem is that you are referring to a for variable, namely %%P, outside of the loop scope, which I do not recommend.

With pure batch scripting, I would change your code to this:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "SEARCH=00000000-0000-0000-0000-000000000000"
for %%P in ("*.xml") do (
    for /F "delims=" %%I in ('
        findstr /N /R "^" "%%~P" ^& ^> "%%~fP" break
    ') do (
        set "LINE=%%I"
        setlocal EnableDelayedExpansion
        set "LINE=!LINE:*:=!"
        if defined LINE set "LINE=!LINE:%SEARCH%=%%~nP!"
        >> "%%~fP" echo(!LINE!
        endlocal
    )
)
endlocal
exit /B

This is capable of modifying the enumerated *.xml files in the current directory directly, so no interim file is required to do the replacement of the attribute parentid.

Each file is read by findstr which precedes every line with a line number and a colon temporarily in order not to lose empty lines (remember that for /F ignores such). The command line set "LINE=!LINE:*:=!" removes the prefix afterwards. Note that delayed expansion is toggled in the for /F in order not to lose any exclamation marks occurring within the files. These measures ensure that the original *.xml file contents are not altered, except the replaced portion of course.

这篇关于使用文件名作为属性值(Powershell或批处理文件)搜索和替换XML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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