Python Forum

Full Version: [Windows] Change directory date?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I mistyped a "dir" command in Windows, and now all the subdirectories have the (wrong) same date :-/

As a way to get a more realistic date, I'd like to 1) loop through each subdirectory, 2) find the oldest file at the root in each, and 3) use that to set the subdirectory's date… but Google didn't help: Is there a command to set a directory's date?

Thank you.

import os

ROOT = r"c:\html"
os.chdir(ROOT)
dirs = [f for f in os.listdir() if os.path.isdir(f)]
for dir in dirs:
	os.chdir(rf"{ROOT}\{dir}")
	files = sorted(os.listdir(os.getcwd()), key=os.path.getmtime)
	oldest = files[0]
	atime = os.stat(oldest).st_atime
	#TODO how to use "oldest" to set the parent folder's time?
[attachment=3295]
Not a Windows user here but from what I read online, you should be able to change the folder's dates by using either Powershell or Git bash. If you can do this it could be automated with Python.