Powershell - 删除文件夹中特定名称超过 30 天的子文件夹 [英] Powershell - Delete subfolder(s) in folder with specific name(s) older than 30 days

查看:80
本文介绍了Powershell - 删除文件夹中特定名称超过 30 天的子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计算器溢出,我试图想出一个 PoSh 脚本来完成一个非常具体的任务,让我在这里用头撞到我的桌子.我是 Powershell 的新手,需要一些帮助.

stackoverflow, I am trying to come up with a PoSh script to do a very specific task that has me beating my head against my desk here. I am pretty new to Powershell, and need some help here.

这是我想要完成的:

我需要能够在驱动器中搜索名为30 Day Toss"的特定文件夹,并删除其中创建日期超过 30 天的所有子文件夹.

I need to be able to search through a drive for a specific folder called "30 Day Toss" and delete any subfolder(s) inside of it that has a creationdate of over 30 days old.

示例:

P:\Project\2014\3D1004\Plan 1\30 Day Toss\Archive1

P:\Project\2014\3D1004\Plan 1\30 Day Toss\Archive1

P:\Project\2014\3D1004\Plan 2\30 Day Toss\04-12-2014

P:\Project\2014\3D1004\Plan 2\30 Day Toss\04-12-2014

上面的 Archive1 文件夹是 10 天前创建的,不需要删除.上面的 04-12-2014 文件夹是在 30 多天前创建的,需要删除,以及它下面的所有文件/文件夹.

The Archive1 folder above was created 10 days ago and doesn't need deleted. The 04-12-2014 folder above was created over 30 days ago and needs deleted, along with all files/folders under it.

项目、年份、工作编号和计划编号都是可变的和变化的,因此硬编码搜索路径不是一种选择.

The Project, year, job number and plan number are all variable and change, so hard coding a search path isn't an option.

非常感谢任何帮助,谢谢!

Any assistance would be greatly appricieated, thanks!

推荐答案

好的,假设您的文件夹结构始终相同,并且您总是想要第 6 级文件夹或其中的子文件夹,这将起作用:

Ok, so assuming that your folder structure is always the same, and you always want the 6th level of folder or subfolders of that, this will work:

gci P:\ -directory -recurse | ?{$_.FullName -match ".:\\.+?\\.+?\\.+?\\.+?\\.+?\\" -and $_.CreationTime -lt (get-date).AddDays(-30)}|Remove-Item -recurse -whatif

查看整个 P: 驱动器中的文件夹,如果文件夹名称是:

That looks at the entire P: drive for folders, if the folder name is:

P:\<anything>\<anything>\<anything>\<anything>\<anything>\<anything>

它会检查它是否是在 30 多天前创建的,如果是,它会删除它及其所有内容.但它必须有这么多级别的脚本才能查看它.

It checks to see if it was created over 30 days ago, and if so it deletes it and all of it's contents. But it has to have that many levels for the script to look at it.

好的,让我们稍微改变一下.如果我们正在查找的文件夹始终称为30 Day Toss",我们将查找具有该名称的所有文件夹,然后拉出每个文件夹中的内容列表.够简单!一、代码:

Ok, let's change it up a little. If the folder we're looking inside of is always called "30 Day Toss" we'll look for all folders with that name, and then pull a listing of things inside each of them. Easy enough! First, the code:

gci P:\ -Directory -Recurse | ?{$_.Name -ieq "30 Day Toss"} | %{GCI $_.FullName | ?{$_.CreationTime -lt (get-date).AddDays(-30)} | Remove-Item -Recurse -Force}

然后稍微分解一下.

首先,我们获得 P: 驱动器上所有文件夹的列表.然后我们只选择名为30 天折腾"的那些(不区分大小写).

First we get a listing of all folders on the P: drive. Then we select only the ones named "30 day toss" (not case sensitive).

对于我们找到的每个具有该名称的文件夹,我们为该文件夹中的所有内容提取目录列表,然后仅选择超过 30 天的内容.对于超过 30 天的所有内容,我们都会删除它们,以及其中的任何内容.

For each folder we find with that name we pull a directory listing for whatever is inside that folder, and then select only the things older than 30 days. For each of those things that are older than 30 days we delete them, and anything inside them.

这篇关于Powershell - 删除文件夹中特定名称超过 30 天的子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