如何在 PowerShell 中创建等于确切名称的文件夹名称变量

问题描述 投票:0回答:1

我正在编写一个脚本,该脚本基本上查找文件夹名称,然后通过文件夹结构递归查找具有特定扩展名的文件,然后删除那些早于 X 天的文件,然后将结果输出到 CSV 文件中。

# *IMPORTANT* DOUBLE CHECK
$fileType = '*.dwg'
$foldername = "Site Plans"

# *IMPORTANT* DOUBLE CHECK - Get the current date and calculate the date -XX days ago
$currentDate = Get-Date
$cutOffDate = $currentDate.AddDays(-1)

### START SCRIPT ### 
#$path = "C:\Users\zbarchuk\desktop\Site Plans - Copy"
$path = Get-ChildItem -Path C:\ | Where-Object {$_.Name -eq $foldername}

# Get Top Folder Name from path
#$pathTopFolder = ($path).split("\")[-1]

# $PSScriptRoot is the current path to the script ****
$GetDate = Get-Date -Format "MM-dd-yyyy_HH_mm_ss"

# File Stats
$log = [System.Collections.ArrayList]@("FileName, FilePath, FileSize")
$TotalSize = 0

# Get all files with the .pdf extension that are older than 45 days in all directories and subdirectories
$filesToDelete = Get-ChildItem -Path $path -Recurse -Filter $fileType | Where-Object { $_.LastWriteTime -lt $cutOffDate }

# Get File Item Count
$count = 1
$fileCount = @($filesToDelete).Count

# Delete each file
$OutputLog = @()
foreach ($file in $filesToDelete) {
    
    #Get Item Size and add to file array
    $size=[math]::Round((Get-Item -LiteralPath $file.FullName).length/1MB,2)

    
    Write-Progress -Activity $count"/"$fileCount
    #Write-Host -Activity "Deleting " $file.FullName

    #Calc total size of deleted files
    $TotalSize = ($TotalSize + $size)
    
    #Remove-Item $file.FullName -Force
    $count = $count+1

    $OutputLog+=@([PSCustomObject]@{
        FileName = $file
        FilePath = Split-Path -Parent ($file.FullName)
        FileSize = "$size" + " MB"
    })
    
}

$OutputLog+=@([PSCustomObject]@{
        FileName = "Total PDF File Cleanup Size"
        FilePath = "Total Items=$fileCount"
        FileSize = "$TotalSize MB"
    })

$OutputLog | Export-CSv -Path C:\users\zbarchuk\desktop\CleanupOutputLog.csv

我遇到的问题似乎无法弄清楚是 $path Get-ChildItem 代码正在抓取与我要查找的名称相似的所有文件夹。我已经尝试使用 -Unique -Eq -Match 进行各种迭代,但似乎没有什么可以将其设置为仅查找特定的文件夹名称。任何帮助将不胜感激!

powershell variables get-childitem
1个回答
0
投票

此代码考虑到可能有多个

Site Plans
目录。它还省略了摘要总计,有利于完成后从日志文件中执行此操作。

使用了

Remove-Item
命令,但它有
-WhatIf
这将导致它只报告它会做什么。这使您可以安全地多次运行脚本而无需删除任何文件。

# *IMPORTANT* DOUBLE CHECK
$fileType = '.dwg'
$foldername = "Site Plans"

# *IMPORTANT* DOUBLE CHECK - Get the current date and calculate the date -XX days ago
$currentDate = Get-Date
$cutOffDate = $currentDate.AddDays(-1)

### START SCRIPT ### 
$paths = Get-ChildItem -Recurse -Directory -Path 'C:\' -ErrorAction SilentlyContinue | Where-Object {$_.Name -eq $foldername}
$filestodelete = @()

foreach ($path in $paths) {
    # Get all files with the $filetype extension that are older than 45 days in all directories and subdirectories
    $filesToDelete += Get-ChildItem -File -Path $path -Recurse |
        Where-Object { ($_.Extension -eq $filetype) -and ($_.LastWriteTime -lt $cutOffDate) }
}

# Get File Item Count
$count = 1
$fileCount = $filesToDelete.Count

# Delete each file
$OutputLog = @()
foreach ($file in $filesToDelete) {
    Write-Progress -Activity "$count/$fileCount"
    Write-Information "Deleting $($file.FullName)"

    # Remove the file
    Remove-Item $file.FullName -Force -Whatif
    $count++

    $OutputLog += [PSCustomObject]@{
        FileName = $file.Name
        FilePath = $file.DirectoryName
        FileSize = $file.Length
    }
}

$LogFileFullName = Join-Path -Path $Env:USERPROFILE -ChildPath 'CleanupOutputLog.csv'
$OutputLog | Export-Csv -Path $LogFileFullName

生成日志文件后,使用以下命令生成计数和大小:

Import-Csv .\CleanupOutputLog.csv | Measure-Object -Sum FileSize

(Import-Csv .\CleanupOutputLog.csv | Measure-Object -Sum FileSize).Sum / 1MB
© www.soinside.com 2019 - 2024. All rights reserved.