使用 Get-ChildItem 命令替换 XML 中的文件名

Replacing file name in XML by using Get-ChildItem command

我正在尝试使用 Get-ChildItem 命令替换 XML 中的一些文件名。 XML里面有6个文件名,每个文件夹有6个文件。我想要做的就是使用 Get-ChildItem 命令替换 XML 中的文件名 如果有帮助,文件名总是递增 1。所以我有以下内容:

Get-ChildItem $dirname\resources\* -filter *.txt -recurse

返回 6 个文件名。当我 select 一个 XML 节点时,这也会返回 6 个文件名。

$xml.SelectNodes('//FileName',$XmlNSManager)

如何从 Get-ChildItem 命令同步替换 XML 中的每个文件名?即

12341_1.txt --> 100011.txt,
12341_2.txt --> 100012.txt,
12341_3.txt --> 100013.txt,

等等。

如果我正确理解你的问题,这样的事情应该有效:

$files = Get-ChildItem $dirname\resources\* -Filter *.txt -Recurse |
         select -Expand Name | sort

$i = 0
$xml.SelectNodes('//FileName', $XmlNSManager) | % {
  $_.'#text' = [string]$files[$i]
  $i++
}