2012-06-21 25 views
0

我在一堆單獨的目錄中有一堆名爲content_ [0-9](內容下劃線後跟一個數字)的xml文件。這些文件均具有一個AIRPORT_CODE元素,像這樣:Windows命令行附加到基於鍵值對的xml文件

<airport_code>JFK</airport_code> 

然後我有一個關鍵值映射關係,就像這樣:

JFK=US/Eastern 
LHR=Europe/London 
etc 

我要循環使用所有這些XML文件並添加正確的時區元素每一個XML文件,使肯尼迪文件內容如下:

<airport_code>JFK</airport_code> 
<timezone>US/Eastern</timezone> 

林試圖做到這一點在Windows批處理腳本(安裝unix utils)。這是我到目前爲止:

@echo off 
for /d %%x in (C:\directory\*) do type %%x\content_? | grep "<airport_code>" | gawk "{gsub(/ <airport_code>/, \"\"); print}" | gawk "{gsub(/<\/airport_code>/, \"\"); print}" 

這將讀取所有xml文件的機場代碼。

如何查看每個機場代碼的鍵值對中的upp值並追加適當的時區元素?這可能嗎?任何幫助不勝感激。

+0

你可以批量處理,但這對我來說就像是一個WSH/JScript作業。 – Cheeso

回答

0

我設法解決這個我自己,但只能切換到PowerShell。意見和改進的讚賞,因爲是建議CMD內做的事情:

foreach ($i in get-childitem $loc -recurse){ 
    if ($i.name -match "content_"){ 
     $content = get-content $i.FullName 
     $airportcode=get-content $i.FullName | select-string -pattern "<airport_code>" 
     $airportcode= $airportcode -replace "<airport_code>", "" 
     $airportcode= $airportcode -replace "</airport_code>", "" 
     $airportcode= $airportcode -replace " ", "" 
     $timezone=$timezones.Get_Item("$airportcode") 
     $newContent = $content -replace "</airports>", "" 
     $newContent = $newContent -replace " ","`n " 
     $newContent="$($newContent)`n<timezone>$($timezone)</timezone>`n</airports>" 
     Set-Content $i.FullName $newContent 
    } 
} 

注意,這也消除了封閉元素標籤< /機場>,然後將其追加到末尾的文件。