English

Error: Error calling unlink for file....Returned Error Code 13

Error Message

Deleting files from within an Avenue script yields this error:

Error calling unlink for file....
Returned Error Code 13

Cause

ArcView still has references to the file that is being deleted from the hard-drive.

Solution or Workaround

Before you delete any file on the hard drive using Avenue code, you must
eliminate all objects that reference the file.

Review and run the script below to understand what your code needs to do in order to successfully delete files.

Code:
'-- Script File.Delete error message:
'
' Error Calling Unlink for File.... |
' Returned Error Code 13 |
'
'-- This Script will:
' 1. Load a shape file into View1. Any simple shape file will do.
' (View 1 will be made if necessary.)
' 2. It then loads the theme and opens the attribute table.
' 3. From here it closes and removes all necessary objects
' that were created.
' 4. Garbage collection is done to remove the objects marked
' for deletion.
' 5. Now the files on the hard drive are removed.
'

'-- Set Variables Here
'
'-- To reproduce the "Error calling unlink..."
'-- set this to true to see the error message
ShowProblem = False

'-- Source for demo (Must be a shape file!)
source = "c:\delete\theme1.shp"

'-- Enter the theme name below minus the .shp extension
'-- used to delete all shape files later...
TheShortThemeName = "theme1"

'-- End Set Variables

'-- Get the view or make it if needed
theView = av.GetProject.FindDoc("View1")
if (theView = nil) then
theView = theView.Make
end

'-- Add a theme from a source data set and add it to the view
theSourceName = SrcName.Make(source)
if (theSourceName = nil) then
MsgBox.Info("The source data set was not found","Error")
exit
end

'-- Make and add a theme
theTheme = Theme.Make(theSourceName)
theView.AddTheme(theTheme)

'-- Get the ftab
theFTab = theTheme.GetFTab

'-- Debug code
av.ShowMsg("Theme is - " ++ theTheme.AsString)

'-- Open the attribute table to demonstrate removing
'-- opened attribute tables (shown below)
theTheme.EditTable

'-- Here is the Cleanup code...
'-- Remove all object references to the source data
if (ShowProblem = false) then

'-- Remove the Attribute table
theTable = av.GetProject.FindDoc("Attributes of" ++ theTheme.GetName)
'MsgBox.Info("Found" ++ theTable.AsString,"")

if (theTable = nil) then
msgbox.info("The Attribute Table was not found!","Error")
exit
end

av.GetProject.RemoveDoc(theTable) '-- Remove the table
theTable = nil '-- Set the table to nil

theView.DeleteTheme(theTheme) '-- Remove the theme
theTheme = nil '-- Set the theme to nil

theFTab.DeActivate '-- Deactivate the Ftab
theFTab = nil '-- Set the Ftab to nil

'-- Run garbage collection to throw away objects
'-- Marked for deletion
av.PurgeObjects

end '-- End of show problem

'-- Delete the files

'-- Strip the location from the source name
theLocation = source.AsFileName '-- Make a file name so that I
'-- Can use the StripFile operation
theLocation.StripFile '-- Get the location from the filename
theLocationName = theLocation.Getname '-- Get the name from above
'-- Debug code
'Msgbox.info(theLocation.GetName,source.asstring)

aFile1 = (theLocationName + "\" + theShortThemeName + ".shp").AsFileName
aFile2 = (theLocationName + "\" + theShortThemeName + ".dbf").AsFileName
aFile3 = (theLocationName + "\" + theShortThemeName + ".shx").AsFileName
File.Delete(aFile1)
File.Delete(aFile2)
File.Delete(aFile3)

MsgBox.Info("Deleted the Files!","Great!")

'-- End of Script