How To: Organize items in the ArcGIS Online root folder into specific folders based on the last modified date using ArcGIS API for Python
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
- Import the necessary modules.
from arcgis.gis import GIS,ContentManager import datetime
- Define the credentials for ArcGIS Online.
ago_gis=GIS('https://arcgis.com','Username','Password')
- 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']
- 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)
- 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)
- 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")
Related Information
Last Published: 1/17/2023
Article ID: 000023812
Software: ArcGIS Online Current ArcGIS API for Python 2.0.1, 2.0, 1.9.1, 1.9, 1.8.5, 1.8.4, 1.8.3, 1.8.2, 1.8.1, 1.8.0, 1.7.1, 1.7.0, 1.6.1, 1.6