Shapefile name to Field Name

712
7
Jump to solution
08-04-2020 09:09 AM
JamesWhite5
New Contributor III

Good afternoon.  I am trying to figure out how to write a script in Python with extremely little experience.  I have been pouring through forums here and at Stack Exchange, but everything seems to be a little out of date.

I have ~200 shapefiles that I merged into a single featureclass within a geodatabase. I am using a schema that has the field "NAME" in it. I am trying to take the name of the individual shapefiles (x27y407.shp for example) and write that name, minus the .shp, into the NAME field.

Most of the code I am finding to try and work with seems like it is using python 9 or earlier, so am having a hard time trying to piece it together.

Any help would be greatly appreciated.

0 Kudos
1 Solution

Accepted Solutions
JoeBorgione
MVP Esteemed Contributor

You'll want to be sure to update name field before you merge.  Once you get going on this, post up any questions you have as you go along...

That should just about do it....

View solution in original post

0 Kudos
7 Replies
JoeBorgione
MVP Esteemed Contributor

You've already done the merge?  Not sure how you can recoup the origin name after the fact.

Are you using Python and Arcpy? If so you could try:

Create a list of all the shapefiles in a given directory: Python os.listdir() Method - Tutorialspoint 

Step through that list and for each shapeFile.shp, 'slice' the name out of it: Python String split() Method - Tutorialspoint 

Join the name back to a string: Python String join() Method - Tutorialspoint 

And finally use arcpy to calculate the value of the field in question to that string: Calculate Field (Data Management)—ArcGIS Pro | Documentation 

That should just about do it....
JamesWhite5
New Contributor III

Thank you. I will look into that.

And yes, I think I did the merge too quick.  I think I need to create the NAME field, then merge, and then I can just use the field calculator to populate the NAME field I really need populated.

0 Kudos
JoeBorgione
MVP Esteemed Contributor

You'll want to be sure to update name field before you merge.  Once you get going on this, post up any questions you have as you go along...

That should just about do it....
0 Kudos
JamesWhite5
New Contributor III

Joe,

Thank you for the replies on this.  I did find it way easier to just add the field FILE_NAME to the shapefile attributes, then merge the schema I needed with the shapefile to create a new .gdb file, then just used field calculator to get rid of NULLs and move the file names over.

Thank you again.

JoshuaBixby
MVP Esteemed Contributor

It appears you have resolved your issue. If so, please mark an answer correct to close out this question. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

James White‌, I unmarked your answer and gave the correct answer to Joe, since it was his comments that sent you down the right path.  Typically, the "Correct" answer is the one that gave you the knowledge to solve the issue, that might be code itself or higher-level information like, "update name field before you merge."  It is helpful for the OP to post their final solution/workflow, but that final reply from the OP typically isn't the "Correct" answer unless there was some substantially new or different information in it.

JamesWhite5
New Contributor III

Joshua,

Ok, I will remember that.  I didn't realize how that worked, thank you.

0 Kudos