Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I still working on my big project, big at least for me, but I am currently stuck on this:

I have a lots of .shp, that I want to move to a gdb. While creating a feature class, I want to add date (YMD) before the name: test.shp -> _120706_test

Possibly the creation date of the SHP but also the importing date will be ok.

I created a list of feature and tried: _fc = '' + fc BUT AIN'T WORKING!

THANKS A LOT

# Import system modules 
import sys, arcpy, datetime, os, traceback, time 
from arcpy import env 

# Load required toolboxes... 
arcpy.AddToolbox("C:\\Program Files (x86)\\ArcGIS\\Desktop10.0\\ArcToolbox\\Toolboxes\\Data Management Tools.tbx")


# Get the active map document 
import arcpy.mapping 
mxd = arcpy.mapping.MapDocument ("CURRENT") 

# List broken links 
# arcpy.mapping.ListBrokenDataSources(mxd) 

# Set environment settings 
ws = "C:\\PARK" 
arcpy.env.workspace = ws 
print("env.workspace completed successfully") 
installdir = arcpy.GetInstallInfo("desktop") 

#check GDB exist 
gdb_nam = "test.gdb" 
gdb_full_path = os.path.join(ws,gdb_nam) 
if os.path.exists(gdb_full_path): 
    arcpy.Delete_management(gdb_full_path) 
print("GDB checked") 

# Execute CreateFileGDB 
arcpy.CreateFileGDB_management(ws, gdb_nam) 
print("CreateFileGDB completed successfully") 
outWorkspace = gdb_full_path 

# Create a list of SHP in mxd 
list = [] 

for df in arcpy.mapping.ListDataFrames (mxd): 
    for fc in arcpy.mapping.ListLayers (mxd, "", df):
        if (fc.supports("DATASOURCE")) and (fc.dataSource.endswith(".shp")):
                    list.append(fc)

# Check for borken datasource
for fc in list:
    if arcpy.mapping.ListBrokenDataSources(fc):
        list.remove(fc)

# Check for projection



# Move fc to gdb
for fc in list:
    rename (fc,'_'+fc.name)
    arcpy.FeatureClassToGeodatabase_conversion(fc, gdb_full_path)
share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

The first thing that jumps out is: the rename command should be:

arcpy.Rename_management(in_data, out_data, data_type)

share|improve this answer
    
Using this command would be an unnecessary step. Instead I would use FeatureClassToFeatureClass which lets you specify the desired output feature class name. –  blah238 Jul 6 '12 at 21:27
    
that is what I did and it works, point is it creates new layers and add them to the current mxd, why? –  Luca Moiana Aug 14 '12 at 8:30
add comment

This may help to find the created date: How to get file creation & modification date/times in Python?

You'll probably want to read up on the os.path and datetime modules and string formatting in Python (new or old styles).

Here is a small example to hopefully get you going:

import os, datetime

fc = r"C:\GISData\atlantic_hurricanes_2000.shp"
ctime = datetime.datetime.fromtimestamp(os.path.getctime(fc))
ymd = ctime.strftime("%Y%m%d")
inputFcName = os.path.splitext(os.path.basename(fc))[0]
outputFcName = "%s_%s" % (ymd, inputFcName)

print outputFcName # prints 20120315_atlantic_hurricanes_2000
share|improve this answer
    
Thanks, I'll try that asap. But will converto to gdb let me use numbers as first in the name? –  Luca Moiana Jul 6 '12 at 9:47
    
I think not actually. Adjust as needed. –  blah238 Jul 6 '12 at 9:52
    
Hi there, the solution you suggested ain't working cause with list I created a list of object that can be renamed using rename. Any Idea? –  Luca Moiana Aug 13 '12 at 12:59
    
Please update your question with the new information and code. Also "ain't working" doesn't explain much. –  blah238 Aug 13 '12 at 16:56
add comment

I always do something like:

from time import strftime
outFile = "myfeatureClassName" + "_" + strftime("%d%b%y")

and then just use the 'outFile' variable for your output parameter on whichever geoprocessing tool you are using. The syntax inside brackets of the strftime function can changes the date format. Here's a decent look at the different types:

http://linux.die.net/man/3/strftime

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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