简体   繁体   English

PowerShell 根据多个文件的特定日期更改“修改日期”

[英]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.

相关问题 PowerShell - 按修改日期删除特定文件夹中的文件 - PowerShell - Delete files by modified date in specific folders 如何从多个文件中提取特定的图案行并获取每行的文件名和修改日期-Powershell - How to extract specific pattern lines from multiple files AND get the file name & date modified for each line - Powershell PowerShell-获取多个文件的上次访问(修改)日期 - PowerShell - get last accessed (modified) date of multiple files 在PowerShell中更改多个文本文件中的日期格式 - Change date format in multiple text files in PowerShell Powershell 根据上次修改日期重命名文件 - Powershell to rename files based on laast modified date Powershell:比较上次修改到特定日期并替换为正确的日期 - Powershell: Compare Last Modified to Specific Date and replace with correct Date 使用 Powershell 归档具有特定日期的文件 - Archive Files with Specific Date with Powershell powershell或其他脚本使文件创建日期=修改日期 - powershell or other script to make files creation date = modified date Powershell脚本:列出具有特定更改日期的文件(可能的话,数量) - Powershell script: List files with specific change date (Amount if possible) Powershell - 根据文件名更改文件创建日期和修改日期 - Powershell - change file Date Created and Date Modified based on Filename
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM