好的,所以我对使用 For/F 不是很熟悉。如果文件是静态的并且有一组我可以跳过然后从中提取数据的行,我可以修改它。我目前正在尝试修改 .XML 文件。该文件将具有不同的行数,但始终具有以下内容</SyncWindow> </AutoSyncWindows> <SyncServiceConnections /> <DaysToRetainRecordedData>90</DaysToRetainRecordedData> <SyncRestartRequired>false</SyncRestartRequired>- <LastGroupsSynced>
<DaysToRetainRecordedData>90</DaysToRetainRecordedData>
的值可能不同,例如 <DaysToRetainRecordedData>30</DaysToRetainRecordedData>
使用 token ,搜索该行的 .XML 文件并使用以下 <DaysToRetainRecordedData>0</DaysToRetainRecordedData>
覆盖它的最有效方法是什么
我无法覆盖整个 .XML 文件,因为它们具有因机器而异的唯一服务器 key 。所以我需要能够找到该行并将值编辑为 0。
有什么想法吗?如果 For/F 不是最有效的方法,如果需要,我可以转移到 VBS。但是它必须从纯 shellcode 中调用,并且会使事情变得更复杂一些。
最佳答案
最优雅、最灵活和最安全的方法是下载 msxsl.exe,然后使用一个很小的 XSLT 样式表只修改您想要的 XML 值:
<!-- DaysToRetainRecordedData.xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="newValue" select="0" />
<!-- this template copies your input XML unchanged -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<!-- this template changes one single value -->
<xsl:template match="DaysToRetainRecordedData/text()">
<xsl:value-of select="$newValue" />
</xsl:template>
</xsl:stylesheet>
在命令行上调用它:
msxsl.exe input.xml DaysToRetainRecordedData.xsl –o output.xml newValue=0
命令行参数
newValue
将显示在 XSL 程序中。关于xml - 批量修改XML文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4661650/