I can copy a shapefile but I want to be able to overwrite the copied shapefile to prevent multiple copies being created each time I run the program. I would also like for the saving and renaming of the copied shapefile to happen behind the scenes i.e. the user doesnt see a gxDialog box.

How do I overwrite the copied file, either as a shapefile or feature class?

  • a good test of any method would be to see how it handles the presence of a .shx file (or .prj or .dbf) when there is no .shp there. I haven't looked lately, but found the most reliable way is to just do a filesystem delete of shpfile.*. Commented May 16, 2012 at 16:26

2 Answers 2


Before making the copy of the feature class, you can open the containing workspace and check the existance of the feature class (IWorkspace2.NameExists), and if exists then delete it (IDataset.Delete).


Dim pWorkspace2 as IWorkspace2
set pWorkspace2 = yourWorkspace

' Checks the existance of the feature class
If pWorkspace2.NameExists(yourDestFeatureClass) Then

   Dim pFeatureWorkspace as IFeatureWorkspace
   set pFeatureWorkspace = yourWorkspace

   ' Open the feature class and cast it to the IDataset interface
   Dim pDataset as IDataset
   set pDataset = pFeatureWorkspace.OpenFeatureClass(yourDestFeatureClass)

   ' Removes the feature class

End if

' Your code to make the copy...
  • Thanks! That code works great for deleting the feature class. However I am having trouble copying a feature class, it seems that the IDataset.Copy won't work for copying a feature dataset, is there easy code to get around this?
    – Sarah
    Commented May 14, 2012 at 17:26
  • Try with IGeoDBDataTransfer. Here is an example:edndoc.esri.com/arcobjects/9.2/ComponentHelp/esrigeodatabase/… Commented May 14, 2012 at 17:47
  • It doesn't work with ArcObjects for Java... If you cast a IFeatureClass to a IDataset like that: IDataset dataset = (IDataset)featureWorkspace.openFeatureClass(fc); then you get a ClassCastException.
    – superrache
    Commented Jan 29, 2015 at 16:35

This code was much easier to use to copy a file geodatabase feature class. Can't find the source but it was obtained through a google search.

Sub CopyFC(pOldWorkspace As IWorkspace, FCname As String, copyWorkSpace As IWorkspace)
    Dim gp As Object
    Set gp = CreateObject("esriGeoprocessing.GPDispatch")
    Dim pEnumDataset As IEnumDataset
    Dim pDataset As IDataset
    Dim pFeatClass As IFeatureClass
    Dim pEnumFeatClass As IEnumFeatureClass
    Set pEnumDataset = pOldWorkspace.Datasets(esriDTFeatureClass)
    Set pDataset = pEnumDataset.Next
    While Not pDataset Is Nothing
    If pDataset.BrowseName = FCname Then
        'get FC from pdataset
        Dim inFC As IFeatureClass
        Set inFC = pDataset

        gp.FeatureClassToFeatureClass_conversion inFC, copyWorkSpace, pDataset.BrowseName
    End If
    Set pDataset = pEnumDataset.Next
End Sub

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.