简体   繁体   English

Powershell删除除某些符合特殊日期条件的文件外的所有文件

[英]Powershell to delete all files except some meeting special date criteria

I've a scenario. 我有一个场景。 Before moving to that, let me say I need a logic as well from here. 在继续之前,我要说的是,我还需要一个逻辑。 I've tried something (which I will tell shortly), but that doesnt seems to be working. 我已经尝试了一些方法(稍后将告诉我),但这似乎没有用。

I've some files which is getting created each hour. 我有一些每小时创建的文件。 I am scripting (powershell) to delete those in the way given below: 我正在编写脚本(powershell)以以下方式删除这些脚本:

Out of 24 files created in a day, I need to keep only 1 (which is the last and latest created, need to delete other 23) daily for an entire week. 在一天创建的24个文件中,我整周每天只需要保留1个文件(这是最近创建的文件,需要删除其他23个文件)。 This is DONE. 这个完成了。

Now, on a weekly basis I need to do this in such a way that every saturday night, I need to delete 6 daily files and retain the latest one (of day 7). 现在,我需要每周执行一次这样的操作,即在每个星期六晚上,我需要删除6个每日文件并保留最新的文件(第7天)。 I am stuck here. 我被困在这里。

What I've tried is like using $date.AddDays(-1) which is okay for the first week. 我试过的就像使用$ date.AddDays(-1),这对于第一周来说还可以。 Second week, I need to retain last week's (1) file and need to retain this week's (1) file. 第二周,我需要保留上周(1)的文件,并且需要保留本周(1)的文件。 So, here I am running out of logic. 因此,这里我的逻辑已不足。

In the same scenario I need to have it monthly also. 在相同的情况下,我也需要每月一次。 I think I can get this done if I get some logic for weekly (which is in Bold ). 我想如果我每周有一些逻辑(以粗体显示 ),我可以做到这一点。

Can anyone please help me at the earliest. 谁能最早帮助我。 Any suggestions or thoughts would be really appreciated. 任何建议或想法将不胜感激。

Why bother calculate anything? 为什么要计算什么呢? Takes advantage of powershell's power :) ! 利用powershell的功能:)!
Get a date (from a file, from a var, or whatever) cast it do datetime and display the dayOfWeek property 获取一个日期(从文件,变量或其他任何东西),将其转换为日期时间并显示dayOfWeek属性

([system.datetime]"09/07/2015").DayOfWeek

Forigve me if I got lost in all the comments but you could use this to get all the dates in the current month that are saturdays. 如果我在所有评论中迷路了,请原谅我,但是您可以使用它来获取当前月份中星期六的所有日期。

$now = Get-date
1..[DateTime]::DaysInMonth($now.Year,$now.Month) | ForEach-Object{get-date -day $_} | Where-Object{$_.DayOfWeek -eq "Saturday"}

So for July 2015 因此对于2015年7月

Saturday, July 04, 2015 8:22:53 AM
Saturday, July 11, 2015 8:22:53 AM
Saturday, July 18, 2015 8:22:53 AM
Saturday, July 25, 2015 8:22:53 AM

Of course you might want just the days so we can just add to the end of this. 当然,您可能只需要几天,所以我们可以在这末尾加上一些内容。 Select-Object -ExpandProperty Day . Select-Object -ExpandProperty Day

4
11
18
25

You can now use those values in you date exclusion logic. 现在,您可以在日期排除逻辑中使用这些值。 Which could be useful if you are trying to compare dates ahead of time. 如果您想提前比较日期,这可能会很有用。 Still PowerShell already has logic to caluculates individual dates to see if they are "saturday". PowerShell仍然具有计算单个日期以查看其是否为“星期六”的逻辑。 So like Kayasax was suggetsting 就像Kayasax暗示

Get-ChildItem *.* | Where-Object{ $_.lastwritetime.dayofweek -notmatch "saturday"} |select-object -expandproperty fullname

And add other date logic for hour or what not as required. 并添加其他日期逻辑为小时或不是所需的日期。

Start with the logic of the part you have already built, which is to group together files that were created in the same day, and retain only the latest file out of each group. 从您已经构建的零件的逻辑开始,该逻辑将将同一天创建的文件分组在一起,并仅保留每个组中的最新文件。 This undoubtedly depends on converting the timestamp of the file's creation into a date. 无疑,这取决于将文件创建的时间戳转换为日期。

What you need is a function, call it "week" which, given a date, returns the week number. 您需要的是一个函数,称为“周”,给定日期,该函数将返回周数。 So, for dates from Jul-5-2015 through Jul-11-2015, it might return the number "27", meaning the 27th week of the year. 因此,对于从2015年7月5日到2015年7月11日的日期,它可能会返回数字“ 27”,表示一年中的第27周。

Now create logic similar to the logic of what you have already built. 现在创建类似于您已构建的逻辑的逻辑。 But, instead of grouping by day, group by week number. 但是,不是按天分组,而是按周编号分组。

If you intend to keep this data for more than a year, you need to do something like the first week of next year returns "53". 如果您打算将此数据保留一年以上,则需要执行类似明年第一周返回“ 53”的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Powershell 根据日期范围条件删除文件夹文件 - Powershell delete folder files based on date range criteria Powershell删除除一个文件夹之外的所有文件夹 - Powershell delete all folders except one Powershell代码可删除目录中除最近2天的数据和2个文件夹外的所有文件和文件夹 - Powershell code to delete all files and folders in a directory except for last 2 days data and 2 folders PowerShell - “从该文件夹中删除除一个以外的所有文件”的最紧凑方式 - PowerShell - Most Compact way of "Delete all files from this folder except one" Powershell 搜索按特殊日期排序的文件 - Powershell search files sorted by special date PowerShell:列出除某些文件以外的所有项目 - PowerShell: List all items except for certain files 重命名除PowerShell中的一个以外的所有png文件 - Renaming all png files except one in PowerShell 电源壳| 在目录中按名称(Name.txt/Name(01).txt/Name(02).txt)查找相似文件,删除除最后修改外的所有文件 - Powershell | Find similar files by name(Name.txt/Name (01).txt/ Name (02).txt) in a directory and delete all except last modified Powershell - 删除除特定子目录中的项目之外的所有项目 - Powershell - Delete all items except those in specific subdirectories PowerShell - 按修改日期删除特定文件夹中的文件 - PowerShell - Delete files by modified date in specific folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM