How can i script for intersecting the two shape files from two different folders based on their names?

i have two folders which contains the shape files with same name i want to intersect the shapefiles based on their name, scripting should pick the files based on their name from two different folders and output file should store in another folder.

I'm using ArcGIS Desktop 9.3 and arcgisscripting (that preceded ArcPy).


1 Answer 1


From my collection of ArcGis 9.3 python scripts:

import arcgisscripting
gp = arcgisscripting.create(9.3)

Shape_A = "c:\\some\\path\\to\\shapefile.shp"
Shape_B = "c:\\some\\other\\path\\other_shapefile.shp"
Intersection = "c:\\output\\path\\output.shp"

gp.Intersect_analysis(Shape_A + ";" + Shape_B,Intersection,"ALL","#","INPUT")

You will need to change the paths to match your data - remember python uses the escape backslash \\ for a backslash or you can use the unix/linux forward slash in single eg. "c:/some/path/to/shapefile.shp".

Using a version 9.3 geoprocessor gp you append the feature class names (full path) with a semi-colon in between see Intersect_analysis - also for the explanation of the parameters "ALL","#","INPUT".

If you want to do a folder full of feature classes matching up to another folder you can do it like this:

import sys,arcgisscripting
gp = arcgisscripting.create(9.3)

InFolder = sys.argv[1]
MatchFolder = sys.argv[2]
OutFolder = sys.argv[3]

gp.workspace = InFolder # set the workspace
FeatureClasses = gp.ListFeatureClasses() # get a list of feature classes

# step through the feature classes
for FC in FeatureClasses:
    MatchFC = MatchFolder + "\\" + FC
    # if the same name is in the match folder
    if gp.Exists(MatchFC):
        InterFC = OutFolder + "\\" + FC
        # perform the intersection
        gp.Intersect_analysis( FC + ";" + MatchFC,InterFC,"ALL","#","INPUT")

The sys.argv match up with the tool parameters (read Adding a Script Tool)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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