2

I have made several backups over the years of our Enterprise Database. Sometimes I want to see some historical aspect of the data. I just realized that what I had been thinking was the oldest backup may or may not be the oldest because the file folder that stores the data (with the .gdb extension) updates simply from opening it in ArcMap (e.g. SDE_DEFAULT_BKUP.gdb was the oldest in the folder until today when I opened it to look at one of the feature classes).

How can I actually know which of these file geodatabases in my backups folder is the oldest?

enter image description here

2
  • 4
    Right-click on a folder and look at the Date Created (rather than Date Modified)
    – Midavalo
    Commented Oct 29, 2018 at 20:38
  • That works too but is a little tedious when there are several. Commented Oct 30, 2018 at 14:24

2 Answers 2

3

Python can be used to get the creation time of a file. If you're using Windows you can use the following code. The output in the IDE window will be the created times of the GDBs sorted oldest first.

import os
import time

directory = r'path\to\directory\of\FGDBs'
fgdbs = [x for x in os.listdir(directory) if x.endswith('.gdb')]
gdb_dict = {}
for gdb in fgdbs:
    gdb_path = os.path.join(directory, gdb)
    t = os.path.getctime(gdb_path)
    created = time.ctime(t) #format time to readable format
    gdb_dict[gdb] = created

for item in sorted(gdb_dict.items(), key=lambda (k,v): v): #sort dictionary 
    print (item)
1
  • That works pretty well so I'll use it (and accept it as the answer). Now you've got me trying to figure out how to sort by the date part of the value. It can sort by value but it is actually sorting by day first (e.g. Friday before Monday before Wednesday). Great answer and great use for native python! Commented Oct 30, 2018 at 14:26
1

This is the same code as in @mdholl6188 's wonderful solution. I'm just adding the code that I used for sorting. I used 3 #'s to show what I added to get it to sort in a way that was meaningful for me. The rest is the same.

import os
import time
### additional module
from datetime import datetime

directory = r'path\to\directory\of\FGDBs'
fgdbs = [x for x in os.listdir(directory) if x.endswith('.gdb')]
gdb_dict = {}
for gdb in fgdbs:
    gdb_path = os.path.join(directory, gdb)
    t = os.path.getctime(gdb_path)
    created = time.ctime(t) #format time to readable format
    ### new line follows
    newtime = datetime.strptime(created, '%a %b %d %H:%M:%S %Y').strftime('%Y %m %d %H:%M:%S')
    gdb_dict[gdb] = newtime ### was created

for item in sorted(gdb_dict.iteritems(), key=lambda (k,v): v): #sort dictionary
    print (item)
1
  • Nice! I didn't realize the answer I provided sorted the date alphabetically instead of the day index. Thanks for the clarification!
    – mdholl6188
    Commented Oct 30, 2018 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.