1

In 2018, after moving my files to another drive, the modified date of my folders is updated. Is there way to change folder's modified date based on the latest modified file in the folder?

The folder path is D:\Yeni klasör\complete
There are 800 subfolders even maybe more! All of them is in the "D:\Yeni klasör\complete" parent folder.

These subfolders contain mostly music files. Example of one folder called "2003-Harem" :

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

Look the modified date of the latest file of that "2003-Harem" :

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

I want these subfolders to be be exact same date/time what they got in inside. Example 2003-Harem folder should be 20.04.2016

Is there way to do it with one Powershell code? I am so bad at CMD/Powershell so the parent folder path is at the top.

I am even more bad at describing about what i want. Please don't hesitate to ask if you did not understand anything. :D Thanks in advance!

1 Answer 1

3

You can try something like this:

$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

basically looks for the file with the most current time, and updates the parent folder to that time.

EDIT:

I haven't tested it out, but the logic is the same as what I commented so it should work. Test it on a few folders and not all of them first.

#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
        }
12
  • It worked! Sir thank you so much but is there way to apply this to whole parent folder so it can change all subfolder's date. If it cannot be done, i will do it manually 1000 times,
    – Tromick
    Commented May 19, 2021 at 10:19
  • Can you clarify, or give an example of what you mean? Is it just one parent folder? Commented May 19, 2021 at 11:36
  • Do you want to do the same thing for every folder in the directory of "complete"? Commented May 19, 2021 at 11:43
  • Yes sir! I want to do same thing for every folder in the directory of 'complete'.
    – Tromick
    Commented May 19, 2021 at 11:45
  • Sure, that's easy enough but, I'm not sure you would be learning anything if I just write the script for you. How about if I explain it, and you write it, then we can see if you did it right together? Commented May 19, 2021 at 11:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .