我有一个像这样的XML文件:
<game id="748" source="ScreenScraper">
<path>./Tecmo Super Bowl (U) (Sep 1993) [!].zip</path>
<name>Tecmo Super Bowl</name>
<desc>Tecmo returns to the gridiron with this new version of Tecmo Super Bowl for 16-bit console systems. Play with real National Football League players and teams in this 2D, side-scrolling arcade game.</desc>
<image>~/.emulationstation/downloaded_images/megadrive/Tecmo Super Bowl (U) (Sep 1993) [!]-image.png</image>
<rating>0.85</rating>
<releasedate>19930000T000000</releasedate>
<developer>Tecmo</developer>
<publisher>Tecmo</publisher>
<genre>Sports</genre>
<players>1-2</players>
<crc32>0F02FBFC</crc32>
<md5>FEC698E5387617B11C81431894B12EDC</md5>
<sha1>77AF672321947C3F8F80F7AE7ADFF8CDE0E2986A</sha1>
</game>
<game id="1014" source="ScreenScraper">
<path>./Action 52 (Unl) [!].zip</path>
<name>Action 52</name>
<desc>Action 52 features 52 different games in a single cartridge! The games are selected from one of three menus and are mostly side scrolling, platform or shooter action games.</desc>
<image>~/.emulationstation/downloaded_images/megadrive/Action 52 (Unl) [!]-image.png</image>
<rating>0.1</rating>
<releasedate>19930000T000000</releasedate>
<developer>Active Enterprises Ltd.</developer>
<publisher>Active Enterprises</publisher>
<genre>Compilation</genre>
<players>1-2</players>
<crc32>7B544625</crc32>
<md5>D32F3F2825DB4AE40D51317BBFF7330E</md5>
<sha1>3D9C1984B5F1B385EBDFF34218FEA6BCE83C43B6</sha1>
</game>
<game id="103612" source="ScreenScraper">
<path>./Bug's Life, A (Unl) [!].zip</path>
<name>A Bug's Life</name>
<developer>X BOY</developer>
<publisher>X BOY</publisher>
<genre>Action</genre>
<crc32>6EBECD9A</crc32>
<md5>27122254E417C44A35310E6D39B61D4A</md5>
<sha1>441A0E019EB32106D1AFD2ED97F117B007299F5B</sha1>
</game>
我需要一个批处理文件来读取每个游戏的名称值,并为每个游戏添加两个这样的新行:
<video>~/.emulationstation/roms/megadrive/Videos/%gamename%.mp4</video>
<marquee>~/.emulationstation/roms/megadrive/Marquees/%gamename%.png</marquee>
%gamename%
必须是<name>
和</name>
之间游戏的名称。
我制作了这个批处理脚本,但它不起作用:
setlocal enableextensions disabledelayedexpansion
set "gamename="
for /f "tokens=3 delims=<>" %%a in ( 'find /i "<name>" ^< "gamelist.xml"' ) do set "gamename=%%a"
(for /F "delims=" %%a in (gamelist.xml) do (
set "newLine=!line:"~/.emulationstation/roms/megadrive/Videos/%gamename%.mp4"
echo !newLine!
)) > newFile.xml
答案 0 :(得分:2)
最强大的解决方案是使用XML解析器, PowerShell 允许轻松访问一个:
# Read the XML file into an XML DOM ([System.Xml.XmlDocument] or [xml], for short).
# Note: If your file truly has no single root XML element,
# use ('<xml>' + (Get-Content ...) + '</xml>')
$doc = [xml] (Get-Content -raw gamelist.xml)
# Loop over all <game> elements.
foreach ($gameEl in $doc.DocumentElement.game) {
$gameName = $gameEl.name
# Append the new elements, using string interpolation to set the inner text
# (note the references to variable ${gameName} inside the double-quoted string).
$gameEl.AppendChild($doc.CreateElement('video')).InnerText = "~/.emulationstation/roms/megadrive/Videos/${gameName}.mp4"
$gameEl.AppendChild($doc.CreateElement('marquee')).InnerText = "~/.emulationstation/roms/megadrive/Marquees/${gameName}.png"
}
# Save the modified XML to a new file, using the .NET framework's default
# UTF-8 encoding without a BOM.
$writer = [System.IO.StreamWriter] "$PWD/newFile.xml"
$doc.Save($writer)
$writer.Close()
OP请求的变体,它使用<path>
元素中包含的路径的无扩展名文件名而不是<name>
元素的值,并且只添加元素{{1 }}和video
如果它们不存在:
marquee