Powershell删除6天前的文件夹
本文关键字:文件夹 6天 删除 Powershell | 更新日期: 2023-09-27 18:05:07
我必须使PS脚本与我们的自动化软件集成。我是PS的初学者,我做了一些尝试,但仍然没有成功。这是我的任务。我有一个参数,将被用作输入参数。即远程磁盘路径% setupRootPath %。这个目录有多个子目录,比如(Beta, Testing, Release等),每个目录都有项目名称。在持续集成系统中,每个项目都有Build number,所以最后我得到了类似
的东西。- β
- % setupRootPath % ' ' ProjektA ' Build1000 β
- % setupRootPath % ' ' ProjektA ' Build1003 β
- % setupRootPath % ' ' ProjektA ' Build1004 β
- % setupRootPath % ' ' ProjektB ' Build1007 β
- % setupRootPath % ' ' ProjektB ' Build1008 β
- % setupRootPath % ' ' ProjektB ' Build1009 β
- % setupRootPath % ' ' ProjektB ' Build10010
- % setupRootPath % ' SystemTesting ' ProjektA ' Build1002
目前有数百个项目,我做了c#代码来做我想要的,但如果我能通过PS而不是c#用调度程序运行它将是伟大的
这里是c# Codeclass 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天的文件,但保留最后一个构建,即使它是旧的。
那么在删除之后我应该得到这样的结果例如
- β
- % setupRootPath % ' ' ProjektA ' Build1000(删除) β
- % setupRootPath % ' ' ProjektA ' Build1003(删除)
- %setupRootPath%'Beta'ProjektA'Build1004(旧的如果删除则没有,跳过) β
- % setupRootPath % ' ' ProjektB ' Build1007(删除) β
- % setupRootPath % ' ' ProjektB ' Build1008(删除)
- %setupRootPath%'Beta'ProjektB'Build1009(不超过6天,跳过)
- %setupRootPath%'Beta'ProjektB'Build10010(不超过6天,跳过)
- %setupRootPath%'SystemTesting'ProjektA'Build1002(较旧但只有一个,跳过)
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
}
}