rename and move a file in VBa

I need to rename and then move a file in VBA.  How do i do that?
vbnetcoderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Easy: Name OldFile As NewFile.

Where OldFile and NewFile are the complete file name including the old and new path.

Obviously, it's a good idea to test whether the old file exist, that the new path exists and that there is no file of the same name.
0
vbnetcoderAuthor Commented:
you have some code?
0
ste5anSenior DeveloperCommented:
Name OldFile As NewFile is the code you need. E.g.

Name "C:\Temp\Book1.xlsx" As "C:\Temp\Test\Book123.xlsx"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

vbnetcoderAuthor Commented:
How do i check if it exists first?
0
Rey Obrero (Capricorn1)Commented:
to check if a file exists,

if dir(strFullPathAndName, vbdirectory)<> "" then
     'file exists
   else
      'file doesn't exists
end if
0
vbnetcoderAuthor Commented:
This code always always displays the file exists msgbox

Dim strFilePath1
strFilePath1 = ""
If Dir(strFilePath1, vbDirectory) <> "" Then
     MsgBox "file exsists"
     
   Else
      MsgBox "file does not exsist"
End If

Open in new window

0
Rey Obrero (Capricorn1)Commented:
that is not the way it works, you have to pass a fullpath and fileName

Dim strFilePath1
strFilePath1 = "c:\folder\myfile.csv"   ' if myfile.csv
If Dir(strFilePath1, vbDirectory) <> "" Then
     MsgBox "file exsists"
     
   Else
      MsgBox "file does not exsist"
End If
0
vbnetcoderAuthor Commented:
ty
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.