简体   繁体   English

通过批处理文件搜索并替换多个xml文件中的多个字符串

[英]Search and replace multiple strings in multiple xml files via batch file

I have some xml files (about 100, some in subfolders) whose IDs need to be changed. 我有一些xml文件(大约100个,在子文件夹中),其ID需要更改。 Therefore I have an Excel list with about 1.100 lines, left column is the old ID name and right column the new one which should replace the old one. 因此,我有一个大约有1.100行的Excel列表,左列是旧的ID名称,右列是新的ID名称,它应该替换旧的ID名称。

I found this script on Stackoverflow (and changed it a little): 我在Stackoverflow上找到了此脚本(并对其进行了一些更改):

@rem replaceids.bat

ECHO off
SETLOCAL enabledelayedexpansion

SET source=%1
SET target=%2

IF EXIST %target% DEL /f %target%

FOR /f "delims=" %%i IN ('FINDSTR . %source%') DO (
   SET line=%%i
   SET line=!line:ö=oe!
   SET line=!line:ä=ae!
   SET line=!line:ü=ue!
   ECHO !line! >> %target%
)

Here's the original link: batch replace multiple different strings 这是原始链接: 批量替换多个不同的字符串

Then I created another batch file to call the one above and run it through my files: 然后,我创建了另一个批处理文件来调用上面的文件,并通过我的文件运行它:

for /R %%F in (*.xml) do (
   replaceids.bat "%%~dpnxF" "C:\mytargetfolder%%~pnxF"
)

Found this on Stackoverflow as well; 也可以在Stackoverflow上找到它; here's the link: Batch process all files in directory 这是链接: 批量处理目录中的所有文件

Also thanks to Meltz014 from reddit, who helped me get this far. 同时还要感谢reddit的Meltz014,他帮助了我。

The problem I have now is that it says that the network path can not be found when I start it (not sure what it says exactly in English since I'm not in the office and running it on a German Windows right now). 我现在遇到的问题是,它说我启动时找不到网络路径(由于我不在办公室,并且现在正在德语Windows上运行,所以不确定它用英文确切说了什么)。 As far as I can see I'm not referencing to any networks. 据我所知,我没有引用任何网络。

Any help would be appreciated. 任何帮助,将不胜感激。

My command line experience is very limited by the way. 顺便说一下,我的命令行经验非常有限。

EDIT 编辑

I just found something else that might get me closer, it still doesn't work yet though: How to Find Replace Multiple strings in multiple text files using Powershell 我只是发现了一些可能会使我更接近的东西,但是它仍然无法正常工作: 如何使用Powershell在多个文本文件中查找替换多个字符串

$Iteration = 0
$FDPATH = 'D:\opt\HMI\Gfilefind_rep'
#& 'D:\usr\fox\wp\bin\tools\fdf_g.exe' $FDPATH\*.fdf
$GraphicsList = Get-ChildItem -Path $FDPATH\*.g | ForEach-Object FullName
$FindReplaceList = Import-Csv -Path $FDPATH\Input.csv
foreach($Graphic in $Graphicslist){
    Write-Host "Processing Find Replace on : $Graphic"
    foreach($item in $FindReplaceList){
    Get-Content $Graphic | ForEach-Object { $_ -replace "$($item.FindString)", "$($item.ReplaceString)" } | Set-Content ($Graphic+".tmp")
        Remove-Item $Graphic
        Rename-Item ($Graphic+".tmp") $Graphic
        $Iteration = $Iteration +1
        Write-Host "String Replace Completed for $($item.ReplaceString)"
    }
}

It seems to do something, but then after finnishing it always says 它似乎在做某事,但整理后总是说

String Replace Completed for 字符串替换完成

My CSV file is created as it is in the example, that means: 我的CSV文件就是在示例中创建的,这意味着:

FindString  ReplaceString
AA1A    171PIT9931A
BB1B    171PIT9931B
etc..

Since it doesn't find anything, I assume something with the CSV might be wrong? 由于找不到任何内容,因此我认为CSV可能有误吗? Any help would be highly appreciated! 任何帮助将不胜感激!

Probably 1 command line to help your work: ( %a = old-id, %b = new-id) 可能有1个命令行可以帮助您完成工作:( %a =旧ID, %b =新ID)

for /f "tokens=1,2" %a in (ids-list.csv) do msr -rp dir1,dirN -f "\\.xml$" -x "%a" -o "%b" -R

  • Remove -R if want to preview result before changing the file; 如果要在更改文件之前预览结果,请删除-R

  • Replace the %a to %%a and %b to %%b if you put above command in a file. 如果将上述命令放在文件中,则将%a替换为%%a ,将%b替换为%%b

Don't quite catch your question. 不太明白您的问题。 I think it's better to tell 2 points: 我认为最好说两点:

  • What's your inputs ? 你有什么投入?
  • What's your required output ? 您需要什么输出?

Assume: 假设:

  • You've a source old/new id list file: ids-list.csv 您有一个源旧/新ID列表文件: ids-list.csv
  • You want to recursively search and replace in folders: dir1,dir2-N 您要递归搜索并替换以下文件夹: dir1,dir2-N

msr.exe is a single portable exe tool in tools in my open project https://github.com/qualiu/msr . msr.exe是我打开的项目https://github.com/qualiu/msrtools中的单个可移植exe工具。 You can use Regex replace ( -t ) instead of plain-text( -x ), and ignore-case ( -i ). 您可以使用Regex替换( -t )代替纯文本( -x )和忽略大小写( -i )。 More info: https://qualiu.github.io/msr/usage-by-running/msr-Windows.html 更多信息: https : //qualiu.github.io/msr/usage-by-running/msr-Windows.html

Additinally: 另外:

  • If your ids-list.csv is separated by , instead of white-space, use: 如果你的ids-list.csv被分开,而不是白色的空间,使用方法:

    for /f "tokens=1,2 delims=," %a in (ids-list.csv)

  • If your ids-list.csv is more complicated, like: "old-id-1","new-id-1" "old-id-2","new-id-2" 如果您的ids-list.csv更复杂,例如: "old-id-1","new-id-1" "old-id-2","new-id-2"

    for /f "tokens=*" %a in ('msr -p ids-list.csv -t ".*\\W(.+?)\\W,\\W(.+?)\\W.*" -o "$1 $2" -PAC')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM