批处理文件-用于在dtsconfig/xml文件中的字符串之间编辑字符串的代码 [英] Batch file - code to edit a string between strings in a dtsconfig / xml file
问题描述
我写了一个批处理文件来替换dtsConfig文件中的某些字符串. 现在从我可以收集的信息来看,批处理不能直接编辑dtsconfig文件,因此我正在使用的解决方法是 首先将.dtsConfig文件转换为.xml,对其进行编辑,然后将其转换回.
I have written a batch file to replace certain strings in a dtsConfig file. Now from what I can gather, batch cannot directly edit dtsconfig files so a workaround I'm using which works is to convert the .dtsConfig files into .xml first, edit them, and convert them back.
但是我有很多.dtsconfig文件,我想更改几个不同的字符串
However I have a lot of .dtsconfig files with several different strings i want to change
例如字符串SERVER_NAME
e.g. the string SERVER_NAME
<ConfiguredValue> Data Source=SERVER_NAME;Integrated Security=True;</ConfiguredValue>
下面的代码可以更改SERVER_NAME的值,但我希望在以下之间更改内容: 数据源=和;集成安全性.这样我就可以处理很多可能具有不同服务器名称的dtsConfig文件
My code below is able to change the SERVER_NAME value but instead I would prefer to change the contents between Data Source= and ;Integrated Security. so that I could do it for a lot of dtsConfig files that might have different server names
批量处理有可能吗?
这是我的下面的代码:
@echo off > *.xml
setLocal DisableDelayedExpansion
:: make a copy of the .dtsConfig files set str="C:\dtsconfig\copyArea"
:: make a copy of the .dtsConfig files set str="C:\dtsconfig\copyArea"
:: Copy all dtsConfig files into the backup directory xcopy "*.dtsConfig" %str1% /E /I
:: Copy all dtsConfig files into the backup directory xcopy "*.dtsConfig" %str1% /E /I
:: Rename all .dtsConfig files to .xml to enable batch to work with them ren *.dtsConfig *.xml
:: Rename all .dtsConfig files to .xml to enable batch to work with them ren *.dtsConfig *.xml
:: set the new server name set dataSource=NEW_SERVER_NAME
:: set the new server name set dataSource=NEW_SERVER_NAME
@echo off > ConfigFile.dtsConfig setLocal DisableDelayedExpansion
@echo off > ConfigFile.dtsConfig setLocal DisableDelayedExpansion
if exist ConfigFile.dtsConfig del ConfigFile.dtsConfig
if exist ConfigFile.dtsConfig del ConfigFile.dtsConfig
for /f "tokens=* delims= " %%G in (ConfigFile.xml) do ( set str=%%G
for /f "tokens=* delims= " %%G in (ConfigFile.xml) do ( set str=%%G
谢谢.
推荐答案
尝试类似这样的方法,而不是当前的替换循环.
Try something like this instead of your current replace loop.
它检查每一行中的字符串"Data Source",如果发现该字符串将行拆分为头部"... Data Source"和尾部; ...",则有效地删除字符串的旧部分.数据源.
例如
<ConfigValue> Data Source=SERVER_NAME;Integrated Security=True;</ConfigValue>
拆分为
head=<ConfigValue> Data Source
tail=;Integrated Security=True;</ConfigValue>
It checks every line for the string "Data Source", if it found the string split the line into a head "... Data Source" and a tail ";...", effectivly remove the old part of the Data Source.
Ex.
<ConfigValue> Data Source=SERVER_NAME;Integrated Security=True;</ConfigValue>
splits to
head=<ConfigValue> Data Source
tail=;Integrated Security=True;</ConfigValue>
@echo off
setLocal DisableDelayedExpansion
set "dataSource=NEW DATASOURCE"
for /f "tokens=* delims= " %%G in (ConfigFile.xml) do (
set "line=%%G"
setLocal EnableDelayedExpansion
REM set the string "SERVER_NAME" to be the dataSource defined above
set str=!str:SERVER_NAME=%dataSource%!
set "newLine=!line:*Data Source=!"
if !newLine! NEQ !line! (
call :length lenNew newLine
call :length lenLine line
set /a headLen=lenLine-lenNew
for %%n in (!headLen!) do (
set "head=!line:~0,%%n!"
)
set "tail=!newLine:*;=!"
set "newLine=!head!=%dataSource%;!tail!"
)
REM generate a new dtsConfig file with the rename in place
(echo(!newLine!)
endlocal
)
goto :eof
:length <resultVar> <stringVar>
(
setlocal EnableDelayedExpansion
set "s=!%~2!#"
set "len=0"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" (
set /a "len+=%%P"
set "s=!s:~%%P!"
)
)
)
(
endlocal
set "%~1=%len%"
exit /b
)
顺便说一句.在括号块内使用::
注释样式不是一个好主意,因为标签在块中的工作方式有所不同,最好使用REM
.
Btw. It isn't a good idea to use the ::
comment style inside of parenthesis blocks, as lables works different in blocks, better is to use REM
.
(
echo 1
:label1 & echo invisble
:label2 & echo visible
echo 2
)
(
echo 3
:label1 creates a syntax error, befor the block executes
echo 4
)
这篇关于批处理文件-用于在dtsconfig/xml文件中的字符串之间编辑字符串的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!