Python Forum
[Windows] Change directory date?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Windows] Change directory date?
#1
Question 
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?
   
Reply
#2
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.
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 2,109 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 2,791 Jan-20-2024, 04:45 AM
Last Post: 1418
  working directory if using windows path-variable chitarup 2 1,530 Nov-28-2023, 11:36 PM
Last Post: chitarup
  change directory of save of python files akbarza 3 3,543 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  [SOLVED] [Windows] Right way to prompt for directory? Winfried 4 3,243 Jan-17-2023, 09:28 PM
Last Post: markoberk
  Date format and past date check function Turtle 5 10,237 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,873 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  change mouse cursor (in Windows) mgroen 0 2,128 Nov-11-2020, 11:26 AM
Last Post: mgroen
  How to add date and years(integer) to get a date NG0824 4 4,001 Sep-03-2020, 02:25 PM
Last Post: NG0824
  Need info for getting directory content in Windows wlathan 2 2,558 May-20-2020, 12:40 PM
Last Post: wlathan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020