Runtime Error 58: “File already exists,” explained and fixed
Error 58, “File already exists”, fires when a statement that requires a fresh name is handed one that is taken, most often the Name statement renaming a file onto an existing file, or code that creates a file only when it expects none to be there. It is the file-system equivalent of a duplicate key: the operation is fine, the destination is occupied. Whether that is a bug depends on intent: sometimes an existing file means “already done,” sometimes it means the output naming scheme collided.
For people who just run the program
- Look in the destination folder. Nine times out of ten the named file is simply sitting there from a previous run, an earlier export, or a colleague's identical job.
- Check whether the existing file is the finished output. If the job already ran successfully once, the error may be the program's clumsy way of saying so; confirm the file's date and contents before rerunning anything.
- Move or rename the existing file if a fresh run is truly needed. Keep it rather than deleting it until the new run succeeds, so a failed rerun leaves you with the old output, not none.
- Watch for date-based names near midnight or month end. Batches that name files by date collide when run twice in one period, and clear themselves when the period rolls over.
For people who write the code
- Decide the policy before the statement: overwrite, skip, or uniquify. Then implement it explicitly instead of discovering the collision at run time:
If Dir$(dest) <> "" Then Kill dest ' policy: overwrite
Name tempPath As dest ' now the name is free
- Make output names unique by construction. Include a timestamp to the second, a record identifier, or a sequence number in generated names so two runs cannot collide even in the same folder.
- Do not use Dir() checks alone in shared folders. Between the check and the create, another process can create the same file. Where that matters, handle error 58 from the create itself as the real test.
Error 58 from Name is not permission to delete blindly. In rename-based workflows the existing destination might be the only good copy. Code that auto-deletes the destination before checking what it is turns a recoverable collision into data loss.
If it still happens
If the folder listing shows no such file, check for hidden and system files and for a name that differs only by trailing spaces or a trailing dot: Windows trims those, so two names that look different to the code are the same name to the file system.
Common questions
Why does Open For Output not raise error 58? Because Output mode is defined to overwrite an existing file. Error 58 comes from operations that require the destination to not exist, such as Name, or code that checks and raises it deliberately.
Is error 58 a sign of a duplicate run? Often. Jobs that name output by date or by record create the same name when run twice. That is why the first diagnostic step is looking at the existing file's timestamp and contents.
Related errors