[英]Powershell delete 6 days old folder
我必須制作PS腳本才能將其與我們的自動化軟件集成。 我是PS的初學者,我做了一些嘗試,但仍然沒有成功。 這是我的任務。 我有一個參數將用作輸入參數。 那將是遠程磁盤路徑%setupRootPath%。 該目錄具有多個子目錄,例如(測試版,測試版,發行版等),每個子目錄都有項目名稱。 在持續集成系統中,每個項目都獲得Build Nomber,所以最后我得到了類似
目前有數百個項目,我制作了滿足自己需求的C#代碼,但是如果我可以通過PS而不是C#與sheduler一起運行它,那就太好了
這是C#代碼
class Program
{
const string rootDirPath = @"\\pcName\AutomationProcess\Deployment";
const int keepNum = 1;
const int deployDepth = 2;
static void Main(string[] args)
{
var delDir = new DirectoryInfo(rootDirPath);
var currentDateTime = DateTime.Now;
int currentDetph = 0;
var deployPathes = new Dictionary<DirectoryInfo, int>();
GetDeployDirectories(delDir, currentDetph, deployPathes);
foreach (var projektDir in deployPathes.Keys)
{
var dirInfoArray = projektDir.GetDirectories();
if (dirInfoArray != null)
{
var sortedDeploymentDirs = from d in dirInfoArray
where (currentDateTime - d.LastWriteTime).Days > 6
orderby d.LastWriteTime
select d;
var deploymentDirsArray = sortedDeploymentDirs.ToArray();
if (deploymentDirsArray.Length > keepNum)
{
for (int i = 0; i < deploymentDirsArray.Length - keepNum; i++)
{
Console.WriteLine("Delete: {0}", deploymentDirsArray[i].FullName);
}
}
}
}
}
private static void GetDeployDirectories(DirectoryInfo currentDirectory, int currentDetph, Dictionary<DirectoryInfo, int> resultDictionary)
{
currentDetph++;
foreach (DirectoryInfo subDir in currentDirectory.GetDirectories())
{
if (currentDetph < deployDepth)
{
subDir.Refresh();
GetDeployDirectories(subDir, currentDetph, resultDictionary);
}
else if (currentDetph == deployDepth)
{
resultDictionary.Add(subDir, currentDetph);
}
}
}
}
我無法刪除所有舊文件,我必須刪除所有早於6天的文件,但是即使是較舊的版本也請保留上一個版本。
所以刪除后我應該得到例如這樣的結果
我將在明天嘗試一下。 但是,如果有人會幫助。 我會很高興的:)這是我目前的狀態。 它有效,但是僅當我給出整個Projekt文件時才有效,而不僅僅是根目錄;(
param([String] $delpath=\\pcName\AutomationProcess\Deployment\Beta\ProjektA\",[String] $keepNum="1")
function Get-SubDirDate($directory)
{
$datetime = $directory.LastWriteTime
$SubDirDatealldir = gci $directory.FullName -filter * | ? { $_.PSisContainer -eq $true}
$SubDirDatealldir = @($SubDirDatealldir)
foreach ($subdir in $SubDirDatealldir)
{
$subdir = $subdir -as [system.io.directoryinfo]
if ($subdir)
{
$days = $subdir.LastWriteTime
if ($days -ge $datetime)
{
$datetime = $days
}
}
}
$datetime
}
cd $delpath
$localdir = get-location
if ($localdir.Path.IndexOf($delpath) -ne -1)
{
$datehash = @{}
$CurrentTime = Get-Date
# Mach es rekursive ;(
$alldir = gci -filter * | ? { $_.PSisContainer -eq $true}
$alldir = @($alldir)
foreach ($dir in $alldir)
{
$dir = $dir -as [system.io.directoryinfo]
$dir.Refresh()
if (($dir) -and ($dir.Exists))
{
if (!$datehash.ContainsKey($dir.FullName))
{
$datehash[$dir.FullName] = Get-SubDirDate $dir
}
}
}
# Alle Verz. Aelter als 6 Tage bis auf die letzten n(=keepNum) loeschen
$itemCount = $datehash.Count
if ($itemCount -gt ([int]$keepNum))
{
# Hash-Array nach Zeitstempel (=Value) absteigend sortieren
# (d.h. neuster Eintrag ist erstes Element) und die neusten n Faelle
# rausnehmen, damit diese nachfolgend nicht geloescht werden.
$datehashSorted = $datehash.GetEnumerator() |
Sort-Object Value -Descending | select-object -Last ($itemCount - [int]$keepNum)
# Alle übrigen, die Älter als 6 Tage sind, löschen
$datehashSorted | foreach-object {
$dir = $_.Name -as [system.io.directoryinfo]
$diffdays = ($CurrentTime-$_.Value).Days
if ($diffdays -ge 6)
{
Write-Host ($dir.fullname)
$dir.Delete($true)
}
}
}
}
我想這就是你所追求的。 從代碼角度來看,您當然可以提高效率,或者輕松地將其轉換為函數,但是我試圖使其可讀。
# Your input parameter
$setupRootPath = "\\pcName\AutomationProcess\Deployment\"
# Retrieve the top level directories (Beta, Testing, Release, etc.).
Get-ChildItem $setupRootPath | Where-Object {$_.PSisContainer -eq $true} | ForEach-Object {
$topDir = $_
# Pull the list of projects in the current top level dir.
Get-ChildItem $topDir | Where-Object {$_.PSisContainer -eq $true} | ForEach-Object {
$projectDir = $_
#Get the list of build folders, sorting by LastWriteTime, skipping the most recent, where the file is older than 6 days; and delete the remaining items.
Get-ChildItem $projectDir | Where-Object {$_.PSisContainer -eq $true} | Sort-Object LastWriteTime -Descending | Select-Object -Skip 1 | Where-Object {((Get-Date)-$_.LastWriteTime).TotalDays -gt 6} | Remove-Item
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.