Reputation: 75
I have a script that parses through an XML file and outputs the text of a specific field to a text file for each XML file within a directory.
I am now trying to have this script exclude a specific sub-directory with no luck.
function xml-convert ($srcdir, $destdir) {
gci $srcdir -r -Filter *.xml | ? {
$_.DirectoryName -ne $destdir
} | ForEach-Object{
$extract = Get-Content $_.FullName
$newname = Get-Item $_.FullName | % {$_.BaseName}
$xml = [xml] $extract
$xml.PortalAutoTextExport.AutoText.ContentText |
Out-File -FilePath "$destdir\$newname.txt"
}
}
When I run just this piece
gci $srcdir -r -Filter *.xml | ? {$_.DirectoryName -ne $destdir}
the file list that is returned does NOT show the excluded directory, nor does it show any of the child items for the excluded directory, which is the intention for the entire script.
I've tried several methods for excluding and all have failed. This is the closest I have come to a working solution.
Upvotes: 0
Views: 402
Reputation: 54821
DirectoryName
will never match .\ Ex:
(dir .\Desktop -File).DirectoryName
C:\Users\frode\Desktop
C:\Users\frode\Desktop
C:\Users\frode\Desktop
You need to convert the relative path to absolute path. Example:
function xml-convert ($srcdir, $destdir) {
$absolutedest = (Resolve-Path $destdir).Path
Get-ChildItem -Path $srcdir -Recurse -Filter *.xml | Where-Object {
$_.DirectoryName -ne $absolutedest
} | ForEach-Object{
$extract = Get-Content $_.FullName
$newname = Get-Item $_.FullName | % {$_.BaseName}
$xml = [xml] $extract
$xml.PortalAutoTextExport.AutoText.ContentText |
Out-File -FilePath "$absolutedest\$newname.txt"
}
}
Upvotes: 1