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.
However I have a lot of .dtsconfig files with several different strings i want to change
e.g. the string SERVER_NAME
<ConfiguredValue> Data Source=SERVER_NAME;Integrated Security=True;</ConfiguredValue>
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
Is this possible with batch?
This is my code below:
@echo 开发者_开发技巧off > *.xml
setLocal DisableDelayedExpansion
:: 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
:: 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
@echo off > ConfigFile.dtsConfig
setLocal DisableDelayedExpansion
if exist ConfigFile.dtsConfig del ConfigFile.dtsConfig
for /f "tokens=* delims= " %%G in (ConfigFile.xml) do (
set str=%%G
setLocal EnableDelayedExpansion
:: set the string "SERVER_NAME" to be the dataSource defined above
set str=!str:SERVER_NAME=%dataSource%!
:: generate a new dtsConfig file with the rename in place
>> ConfigFile.dtsConfig echo(!str!
endlocal)
Thank you.
Try something like this instead of your current replace loop.
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
)
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
)
精彩评论