Python Forum
[SOLVED] [Windows] Change directory date?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [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
#3
Thanks. I ended up using NirSoft's FolderTimeUpdate.

It uses "the latest modified time of the files stored in it", instead of the oldest, but it was good enough in this particular case.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] [Windows] Is this right way to run a CLI app? Winfried 2 1,051 Nov-28-2024, 10:01 AM
Last Post: Winfried
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,729 Oct-17-2024, 01:15 AM
Last Post: Winfried
  [SOLVED] Reportlab - change a single page orientation beetlecad 1 1,711 Aug-16-2024, 05:43 PM
Last Post: beetlecad
  Compare current date on calendar with date format file name Fioravanti 1 2,212 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 2,897 Jan-20-2024, 04:45 AM
Last Post: 1418
  working directory if using windows path-variable chitarup 2 1,575 Nov-28-2023, 11:36 PM
Last Post: chitarup
  change directory of save of python files akbarza 3 3,712 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  [SOLVED] [Windows] Fails reading strings with accents Winfried 1 1,598 Apr-23-2023, 05:27 PM
Last Post: Larz60+
  [SOLVED] [Windows] Right way to prompt for directory? Winfried 4 3,319 Jan-17-2023, 09:28 PM
Last Post: markoberk
  [SOLVED] Alternative to regex to extract date from whole timestamp? Winfried 6 3,835 Nov-16-2022, 01:49 PM
Last Post: carecavoador

Forum Jump:

User Panel Messages

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