Newbie to Powershell. I'd like to parse multiple files one after another. In order to do this, I need to load the files as a XML. This is my code so far:
for ($i=0; $i -le 5; $i )
{
$contents = Get-ChildItem -Path $path -Force -Recurse
File |Select-Object -First $i | Select-Object -Last 1
$fl = $contents.Name
$xml.Load(".\downloads\Test\$fl")
}
If I remove for ($i=0; $i -le 5; $i )
it works. But I need it to work with the for loop. Any ideas?
CodePudding user response:
you could do:
$filepaths = (get-childitem -Path C:\tmp -Recurse -Filter '*.xml').psPath
#XML per cast
$xmldocuments = @(
$filepaths | %{
[xml](get-content -path $_)
}
)
Alternatively create a new xml object and use the load method
$xml = New-Object -TypeName xml
$xmldocuments = @(
$filepaths | %{
$xml.Load($_)
$xml
}
)
The Array xmlDocuments contains each xml document as element.
CodePudding user response:
Solved it with this code:
$files = Get-ChildItem -LiteralPath ".\downloads\Test"
$xml = New-Object System.Xml.XmlDocument
foreach ($file in $files)
{
$xml.Load($file.FullName)
}
This enabled me to acess and encode every file in the folder.