无法在PowerShell中使用Get-ChildItem获取文件名

时间:2019-01-14 18:01:22

标签: powershell get-childitem

我正在检查每个文件夹中的HTMLreports目录,然后删除30天以上的文件。但是,当它到达$document.Name循环中的$image.nameforeach的文件名时,并没有得到删除Remove-Item命令文件的文件名。

$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);
                    }
                }
            }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

与其重复一遍又一遍的长路径,不如将其存储在变量中。

您错误地在

之间插入了几次额外的空格
C:\Program  Files (x86)\Jenkins\jobs\

还有

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

答案 1 :(得分:0)

再次查看后,先前的答案已删除。

您似乎只是想在特定日期后删除jobs \ htmlreport文件夹中的所有文件,并且它是图像子文件夹,您可以将代码缩减为该文件...

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)"}
}

…当然,如果还有其他文件夹,则只应过滤这两个文件夹即可。

答案 2 :(得分:0)

感谢大家的所有见解,我能够使用下面的代码来完成此工作。我正在检查作业目录中的所有文件夹或报告,对于每个报告,我将进入htmlreports目录,并删除图像目录中已存在30天以上的所有文件和图像。如果有更清洁的方法可以执行此操作,请告诉我,我使用的是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
                }
            }

        }
    }
}