Directory Last Modified Date

Directory last modified date

The mtime (modification time) on the directory itself changes when a file or a subdirectory is added, removed or renamed.

Modifying the contents of a file within the directory does not change the directory itself, nor does updating the modified times of a file or a subdirectory. Additionally, adding, removing or renaming files/directories in subdirectories does not propagate up to the directory. If you change the permissions on the directory, the ctime changes but the mtime does not.

Rules for Date Modified of folders in Windows Explorer

This is not explorer specific, this is NTFS-related.

See http://support.microsoft.com/kb/299648 https://web.archive.org/web/20080219020154/http://support.microsoft.com/kb/299648 for some rules.

Note: Modified time can be disabled with filesystem option, so you should never rely on it. Any suggested reliable methods would be appreciated.

Possible to get last modified date of folder?

Instead of hoping that the directory entries are returned in ascending date order, you should actively find the latest one. You can do that with the LINQ Max method like this:

Shared Function GetLatestFileModified(d As String) As DateTime

Dim di = New DirectoryInfo(d)
Dim latest = di.EnumerateFiles().Max(Function(i) i.LastWriteTimeUtc)

Return latest

End Function

Shared Function GetLatestDirectoryModified(d As String) As DateTime

Dim di = New DirectoryInfo(d)
Dim latest = di.EnumerateDirectories().Max(Function(i) i.LastWriteTimeUtc)

Return latest

End Function

For example,

Dim src = "C:\temp"
Console.WriteLine(GetLatestFileModified(src).ToShortDateString())
Console.WriteLine(GetLatestDirectoryModified(src).ToShortDateString())

might give

26/04/2019

10/04/2019

How do I pull the 'last modified time' of each file within a directory in Python?

The os.listdir() method lists the files of the given path excluding the path, hence you will need to concatenate the path yourself:

for file in os.listdir('../File Transfer/Old Files/'):
if file.endswith('.txt'):
time_mod = os.path.getmtime('../File Transfer/Old Files/' + file)
print(time_mod)

The glob.glob() method works great in cases like this:

import os
import glob

for file in glob.globr('../File Transfer/Old Files/*.txt'):
time_mod = os.path.getmtime('../File Transfer/Old Files/' + file)
print(time_mod)

You can get the amount of hours passed since the last modification of each file like so:

import os
from time import time

PATH = '../File Transfer/Old Files/'

for file in os.listdir(PATH):
if file.endswith('.txt'):
time_mod = time() - os.path.getmtime(PATH + file)
print(time_mod // 3600)

how get directory last modify date = textboxdate and get file in this directory c#

You can pass the directory path to the instance of the DirectoryInfo and use the DirectoryInfo class, LastWriteTime property to figure out when the directory was last written to (modified):

DirectoryInfo info = new DirectoryInfo("myDirPath");
if (info.LastWriteTime > someDate){
var allfiles = Directory.GetFiles(path, "*.*", System.IO.SearchOption.AllDirectories);
//do something on allfiles
}

If it was last written after certain time, then get all the files in that directory by what you have shown and you could proceed as you want.

Edit:

If you need sub-directories instead of files, you could use Directory.GetDirectories() instead. And to get the directory last write time, simply do the same thing as above:

var alldirs = Directory.GetDirectories("myRootPath")
.Select(x => new DirectoryInfo(x));
foreach (var dir in alldirs) {
if (dir.LastWriteTime > someDateTime) {
//do something
}
}

Python - It returns the modified date from the folder

You need to list all the files in the directory and find timestamp after that. Below is a sample code.

Update - Added handling for Windows and Linux separately.

import os
import time
import platform
from datetime import datetime

path = 'C://Reports/Script/'
files_path = ['%s%s'%(path, x) for x in os.listdir(path)]

print platform.system()

for file_p in files_path:
if platform.system() == 'Windows':
modTimesinceEpoc = os.path.getctime(file_p)
else:
statbuf = os.stat(file_p)
modTimesinceEpoc = statbuf.st_mtime

modificationTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(modTimesinceEpoc))
modificationTime = datetime.strptime(modificationTime, '%Y-%m-%d %H:%M:%S')

print file_p, modificationTime

How do I get the last modified date of a file in multiple directories?

IF your system is Windows Vista or later (or if you are allowed to download the Windows Server 2003 Resource Kit Tools), you can use robocopy to copy the 4 files into a folder. Yes, i have readed the notes. The trick is that the /create switch in robocopy generate a 0 bytes instance of the file in the target folder but maintains the timestamp of the files. Then you can use your first code to solve the problem

If you can not use robocopy, but administrador rights are not a problem

@echo off
setlocal enableextensions disabledelayedexpansion

for %%f in (
"c:\\somewhere\\file1.txt"
"c:\\other\\place\\file2.txt"
"c:\\somewhere\\file2.txt"
) do for /f "tokens=2 delims==." %%a in ('
wmic datafile where name^="%%~f" get lastModified /value
') do set "_t[%%a]=%%~f"

for /f "tokens=2 delims==" %%a in ('set _t[') do set "lastFile=%%a"
set "lastFile=%lastFile:\\=\%"

echo %lastFile%

This code uses wmic to retrieve the timestamp of the files, store in an array in environment using the timestamp as index. Then retrieve the list, that will be in ascending order. The last element in the array is the newer file.



Related Topics



Leave a reply



Submit