helenak

batch add field among multiple shp in ArcCatalog

Discussion created by helenak on Jun 4, 2010
Dear,

There are about 200 shape files, which I want to add the same field in the attribute table called GID1.

I want to implement it in ArcCatalog and prepared a code below.
However, it is for one shape file each time.
Please kindly help to modify it for multiple shape files (within the same folder) in a run.
Thanks.


Code

Public Sub AddField()

' Part 1: Define the workspace
Dim pApp As esriCatalogUI.IGxApplication
  Set pApp = esriArcCatalog.Application

  Dim pGxSelection As esriCatalog.IGxSelection
  Set pGxSelection = pApp.Selection

  Dim plist As esriCatalog.IEnumGxObject
  Set plist = pGxSelection.SelectedObjects
 
  Dim pGxObject As esriCatalog.IGxObject
  Dim pName As esriSystem.IName
  Dim pDS As esriGeoDatabase.IDataset
  Dim pFE As esriGeoDatabase.IFieldEdit
  Dim pGDSE As esriGeoDatabase.IGeoDatasetSchemaEdit
  Dim pStatusBar As esriSystem.IStatusBar
  Set pStatusBar = esriArcCatalog.Application.StatusBar
  Dim pTable As ITable
  Dim pFeatureLayer As esriCarto.IFeatureLayer
  Dim pFeatureClass As esriGeoDatabase.IFeatureClass
  Dim pGxDataset As esriCatalog.IGxDataset
 



' Part 2: Delete the field
      
         ' Define the first new field.
           Dim pFields As IFields
           Dim ii As Integer
           Dim pField3 As IField
      
   
        ' Define the second new field.
             Dim pField2 As IFieldEdit
             Set pField2 = New Field
            pField2.Name = "GID2"
            pField2.Type = esriFieldTypeString
            pField2.Length = 6
           
           
          
  Set pGxObject = plist.Next
 
  If TypeOf pGxObject Is IGxDataset Then

         Set pGxDataset = pGxObject
         Set pFeatureLayer = New FeatureLayer
         Set pFeatureLayer.FeatureClass = pGxDataset.Dataset
         Set pFeatureClass = pFeatureLayer.FeatureClass
        
         Set pFields = pFeatureClass.Fields
         ii = pFields.FindField("GID1")
         Set pField3 = pFields.Field(ii)
   
  End If
 
        
    If pGxObject Is Nothing Then
    MsgBox "You need to select the files", vbOKOnly, "Error"
    Else


           
            ' Add the new field
             pFeatureClass.AddField pField2

           

        MsgBox "fields added.", vbOKOnly + vbInformation, "Add GID1"
  End If



Exit Sub
ErrorHandler:
   MsgBox "AddGID1: " & Err.Number & "-" & Err.Description, vbCritical, "Add GID1"
End Sub

Outcomes