2013-01-23 99 views
0

我有下面的標籤的XML文件:讀取批處理文件行沒有for循環

<param> 
    <name>date</name> 
    <Value>20010202</Value> 
</param> 

我有一個文件很多這樣的標籤。我需要通過批處理讀取此文件,並將日期值更新爲名稱標籤包含「日期」的字符串。 我已經批量編寫了一些代碼來替換日期(無論它在完整文件中找到的位置)爲所需的字符串。我的代碼如下:

for /f "tokens=1,* delims=]" %%K in ('"type "example.xml"|find /n /v """') do (
set "line=%%L" 
if defined line (
    call set "line=echo.%%line:20010202=desiredString%%" 
    for /f "delims=" %%C in ('"echo."%%line%%""') do %%~C 
) else echo. 
) >> "example.tmp" 

但我需要進行檢查更換日期應爲名稱標籤爲「日期」的價值標籤。 我想執行循環內檢查,然後讀取下一行,如果當前行匹配。

如何讀取循環內部的一行並檢查批處理文件中是否存在條件?

+0

所以,你要檢查,如果你正在閱讀的行中的名稱標籤匹配,並且如果它不只是在下一行更換日期值? –

+0

是的,這只是爲了確保我不會替換文件中任何隨機出現的字符串「20010202」。 – Vishal

回答

4

我想你可以使用純批量編寫解決方案,但它可能會很慢並且很脆弱。通過脆弱我的意思是XML可以重新格式化在邏輯上等效的方式,但破壞你的代碼。批處理可能是您可以用來操縱XML的最差語言之一。

理想情況下,您應該使用明確設計用於處理XML的命令行工具。或者,您可以使用任何支持正則表達式搜索和替換的語言或工具。一些選項 - VBScript,JScript,gnu sed for Windows,Powershell ...列表繼續。

我已經編寫了一個混合批處理/ JScript REPL.BAT實用程序,它爲批處理執行正則表達式搜索和替換文本文件提供了一種簡單的方法。它非常快速,比任何純批處理解決方案都快得多。

下面是一個使用我的REPL.BAT工具一個簡單的解決方案:

@echo off 
setlocal 
set "file=example.xml" 
set "oldDate=20010202" 
set "newDate=20130123" 
set "search=(<param>\s*<name>date</name>\s*<Value>)%oldDate%(</Value>\s*</param>)" 
type "%file%"|repl "%search%" "$1%newDate%$2" m >"%file%.new" 
move /y "%file%.new" "%file%" >nul 

這裏是REPL.BAT實用。完整的文檔嵌入在腳本中。

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment 

::************ Documentation *********** 
::: 
:::REPL Search Replace [Options [SourceVar]] 
:::REPL /? 
::: 
::: Performs a global search and replace operation on each line of input from 
::: stdin and prints the result to stdout. 
::: 
::: Each parameter may be optionally enclosed by double quotes. The double 
::: quotes are not considered part of the argument. The quotes are required 
::: if the parameter contains a batch token delimiter like space, tab, comma, 
::: semicolon. The quotes should also be used if the argument contains a 
::: batch special character like &, |, etc. so that the special character 
::: does not need to be escaped with ^. 
::: 
::: If called with a single argument of /? then prints help documentation 
::: to stdout. 
::: 
::: Search - By default this is a case sensitive JScript (ECMA) regular 
:::   expression expressed as a string. 
::: 
:::   JScript syntax documentation is available at 
:::   http://msdn.microsoft.com/en-us/library/ae5bf541(v=vs.80).aspx 
::: 
::: Replace - By default this is the string to be used as a replacement for 
:::   each found search expression. Full support is provided for 
:::   substituion patterns available to the JScript replace method. 
:::   A $ literal can be escaped as $$. An empty replacement string 
:::   must be represented as "". 
::: 
:::   Replace substitution pattern syntax is documented at 
:::   http://msdn.microsoft.com/en-US/library/efy6s3e6(v=vs.80).aspx 
::: 
::: Options - An optional string of characters used to alter the behavior 
:::   of REPL. The option characters are case insensitive, and may 
:::   appear in any order. 
::: 
:::   I - Makes the search case-insensitive. 
::: 
:::   L - The Search is treated as a string literal instead of a 
:::    regular expression. Also, all $ found in Replace are 
:::    treated as $ literals. 
::: 
:::   E - Search and Replace represent the name of environment 
:::    variables that contain the respective values. An undefined 
:::    variable is treated as an empty string. 
::: 
:::   M - Multi-line mode. The entire contents of stdin is read and 
:::    processed in one pass instead of line by line.^anchors 
:::    the beginning of a line and $ anchors the end of a line. 
::: 
:::   X - Enables extended substitution pattern syntax with support 
:::    for the following escape sequences: 
::: 
:::    \\  - Backslash 
:::    \b  - Backspace 
:::    \f  - Formfeed 
:::    \n  - Newline 
:::    \r  - Carriage Return 
:::    \t  - Horizontal Tab 
:::    \v  - Vertical Tab 
:::    \xnn - Ascii (Latin 1) character expressed as 2 hex digits 
:::    \unnnn - Unicode character expressed as 4 hex digits 
::: 
:::    Escape sequences are supported even when the L option is used. 
::: 
:::   S - The source is read from an environment variable instead of 
:::    from stdin. The name of the source environment variable is 
:::    specified in the next argument after the option string. 
::: 

