使用Powershell以不同的时间间隔删除文件

时间:2017-06-12 18:17:20

标签: powershell-v2.0

我已经看过类似的问题,但是我决定发帖,因为我没有看到一个能够解决我所寻求的细微差别的问题。

我正在寻找一种方法来添加一个子句,只留下空文件,然后是用户维护的某些文件。

  1. 单独保留所有空文件夹
  2. 忽略指定的文件夹和文件类型
  3. 到目前为止,我有这个:

    Start-Transcript C:\Temp3\filecleanup.log -Append
    $folder_path = "[[-Path] <String[K:\]>"
    # replace -90 with days before current date to be deleted
    $1m = "-30"
    $3m = "-90"
    $6m = "-180"
    $12m = "-360"
    $18m = "-540"
    $curr_date = Get-Date
    
    #script for 30 days
    $del_date1m = $curr_date.AddDays($1m)
    Get-ChildItem $folder_path -Recurse [-Exclude <file folders, Applications, 
    *.bat>] | Where-Object { $_.LastAccessTime -lt $del_date1m } | Remove-Item -
    Force -Verbose
    
    #script for 90 days
    $del_date3m = $curr_date.AddDays($3m)
    Get-ChildItem $folder_path -Recurse [-Exclude <file folders, Applications, 
    *.bat>] | Where-Object { $_.LastAccessTime -lt $del_date3m } | Remove-Item -
    Force -Verbose
    
    #script for 180 days
    $del_date1m = $curr_date.AddDays($6m)
    Get-ChildItem $folder_path -Recurse [-Exclude <file folders, Applications, 
    *.bat>] | Where-Object { $_.LastAccessTime -lt $del_date6m } | Remove-Item -
    Force -Verbose
    
    #script for 360 days
    $del_date1m = $curr_date.AddDays($12m)
    Get-ChildItem $folder_path -Recurse [-Exclude <file folders, Applications, 
    *.bat>] | Where-Object { $_.LastAccessTime -lt $del_date12m } | Remove-Item 
    -Force -Verbose
    
    #script for 540 days
    $del_date1m = $curr_date.AddDays($18m)
    Get-ChildItem $folder_path -Recurse [-Exclude <file folders, Applications, 
    *.bat>] | Where-Object { $_.LastAccessTime -lt $del_date18m } | Remove-Item 
    -Force -Verbose
    Stop-Transcript
    

1 个答案:

答案 0 :(得分:0)

此脚本使用所有月份的哈希表:

变量$exclude_file_types可以包含目录以及* .bat等文件的结尾。

首先,它找到非空目录,然后使用foreach在其中循环。第二个循环使用哈希表循环遍历所有月份。

修改

# if you want to recurse set to true
$recurse = $true

$folder_path = 'C:\prg'
# hash table to access it one month -> $time_definition.1m
$time_definition=@{'1m'="-30"; '3m'="-90"; '6m'="-180"; '12m'="-360"; '18m'="-540"}
Write-host "$time_definition.1m"

# Directories should be with full path
$excluded_directories = @("C:\prg\PowerShell\_Snippets\search_files_or_directories\test")
# Exclude any extension
$excluded_file_types = @("*.bat", "*.cmd", "*.ps1")


# Get all non-empty directories
# If you want to disable the top directory with the whole tree beneath it remove the -Recurse option
$get_full_directories = Get-ChildItem -Path $folder_path -Recurse:$recurse -exclude $excluded_directories | Where-Object {$_.PSIsContainer -eq $True} | Where-Object {!$_.GetFiles().Count -eq 0}
#search in those directories for all periods
foreach ($search_dir in $get_full_directories) {
    foreach ($time in $time_definition.GetEnumerator()) {
        # to check
        #write-host 'time_months:'$($time.Name);
        write-host 'Currently writing for these months:'$($time.Name);
        Get-ChildItem -Path $search_dir -recurse -exclude $excluded_file_types | Where-Object {$_.PSIsContainer -eq $FALSE} | where {$_.LastAccessTime -lt (get-date).adddays($time_definition.$($time.Name))} | Remove-Item -Force -Verbose
    }
}

我测试了除最后一个管道| Remove-Item -Force -Verbose之外的所有代码,但这应该可以很好地工作。