Select to view content in your preferred language

Is it possible to do a geoprocess automatically with all the layers of a folder?

1900
19
07-23-2018 03:50 AM
Jose_AlbertoNúñez
Emerging Contributor

Hi

I have 200 shapefile in a folder. all fo them are similar. I need select by atribute and them aply eliminate tool and save the shapes files. 

Is it possible to do automatically with all the layers from model builder or arcpy? How can I do?

thnak you very much

Tags (1)
0 Kudos
19 Replies
DanPatterson_Retired
MVP Emeritus

so before the select by attributes you have to squeeze in a line

arcpy.MakeFeatureLayer_management(fc,"fc_lyr")

then change 'fc' to 'fc_layer' in the selectbyattributes line (going blindly again)

Jose_AlbertoNúñez
Emerging Contributor

Hi @Dan Patterson

I think understand you:

import arcpy

from arcpy import env
env.overwriteOutput = True
import os

arcpy.env.workspace = r"E:\PLANFOR\PD_G000053VA_N\Carto\A_Monre\Tipos_masa\Prueba_subrodal\Rodales_tipmas.gdb"
fcs = arcpy.ListFeatureClasses()
out_folder = r"E:\PLANFOR\PD_G000053VA_N\Carto\A_Monre\Tipos_masa\Prueba_subrodal\Salida"
for fc in fcs:

arcpy.MakeFeatureLayer_management(fc,"fc_lyr")

arcpy.SelectLayerByAttribute_management(fc_lyr, "NEW_SELECTION", "\"Superfic_1\" <= 3")

out_name = "{}\\{}_elim.shp".format(out_folder, fc_lyr)

arcpy.Eliminate_management(fc_lyr, out_name, "LENGTH", "", "")

But I have a error. I do not find it. Do you see it? Thank you very much

0 Kudos
NeilAyres
MVP Alum

In python code blocks like this loop are marked by indentation of the code, see Dan's original code snippet.

NeilAyres
MVP Alum

So those 4 lines following "for fc in fcs:"

should be indented, usually 4 spaces

Jose_AlbertoNúñez
Emerging Contributor

@Neil Ayres @Dan Patterson

I use spaces, I did not that are so important in python, sorry. I think that know is correct but i have a new error,

0 Kudos
DanPatterson_Retired
MVP Emeritus

"fc_lyr"  in quotes do the same for it on the last line

Jose_AlbertoNúñez
Emerging Contributor

Hi @Dan Patterson.

I think that the script does not generate errors. I think it performs the same process with all the layers (190). The problem is that since the output file has a unique name "fc_lyr", all the files are rewritten and at the end there is a single file instead of 190. How could I do it to keep the original name files + a sequence so that it does not overwrite?

Thank you very much

0 Kudos
DanPatterson_Retired
MVP Emeritus

in the out_name  line...the... fc_layer ...there should be... fc   

we got carried away replacing fc with fc_layer

Jose_AlbertoNúñez
Emerging Contributor

Excellent !! I think it works perfectly
Thank you very much to all the people who have helped me, especially @Dan Patterson.
This is a problem that I had in my workflow and that saved me a lot of work. So far I was doing the process manually.
Thank you!!

0 Kudos
DanPatterson_Retired
MVP Emeritus

Glad it worked out Jose

0 Kudos