C# Powershell删除6天以前的文件夹
我必须使PS脚本集成到我们的自动化软件。我是PS的初学者,我做了一些尝试,但仍然没有成功。这是我的任务。 我有一个参数将用作输入参数。这将是远程磁盘路径 %setupRootPath%。该目录有多个子目录,如Beta、Testing、Release等。每个子目录都有项目名称。在持续集成系统中,每个项目都有构建名称,所以最后我得到了 %setupRootPath%\Beta\ProjektA\Build1000 %setupRootPath%\Beta\ProjektA\Build1003 %setupRootPath%\Beta\ProjektA\Build1004 %setupRootPath%\Beta\ProjektB\Build1007 %setupRootPath%\Beta\ProjektB\Build1008 %setupRootPath%\Beta\ProjektB\Build1009 %setupRootPath%\Beta\ProjektB\Build10010 %setupRootPath%\SystemTesting\ProjektA\Build1002 目前有数百个项目,我编写了C代码来满足我的需求,但是如果我可以通过PS而不是C使用调度程序来运行它,那就太好了 这是C代码C# Powershell删除6天以前的文件夹,c#,powershell,automation,teamcity,C#,Powershell,Automation,Teamcity,我必须使PS脚本集成到我们的自动化软件。我是PS的初学者,我做了一些尝试,但仍然没有成功。这是我的任务。 我有一个参数将用作输入参数。这将是远程磁盘路径 %setupRootPath%。该目录有多个子目录,如Beta、Testing、Release等。每个子目录都有项目名称。在持续集成系统中,每个项目都有构建名称,所以最后我得到了 %setupRootPath%\Beta\ProjektA\Build1000 %setupRootPath%\Beta\ProjektA\Build1003 %se
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);
}
}
}
}
我想这就是你想要的。您当然可以从代码的角度提高效率,或者轻松地将其转换为函数,但我试图使其可读
# 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
}
}
如果您已经有一个C语言的工作程序,为什么不直接从powershell脚本调用它呢?用于功能更改。更改TeamCity步骤中编写的脚本比维护C代码和应用程序更容易。非常感谢。因此,当我键入instad时,选择对象-跳过1 |选择对象-跳过$keepNum也应该起作用。但使用此解决方案,我无法配置构建项目的深度。不管怎样,我今天要试试。再次非常感谢:
# 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
}
}