::************ Batch portion *********** 
@echo off 
if .%2 equ . (
    if "%~1" equ "/?" (
    findstr "^:::" "%~f0" | cscript //E:JScript //nologo "%~f0" "^:::" "" 
    exit /b 0 
) else (
    call :err "Insufficient arguments" 
    exit /b 1 
) 
) 
echo(%~3|findstr /i "[^SMILEX]" >nul && (
    call :err "Invalid option(s)" 
    exit /b 1 
) 
cscript //E:JScript //nologo "%~f0" %* 
exit /b 0 

:err 
>&2 echo ERROR: %~1. Use REPL /? to get help. 
exit /b 

************* JScript portion **********/ 
var env=WScript.CreateObject("WScript.Shell").Environment("Process"); 
var args=WScript.Arguments; 
var search=args.Item(0); 
var replace=args.Item(1); 
var options="g"; 
if (args.length>2) { 
    options+=args.Item(2).toLowerCase(); 
} 
var multi=(options.indexOf("m")>=0); 
var srcVar=(options.indexOf("s")>=0); 
if (srcVar) { 
    options=options.replace(/s/g,""); 
} 
if (options.indexOf("e")>=0) { 
    options=options.replace(/e/g,""); 
    search=env(search); 
    replace=env(replace); 
} 
if (options.indexOf("l")>=0) { 
    options=options.replace(/l/g,""); 
    search=search.replace(/([.^$*+?()[{\\|])/g,"\\$1"); 
    replace=replace.replace(/\$/g,"$$$$"); 
} 
if (options.indexOf("x")>=0) { 
    options=options.replace(/x/g,""); 
    replace=replace.replace(/\\\\/g,"\\B"); 
    replace=replace.replace(/\\b/g,"\b"); 
    replace=replace.replace(/\\f/g,"\f"); 
    replace=replace.replace(/\\n/g,"\n"); 
    replace=replace.replace(/\\r/g,"\r"); 
    replace=replace.replace(/\\t/g,"\t"); 
    replace=replace.replace(/\\v/g,"\v"); 
    replace=replace.replace(/\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}/g, 
    function($0,$1,$2){ 
     return String.fromCharCode(parseInt("0x"+$0.substring(2))); 
    } 
); 
    replace=replace.replace(/\\B/g,"\\"); 
} 
var search=new RegExp(search,options); 

if (srcVar) { 
    WScript.Stdout.Write(env(args.Item(3)).replace(search,replace)); 
} else { 
    while (!WScript.StdIn.AtEndOfStream) { 
    if (multi) { 
     WScript.Stdout.Write(WScript.StdIn.ReadAll().replace(search,replace)); 
    } else { 
     WScript.Stdout.WriteLine(WScript.StdIn.ReadLine().replace(search,replace)); 
    } 
    } 
} 
+0

不能強調*脆弱*部分足夠強。除非有人用槍逼迫你,否則不應該逐行處理XML。 –

+0

@AnsgarWiechers - 同意:-)我上面的正則表達式使用多行選項,所以它忽略了行。回車和換行符在搜索中被'\ s *'簡單地視爲空格。我不是XML的專家,因此可能會對搜索條件進行改進。但它對我來說似乎很強大。 – dbenham

+0

解決方案看起來不錯,但我需要純批處理解決方案。這是我需要在完整批處理文件中實現的邏輯的一部分。感謝您的建議:) – Vishal

0

儘管我們都知道批處理速度很慢,但我認爲沒有人知道與其他解決方案進行比較的準確程度。下面有一個針對這個問題的純批處理解決方案,我認爲它可以相當快。我可以請你(或任何人)用一個大的.xml文件測試這個解決方案並獲得比較時間嗎?這些信息可能對我們所有人都有價值!

@echo off 
setlocal EnableDelayedExpansion 
set "file=example.xml" 
set "oldDate=20010202" 
set "newDate=20130123" 
set lastLine=1 
set line= 
echo "<name>date</name>" >> "%file%" 
< "%file%" (for /F "delims=:" %%a in ('findstr /N /C:"<name>date</name>" "%file%"') do (
    if not defined line set /P line= 
    set /A lines=%%a-lastLine, lastLine+=lines+1 
    for /L %%i in (1,1,!lines!) do set /P "line=!line!" & echo/ 
    set nextLine= 
    set /P nextLine= 
    if defined nextLine (
     echo !line! 
     set "line=!nextLine:<Value>%oldDate%</Value>=<Value>%newDate%</Value>!" 
    ) 
)) > "%file%.new" 
move /Y "%file%.new" "%file%" 

請注意,以前的批處理程序錯誤地處理文件中的空行。雖然這一點可以解決,但額外的代碼會減慢程序的運行速度,所以我想先了解一下原始代碼與其他解決方案的比較。用沒有空行的.xml文件測試這個程序。

安東尼

+0

〜1 MB文件:我的代碼 - 0.23秒。你的代碼 - 26.35秒。任何批處理解決方案都會變得很脆弱,但是使用SET/P會將行長限制爲1021 max,並且行必須以CR/LF結尾。這兩個都是XML的嚴重限制。 – dbenham