I have some xml files (about 100, some in subfolders) whose IDs need to be changed. 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.
I found this script on Stackoverflow (and changed it a little):
@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; here's the link: Batch process all files in directory
Also thanks to Meltz014 from reddit, who helped me get this far.
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). 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
$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:
FindString ReplaceString
AA1A 171PIT9931A
BB1B 171PIT9931B
etc..
Since it doesn't find anything, I assume something with the CSV might be wrong? Any help would be highly appreciated!
Probably 1 command line to help your work: ( %a
= old-id, %b
= new-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;
Replace the %a
to %%a
and %b
to %%b
if you put above command in a file.
Don't quite catch your question. I think it's better to tell 2 points:
Assume:
ids-list.csv
dir1,dir2-N
msr.exe
is a single portable exe tool in tools
in my open project https://github.com/qualiu/msr . You can use Regex replace ( -t
) instead of plain-text( -x
), and ignore-case ( -i
). More info: 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:
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"
for /f "tokens=*" %a in ('msr -p ids-list.csv -t ".*\\W(.+?)\\W,\\W(.+?)\\W.*" -o "$1 $2" -PAC')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.