Use Calculate field to return File Path

858
1
Jump to solution
05-26-2022 05:47 PM
Labels (1)
AmandaLaur3
New Contributor

I have a Folder of 1000+ of 1 feature shapefiles. Ultimatley looking to Append them together but retain the name of the shapefile as a field.

In ARCADE or Python within field Calculator; looking for a way to return the filepath, ultimatley to get the name of the Shapefile. (via the file path)

Not sure if it makes a Difference whether its a featureclass or shapefile.

 I know there is some type of modelbuilder tool that can Parse shapefile, but i would like to do this just in field Calculator as well for single type instances. the Shapefile names are typically too long and complicated to enter in..

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

You won't be able to do it from inside Calculate Field. In Arcade there's no way to get the shape file's path, maaaybe you could do it with Python (I doubt it), but that would be complicated.

This is a job for a simple Python script. Edit the script and execute it in the Python Window, in a Notebook, or in your IDE:

import arcpy
from pathlib import Path

# get a list of your shape files
# if they are in different sub folders, use rglob instead of glob
folder = Path(r"H:\Test")
shape_files = folder.glob("*.shp")

# go through the list and calculate the field for each shape file
for shape_file in shape_files:
    shape_file = str(shape_file) # CalculateField is old and can't handle Path objects...
    print(f"Adding field to {shape_file}")
    arcpy.management.CalculateField(
        in_table=shape_file,
        field="OrigPath", # shape file, so can only be 10 characters
        expression=f"'{shape_file}'" # this is a string argument, but we also want to return a string, so we use single quotes inside the expression
        )

 


Have a great day!
Johannes

View solution in original post

1 Reply
JohannesLindner
MVP Frequent Contributor

You won't be able to do it from inside Calculate Field. In Arcade there's no way to get the shape file's path, maaaybe you could do it with Python (I doubt it), but that would be complicated.

This is a job for a simple Python script. Edit the script and execute it in the Python Window, in a Notebook, or in your IDE:

import arcpy
from pathlib import Path

# get a list of your shape files
# if they are in different sub folders, use rglob instead of glob
folder = Path(r"H:\Test")
shape_files = folder.glob("*.shp")

# go through the list and calculate the field for each shape file
for shape_file in shape_files:
    shape_file = str(shape_file) # CalculateField is old and can't handle Path objects...
    print(f"Adding field to {shape_file}")
    arcpy.management.CalculateField(
        in_table=shape_file,
        field="OrigPath", # shape file, so can only be 10 characters
        expression=f"'{shape_file}'" # this is a string argument, but we also want to return a string, so we use single quotes inside the expression
        )

 


Have a great day!
Johannes