• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

How to rename a file even if a file by the same name already exists using Access 2003?

I am developing an Access 2003 application..

How would you rewrite the following code to rename a file even if a file with the same name (AGCS_NyCashBreaksBod.txt) already exists:

MyPath = CurrentProject.Path

filedat = "\AGCS_NyCashBreaksBod.dat"

newfiledat = "\AGCS_NyCashBreaksBod.txt"

If Dir(MyPath & filedat) <> "" Then
   Name MyPath & filedat As MyPath & newfiledat
End If
0
zimmer9
Asked:
zimmer9
1 Solution
 
mvidasCommented:
Change the last block to check for the file's existence, and if it does exist you can delete it
If Dir(MyPath & filedat) <> "" Then
   If Dir(MyPath & newfiledat) <> "" Then Kill MyPath & newfiledat
   Name MyPath & filedat As MyPath & newfiledat
End If

Open in new window

0
 
Rey Obrero (Capricorn1)Commented:
try


If Dir(MyPath & filedat) <> "" Then
   if dir(MyPath & newfiledat) <>"" then
      kill MyPath & newfiledat  'delete the file if exists
      end if
   Name MyPath & filedat As MyPath & newfiledat
End If
0
 
mbizupCommented:
Try this - you'll need a reference to the MS Scripting runtime library.
Dim fso As New FileSystemObject

MyPath = CurrentProject.Path

filedat = "\AGCS_NyCashBreaksBod.dat"

newfiledat = "\AGCS_NyCashBreaksBod.txt"

fso.CopyFile MyPath & filedat , MyPath & filedat , True

Open in new window

0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
MyPath = CurrentProject.Path

filedat = "\AGCS_NyCashBreaksBod.dat"

newfiledat = "\AGCS_NyCashBreaksBod.txt"

If Dir(MyPath & filedat) <> "" Then
   On Error Resume Next
   Kill MyPath & newfiledat
   On Error Goto 0
   Name MyPath & filedat As MyPath & newfiledat
End If

Jim.
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
Just an FYI, you'll find that the code I posted is a tad faster because you only do the directory search once rather then twice.

If the file is local, it's not a big difference, but if it's on a network share, the difference is noticeable.

Jim.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now