recursive search in XML

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Kenny Master Kenny Master 1 year, 11 months ago.

  • Author
    Posts
  • #21104
    Profile photo of Kenny Master
    Kenny Master
    Participant

    Hello fellow members,

    I do have a problem with my script, let me explain it.

    What I want ?
    to associate 1 or n files extension(s) to a folder

    The XML File :
    I cannot copy/paste the XML code it's destroying the format ..
    [ol]{

    SERVER-0001
    SERVER-0001

    D$\Hyperion\logs\openldap\
    *.txt

    D$\Hyperion\logs\BIPlus\
    server_messages.log.*
    Annotations.log.*
    eiengine.log

    }[/ol]

    the script :
    foreach ($server in $xmlfile.SI.Environnement.Foundation.SERVER) {
    foreach($path in $xmlfile.SI.Environnement.Foundation.Paths.PATH) {
    $fullpath = $path.FOLDER
    $Exist = Test-Path "\\$server\$fullpath"
    if ($Exist -eq "True") {
    write-host "directory :"$path.FOLDER "exist" on $Server
    $TotalRemoved = Get-ChildItem "\\$server\$fullpath\*" -Include $path.PATTERN | Where {$_.LastWriteTime -le "$Limit"} | Measure-Object | Select-Object -ExpandProperty Count
    Write-host $TotalRemoved "files were removed with pattern" $path.PATTERN
    }
    elseif ($Exist -ne "True") {
    Write-Host "directory :"$fullpath "does not exist" on $Server
    }
    write-host `r
    }
    }

    ————————————————————————————————————————————————————-
    the code return :
    directory : D$\Hyperion\logs\openldap\ exist on Server-0001
    0 files were removed with pattern *.txt

    directory : D$\Hyperion\logs\BIPlus\ exist on Server-0001
    35 files were removed with pattern [b]server_messages.log.* Annotations.log.* eiengine.log[/b]

    directory : D$\Hyperion\logs\openldap\ does not exist on Server-0002
    directory : D$\Hyperion\logs\BIPlus\ does not exist on Server-0002
    ————————————————————————————————————————————————————-

    The problem is in bold, I'd like "server_messages.log.*" on a line (as a variable to reuse, "Annotations.log.*" on a sercond and "eiengine.log" on a thrid one.

    Is there a way to get 1 or n extension(s) PER ?

    thank you for your help

  • #21105
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hey Kenny. Have you tried upload it? Rename the file to .txt extension first.

  • #21107
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    In this case, it looks like your $path variable is an array, and you're running PowerShell 3.0 or later. That makes $path.Pattern resolve also to an array (of strings), and when you pass that array on to Write-Host, it gets converted to a string for display by joining the array using the default field separator (a space character.)

    Where you go from here depends on what you want the output to look like. For example, you could do something like this:

    Write-Host "$TotalRemoved files were removed with the following patterns:"
    foreach ($pattern in $path.Pattern)
    {
        Write-Host $pattern
    }
    
  • #21109
    Profile photo of Kenny Master
    Kenny Master
    Participant

    Answer is :

    [hr]

    foreach ($server in $xmlfile.SI.Environnement.Foundation.SERVER) {

    foreach ($path in $xmlfile.SI.Environnement.Foundation.PATHS.PATH) {

    foreach ($Pattern in $path.PATTERNS.PATTERN) {

    $allpath = $path.FOLDER

    $Exist = Test-Path \\$server\$allpath

    if ($Exist -eq "True") {
    write-host "directory :"$path.FOLDER "exist" on $Server
    $TotalRemoved = Get-ChildItem "\\$server\$allpath\*" -Include $path.PATTERN | Where {$_.LastWriteTime -le "$Limit"} | Measure-Object | Select-Object -ExpandProperty Count

    Write-host $TotalRemoved "files were removed with pattern" $Pattern
    write-host `r
    }

    elseif ($Exist -ne "True") {
    Write-Host "directory :"$fullpath "does not exist" on $Server
    }
    }

    }
    }
    [hr]

You must be logged in to reply to this topic.