ArcMap – Batch Export Selected Features in All Layers to Shapefile

arcgis-10.1arcgis-desktopexportselectshapefile

I have several layers in an ArcMap Document. All of these layers are stored as shape files.

Specific features of the said layers are selected. I want to export the data as shapefile programatically. How can I do this using arcpy?

Sample view of my arcmap workspace

Best Answer

Try..

    ## This script will look for all the layers that have feature selected in them in the TOC and export them in seperated shapefile.
    ##output layer name will be the original name+ _selected e.g. luzon_loss_selected
    import arcpy
    arcpy.env.overwriteOutput = True
    mxd = arcpy.mapping.MapDocument("CURRENT")
    df = arcpy.mapping.ListDataFrames( mxd, "Layers")[0]
    layers = arcpy.mapping.ListLayers(mxd, "*", df)
    output_folder = r'C:\Users\YOUR_USER_NAME\Desktop\test' ##folder path of output
    for layer in layers:
        Sel=arcpy.Describe(layer)
        if Sel.FIDset: 
            arcpy.FeatureClassToFeatureClass_conversion(layer,output_folder,layer.name + "_selected","","","")