2011-03-14 40 views
1

我寫了一個批處理文件來替換dtsConfig文件中的某些字符串。 現在從我可以收集的內容來看,批處理無法直接編輯dtsconfig文件,所以我使用的解決方法是使用 將.dtsConfig文件首先轉換爲.xml文件,編輯它們並將其轉換回來。批處理文件 - 在dtsconfig/xml文件中的字符串之間編輯字符串的代碼

但是我有很多的.dtsconfig文件與幾個不同的字符串我想改變

例如下面

<ConfiguredValue> Data Source=SERVER_NAME;Integrated Security=True;</ConfiguredValue>

我的串碼SERVER_NAME是能夠改變SERVER_NAME值,而是我更願意改變之間 數據源中的內容=和;集成安全性。所以我可以做很多dtsConfig文件,可能有不同的服務器名稱

這是可能的批處理?

這是我下面的代碼:


@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)

謝謝。

回答

1

試試這樣的東西,而不是你目前的替換循環。

它檢查字符串「數據源」的每一行,如果它發現字符串拆分行頭「...數據源」和尾巴「; ...」,有效地刪除舊的部分數據源。例子:
Ex。
<ConfigValue> Data Source=SERVER_NAME;Integrated Security=True;</ConfigValue>
分流至
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

(
echo 1 
:label1 & echo invisble 
:label2 & echo visible 
echo 2 
) 

(
echo 3 
:label1 creates a syntax error, befor the block executes 

echo 4 
) 
+0

感謝您付出的努力。 – Stephen 2011-03-14 15:49:58

+0

但是,請注意,您的代碼與我的一樣,依賴於事先知道每個dtsconfig文件的SERVER_NAME,換句話說,對於 Data Source = SERVER_NAME; Integrated Security = True;的其他字符,以下是之前的字符串並且在SERVER_NAME之後,在每個dtsconfig文件中都是相同的,但如果找到我,SERVER_NAME可能會有所不同? – Stephen 2011-03-14 15:53:37

+1

@Stephen:在我的例子中,SERVER_NAME並不重要,因爲'='和';'之間的所有字符都被替換爲新的'%dataSource%' – jeb 2011-03-15 12:08:51

相關問題