[英]PowerShell to change "Date Modified" base on a specific date for Multiple Files
I am trying to change the "Date Modified" (LastWriteTime) of over 2K files in a specific folder.我正在尝试更改特定文件夹中超过 2K 文件的“修改日期”(LastWriteTime)。 All files were modified on the same Day.
所有文件均在同一天修改。 However, it does have multiple times.
但是,它确实有多次。 So, basically what I am trying to accomplish is this:
所以,基本上我想要完成的是:
Myfile1.Zip --> Date Modified = "1/4/2022 12:21 PM" ==> Date Modified = "1/3/2022 9:00 AM" Myfile1.Zip --> 修改日期 = "1/4/2022 12:21 PM" ==> 修改日期 = "1/3/2022 9:00 AM"
Myfile2.Zip --> Date Modified = "1/4/2022 12:25 PM" ==> Date Modified = "1/3/2022 9:00 AM" Myfile2.Zip --> 修改日期 = "1/4/2022 12:25 PM" ==> 修改日期 = "1/3/2022 9:00 AM"
Meaning that all files that were Modified on 1/4/2021 needs to display "Date Modified" as 1/3/2021, time really does not matter here.这意味着在 2021 年 1 月 4 日修改的所有文件都需要将“修改日期”显示为 2021 年 1 月 3 日,时间在这里真的无关紧要。
Is it possible to do a bulk change with a PowerShell Script?是否可以使用 PowerShell 脚本进行批量更改? I am not familiar at all with it and need this change made asap.
我对它一点也不熟悉,需要尽快做出改变。
Thanks in Advance for your help.在此先感谢您的帮助。
You can inspect the existing LastWriteTime
property on file system objects returned by Get-ChildItem
, then assign a new value to the same:您可以检查
Get-ChildItem
返回的文件系统对象的现有LastWriteTime
属性,然后为其分配一个新值:
# define date/time variables
$filterDate = (Get-Date -Day 4 -Month 1 -Year 2022).Date
$targetDateTime = Get-Date -Day 3 -Month 1 -Year 2022 -Hour 9 -Minute 0 -Second 0
# locate and filter relevant files
$relevantFiles = Get-ChildItem -Path .\path\to\folder\ |Where-Object { $_.LastWriteTime.Date -eq $filterDate }
# update their timestamps
$relevantFiles |ForEach-Object {
$_.LastWriteTime = $targetDateTime
}
Dereferencing the Date
property on an existing [DateTime]
value gives you the date at midnight, so the comparison $_.LastWriteTime.Date -eq $filterDate
will work regardless of whether the file was updated at 1AM or 6PM, as long as it was on January 4.取消对现有
[DateTime]
值的Date
属性的引用会为您提供午夜的日期,因此无论文件是在凌晨 1 点还是下午 6 点更新,比较$_.LastWriteTime.Date -eq $filterDate
都将起作用,只要它是1 月 4 日。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.