Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
默认情况下,X天后删除旧文件,但某些目录上的文件除外[Powershell]_Powershell_Scripting - Fatal编程技术网

默认情况下,X天后删除旧文件,但某些目录上的文件除外[Powershell]

默认情况下,X天后删除旧文件,但某些目录上的文件除外[Powershell],powershell,scripting,Powershell,Scripting,我正在尝试修复以下场景: 我有一个目录,其中有多个子目录和文件,我需要在其中设置类似于保留策略的内容,默认情况下,我会将文件设置为不超过365天,其中有一些特殊的目录,我希望保留不同的时间段,而不是默认值。这些目录在txt文件中用以下语法指定 驱动器内容:\Path\to\special\u dirs.txt D:\Path\to\vendor1 -396 D:\Path\to\vendor2 -45 这是我到目前为止提出的(它只处理特殊目录,下一部分我想继续处理其余的部分不起作用): 我不想

我正在尝试修复以下场景:

我有一个目录,其中有多个子目录和文件,我需要在其中设置类似于保留策略的内容,默认情况下,我会将文件设置为不超过365天,其中有一些特殊的目录,我希望保留不同的时间段,而不是默认值。这些目录在txt文件中用以下语法指定

驱动器内容:\Path\to\special\u dirs.txt

D:\Path\to\vendor1 -396
D:\Path\to\vendor2 -45
这是我到目前为止提出的(它只处理特殊目录,下一部分我想继续处理其余的部分不起作用):

我不想删除目录,即使它们是空的,我只是想删除文件,脚本的最后一部分只是删除所有超过365天的内容,其中有一个目录,我想保留30天以上的默认时间段,如何做到这一点?
我使用了文件列表,这样我就可以继续向供应商添加目录,在这些目录中,我可以保留超过或甚至少于默认天数。

这样的内容怎么样

$30DayDirs = @('C:\Temp\Test1\','C:\Temp\Test2\')
$60DayDirs = @('C:\Temp\Test3\')
$365DayDirs = @('C:\Temp\Test4\')

foreach($file in $30DayDirs){
    $file = Get-ChildItem $30DayDirs -Recurse |where LastWriteTime -LT (Get-Date).AddDays(-30)
    Remove-Item $file.FullName -Force -Recurse
}

foreach($file in $60DayDirs){
    $file = Get-ChildItem $60DayDirs -Recurse |where LastWriteTime -LT (Get-Date).AddDays(-60)
    Remove-Item $file.FullName -Force -Recurse
}

foreach($file in $365DayDirs){
    $file = Get-ChildItem $365DayDirs -Recurse |where LastWriteTime -LT (Get-Date).AddDays(-365)
    Remove-Item $file.FullName -Force -Recurse
}

我已解决了以下问题:

# Script to remove old files from DFS Archive: D:\Path\to
# Declaring variables to be used
$controlfile = "Drive:\Path\to\list\special_dirs.txt"
$dir = "D:\Path\to"
$default_days = "-365"
$excluded_dirs = Get-Content $controlfile | Foreach-Object {$_.Split()[0]}

# Removing old files from special directories first
cd $dir
foreach ($line in Get-Content $controlfile) {
    $split = $line.split(" ")
    $file_path = $split[0]
    $max_days = $split[1]

    Get-ChildItem $file_path -Include *.* -File -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays($max_days))} | Remove-Item -Force -Recurse -Verbose
}

# Removing everything else older than 365 days old
Get-ChildItem $dir -Directory -Exclude $excluded_dirs | Get-ChildItem -Include *.* -File -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays($default_days))} | Remove-Item -Force -Recurse -Verbose
我已将我的特殊_dirs.txt文件修改为:

vendor1 -396
vendor2 -45

谢谢你的帮助。我只是尝试在我的案例中实现它,但是问题如下:在C:\Temp\下面有一堆其他目录,在这种情况下,只有其中两个需要不同的时间才能删除,其余的和新的文件夹应该保留一年。我想保留一个列表文件,以便我可以轻松地添加需要不同保留期的新目录
vendor1 -396
vendor2 -45