-1

I am checking each folder for an HTMLreports directory and then deleting files that are over 30 days old. But when it gets to the filename for $document.Name or $image.name in the foreach loop it is not getting the filename to delete the file for the Remove-Item command.

$items = Get-ChildItem -Path "C:\Program Files (x86)\Jenkins\jobs\"
$time = (Get-Date).AddDays(-30)
$today = Get-Date

echo $today

# enumerate the items array
foreach ($item in $items) {
   # if the item is a directory, then process it.
   if ($item.Attributes -eq "Directory") {
        Write-Host "job folder: " $item.Name

        $folder = Get-ChildItem -Path ("C:\Program Files (x86)\Jenkins\jobs\" + $item.Name + "\")

        if (Test-Path ("C:\Program Files (x86)\Jenkins\jobs\"+$item.Name+"\htmlreports\")) {
            $reports = Get-ChildItem -Path ("C:\Program Files (x86)\Jenkins\jobs\" + $item.Name + "\htmlreports\")

            foreach ($report in $reports) {
                Write-Host "report folder" $report.Name

                $documents = Get-ChildItem -Path ("C:\Program Files (x86)\Jenkins\jobs\" + $item.Name + "\htmlreports\" + $report.Name + "\") |
                             Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $time }

                foreach ($document in $documents) {
                    if ($document.Attributes -eq "Directory") {
                        echo "Folder exists"
                        Write-Host "delete document folder" $document.Name
                        Remove-Item ("C:\Program Files (x86)\Jenkins\jobs\" + $item.Name + "\htmlreports\"+$report.Name+"\" + $document.Name)
                    } else {
                        echo "File exists"
                        Write-Host "delete document " $document.Name
                        Remove-Item ("C:\Program Files (x86)\Jenkins\jobs\" + $item.Name + "\htmlreports\"+$report.Name+"\" + $document.Name)
                    }
                }

                if (Test-Path ("C:\Program Files (x86)\Jenkins\jobs\" + $item.Name + "\htmlreports\" + $report.Name + "\images\")) {
                    echo "Image Folder exists"

                    $images = Get-ChildItem -Path ("C:\Program Files  (x86)\Jenkins\jobs\" + $item.Name + "\htmlreports\" + $report.Name + "\images\") -Name |
                              Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $time }

                    foreach ($image in $images) {
                        Write-Host "delete image: C:\Program  Files (x86)\Jenkins\jobs\"$item.Name"\htmlreports\"$report.Name"\images\"$image.Name
                        Remove-Item ("C:\Program Files (x86)\Jenkins\jobs\" + $item.Name + "\htmlreports\" + $report.Name + "\images \" + $image.Name);
                    }
                }
            }
        }
    }
}
Peter G
  • 93
  • 1
  • 4
  • 11
  • There is an ambiguity, `$Documents` is assigned a gci and a where which **excludes** folders, nevertheless the if checks again if $document is a folder?? –  Jan 14 '19 at 20:47
  • $documents gci should get all file and folders, what is the error in the gci? – Peter G Jan 14 '19 at 21:28
  • The `Where-Object { !$_.PSIsContainer` excludes folders to be in `$documents` –  Jan 14 '19 at 21:29
  • Just a hint, you don't need to iterate all folder levels seperatly, Get-ChildItem will work wit wildcards in more than one level, `Get-ChildItem "C:\Program Files (x86)\Jenkins\jobs\*\htmlreports\*\*"` should return document files **and** folders (including images). IIUR your structure. –  Jan 14 '19 at 23:26

3 Answers3

0

Instead of repeating a long path over and over again, store it in a variable.

You erroneously have an additional space inserted several times between

C:\Program  Files (x86)\Jenkins\jobs\

and also

"C:\Program Files  (x86)\Jenkins\jobs\"
0

Previous answer removed after looking at this again.

You just seem to be wanting to remove all the files after a specific date in the jobs\htmlreport folder and it's images subfolder you can reduce your code to this...

Clear-Host

$ReportFiles = (Get-ChildItem -Path "${env:ProgramFiles(x86)}\Jenkins\jobs\htmlreports" -Recurse).FullName

($today = Get-Date)
($time = (Get-Date).AddDays(-30))

ForEach($File in (Get-ChildItem -Path $ReportFiles -File))
{
    If ($File.FullName.CreationTime -lt $time)
    {"Processing $($File.FullName)"}
}

… of course if there are other folders, you should just filter for those two.

postanote
  • 15,138
  • 2
  • 14
  • 25
0

Thank You everyone for all your insights, I was able to get this working with my below code. I am checking all the folders or reports in the jobs directory and for each report I am going into the htmlreports directory and deleting all files and images in the images directory that are more than 30 days old. If there is a cleaner way to do this please let me know, I am using powershell v1.0.

$items = gci "C:\Program Files (x86)\Jenkins\jobs\"
($today = Get-Date)
($time = $today.AddDays(-30))

Write-Host $today

# enumerate the items array
foreach ($item in $items) {
   # if the item is a directory, then process it.
   if ($item.Attributes -eq "Directory") {
        Write-Host "job folder: " $item.Name
        if (Test-Path ("C:\Program Files (x86)\Jenkins\jobs\"+$item.Name+"\htmlreports\")) {
            $reports = Get-ChildItem -Path ("C:\Program Files (x86)\Jenkins\jobs\" + $item.Name + "\htmlreports\")

            foreach ($report in $reports) {
                Write-Host "report folder" $report.Name
                $documents = ("C:\Program Files (x86)\Jenkins\jobs\" + $item.Name + "\htmlreports\" + $report.Name + "\")
                dir -recurse $documents | where {$_.LastWriteTime -le $time} | del


                if (Test-Path ("C:\Program Files (x86)\Jenkins\jobs\" + $item.Name + "\htmlreports\" + $report.Name + "\images\")) {
                    Write-Host "Image Folder exists"
                    $images = ("C:\Program Files (x86)\Jenkins\jobs\" + $item.Name + "\htmlreports\" + $report.Name + "\images\")                      
                    dir -recurse $images | where {$_.LastWriteTime -le $time} | del
                }
            }

        }
    }
}
Peter G
  • 93
  • 1
  • 4
  • 11