使文件夹的上次修改日期与其子文件夹/文件的上次修改日期相对应

使文件夹的上次修改日期与其子文件夹/文件的上次修改日期相对应

2018 年,将我的文件移动到另一个驱动器后,我的文件夹的修改日期会更新。有没有办法根据文件夹中最新修改的文​​件来更改文件夹的修改日期?

文件夹路径为 D:\Yeni klasör\complete
有 800 个子文件夹,甚至更多!它们全部位于“D:\Yeni klasör\complete”父文件夹中。

这些子文件夹主要包含音乐文件。例如,一个名为“2003-Harem”的文件夹:

https://i.hizliresim.com/P7voqb.png

看一下那个“2003-Harem”的最新文件的修改日期:

https://i.hizliresim.com/2OvkkO.png

我希望这些子文件夹的日期/时间与它们内部的日期/时间完全相同。例如 2003-Harem 文件夹应该是 20.04.2016

有没有办法用一个 Powershell 代码来完成?我不擅长使用 CMD/Powershell,所以父文件夹路径在顶部。

我更不擅长描述我想要的东西。如果你有什么不明白的,请随时询问。:D 提前谢谢!

答案1

您可以尝试这样的操作:

$Current_File  = Get-ChildItem "C:\My\Path\Here" -file -Recurse | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1
$Parent_Folder = Split-Path $Current_File.FullName
$Folder_Item   = Get-Item -Path $Parent_Folder

#Change "LastWriteTime" property to most current file
$Folder_Item.LastWriteTime = $Current_File.LastWriteTime

基本上寻找具有最新时间的文件,并将父文件夹更新到该时间。

编辑:

我还没有测试过,但逻辑和我评论的一样,所以应该可以工作。先在几个文件夹上测试,而不是全部文件夹。

#Gather only the directories in your parent folder into a variable as an array
$Directory_Collection = Get-ChildItem -Path "C:\My\Parent_Folder" -Directory

#Create a foreach loop to iterate through all the folders 
    foreach($DIrectory in $Directory_Collection){
        

        #Use the current directory its on to loop through,
        #and get the most current file to update the directory date to
        $Current_File  = Get-ChildItem $DIrectory.FullName  -file -Recurse | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1
        $Parent_Folder = Split-Path $Current_File.FullName
        $Folder_Item   = Get-Item -Path $Parent_Folder

        #Change "LastWriteTime" property to most current file
        $Folder_Item.LastWriteTime = $Current_File.LastWriteTime
        }

相关内容