Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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. Any suggestions on how to overwrite the copied file, either as a shapefile or feature class would be greatly appreciated.

share|improve this question
    
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.*. –  Kirk Kuykendall May 16 '12 at 16:26
add comment

2 Answers

up vote 1 down vote accepted

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).

Example:

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
   pDataset.Delete

End if

' Your code to make the copy...
share|improve this answer
    
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 May 14 '12 at 17:26
    
Try with IGeoDBDataTransfer. Here is an example:edndoc.esri.com/arcobjects/9.2/ComponentHelp/esrigeodatabase/… –  cag May 14 '12 at 17:47
add comment

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
 Wend
End Sub
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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