HOW TO

Organize items in the ArcGIS Online root folder into specific folders based on the last modified date using ArcGIS API for Python

Last Published: January 18, 2023

Summary

There are several times when items pile up in the root folder of a user in ArcGIS Online. When dealing with multiple items, it is often desirable to have the items to be organized to identify the usage of the items in their respective months.

This article describes how to organize items in the ArcGIS Online Content page root folder by analyzing the last date modified and move it to a folder for the respective month the items were last modified using ArcGIS API for Python. The script can be run using a standalone .py file or through ArcGIS Notebook.

Procedure

  1. Import the necessary modules.
from arcgis.gis import GIS,ContentManager
import datetime
  1. Define the credentials for ArcGIS Online.
ago_gis=GIS('https://arcgis.com','Username','Password')
  1. Define the variables for the user, the root folder, and the name of the folders for the respective months.
me = ago_gis.users.me
rootitems=me.items(folder=None,max_items=1000)
year=['01-January','02-February','03-March','04-April','05-May','06-June','07-July','08-August','09-September','10-October','11-November','12-December']
  1. Create the folders according to the name specified in Step 3.
year1=['01-January','02-February','03-March','04-April','05-May','06-June','07-July','08-August','09-September','10-October','11-November','12-December']
for month in year1:
    ago_gis.content.create_folder(month)
  1. Create a list of items in the root folder and date modified.
DatemodifiedList=[]

for i in rootitems:
    d=(i.modified/1000)
    dt = datetime.datetime.fromtimestamp(d)
    tempstring=str(dt.date())+'-'+i.title
    DatemodifiedList.append(tempstring)
  1. Move all items to the folders for the corresponding months of the last date modified.
for item in rootitems:
    epochtime=(item.modified/1000)
    date_time = datetime.datetime.fromtimestamp(epochtime)
    itemYear=date_time.year
    Month=date_time.month
    item.move(year[Month-1])

DatemodifiedList
print ("Items are organized")

The following is a sample of a full script:

from arcgis.gis import GIS,ContentManager
import datetime

ago_gis=GIS('https://arcgis.com','User1_esri','useresri')

me = ago_gis.users.me
rootitems=me.items(folder=None,max_items=1000)
year=['01-January','02-February','03-March','04-April','05-May','06-June','07-July','08-August','09-September','10-October','11-November','12-December']

year1=['01-January','02-February','03-March','04-April','05-May','06-June','07-July','08-August','09-September','10-October','11-November','12-December']
for month in year1:
    ago_gis.content.create_folder(month)

DatemodifiedList=[]

for i in rootitems:
    d=(i.modified/1000)
    dt = datetime.datetime.fromtimestamp(d)
    tempstring=str(dt.date())+'-'+i.title
    DatemodifiedList.append(tempstring)

for item in rootitems:
    epochtime=(item.modified/1000)
    date_time = datetime.datetime.fromtimestamp(epochtime)
    itemYear=date_time.year
    Month=date_time.month
    item.move(year[Month-1])#The minus one is to indicate the position in the list

DatemodifiedList
print ("Items are organized")

Article ID:000023812

Software:
  • ArcGIS Online
  • ArcGIS API for Python 1 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic