ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Catching duplicate filenames when renaming a file (https://www.excelbanter.com/excel-programming/323652-catching-duplicate-filenames-when-renaming-file.html)

General

Catching duplicate filenames when renaming a file
 
Hi,

In my code I have to rename some files, using the command:

Name oldfilename as newfilename

Sometimes the newfilename already exists so I am looking for a way to
detect that condition, I tried:

If iserror(Name oldfilename as newfilename) then
CODE HERE
else
name oldfilename as newfilename
end if

However, Excel "Expects list seperator or )" around the oldfilename in
the iserror statement. Help!

Also, for this code I am in the middle of an application filesearch so
I can't search for the file using application.filesearch.

Thank you!!!

Phil


Robin Hammond[_2_]

Catching duplicate filenames when renaming a file
 
Phil,

How about

If Dir(newfilename)<"" then
'the file exists
else
'save the file
end if

Robin Hammond
www.enhanceddatasystems.com

"General" wrote in message
ups.com...
Hi,

In my code I have to rename some files, using the command:

Name oldfilename as newfilename

Sometimes the newfilename already exists so I am looking for a way to
detect that condition, I tried:

If iserror(Name oldfilename as newfilename) then
CODE HERE
else
name oldfilename as newfilename
end if

However, Excel "Expects list seperator or )" around the oldfilename in
the iserror statement. Help!

Also, for this code I am in the middle of an application filesearch so
I can't search for the file using application.filesearch.

Thank you!!!

Phil




Bob Phillips[_6_]

Catching duplicate filenames when renaming a file
 
Another way

On Error Resume Next
Name oldfilename As newfilename
If Err.Number = 58 Then 'already exists
MsgBox "file " & newfilename & " already exists"
End If
On Error GoTo 0


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Robin Hammond" wrote in message
...
Phil,

How about

If Dir(newfilename)<"" then
'the file exists
else
'save the file
end if

Robin Hammond
www.enhanceddatasystems.com

"General" wrote in message
ups.com...
Hi,

In my code I have to rename some files, using the command:

Name oldfilename as newfilename

Sometimes the newfilename already exists so I am looking for a way to
detect that condition, I tried:

If iserror(Name oldfilename as newfilename) then
CODE HERE
else
name oldfilename as newfilename
end if

However, Excel "Expects list seperator or )" around the oldfilename in
the iserror statement. Help!

Also, for this code I am in the middle of an application filesearch so
I can't search for the file using application.filesearch.

Thank you!!!

Phil






General

Catching duplicate filenames when renaming a file
 
Thanks, that solves it for me!



All times are GMT +1. The time now is 11:11 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com