Rename a file name

T

Tom Knox

I need to rename a file to a specific name. This is a file that is created
daily which has the date in it's file name. The only part of the file name
that is consistent is the extension (ch). Is there a way to rename the file
based only on the extension name?

Old file name: A060725.CH
Desired new file name: CH.TXT

Using the "Name" command requires me to have the exact name of the file
(A060725.CH). I can rename the file manually, but I'm looking for a way to
do it in a macro.

Any help would be appreciated.

Thanks

Tom Knox
 
S

strive4peace

Hi Tom,

if you know where the file is, you can write VBA code to go get the
first file whose extension is CH and change the name to CH.TXT

Do you have the path of the file?

Do you want VBA code? Since you mentioned macro, I didn't want to scare
you ;)

Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote programming and Training
strive4peace2006 at yahoo.com
*
 
T

Tom Knox

Crystal

Yes I have the path to the file and yes, if you have VBA code that will work
, I would love to see an example of it.
When I mentioned macro, I have a series of queries and VBA modules within a
macro to accomplish the task.

Thanks again for your help

Tom
 
S

strive4peace

Hi Tom,

here is some VBA code ... do you know what to do with it?

~~~~~~~~~~~~~~
Sub test()
Dim mFile As String, mPath As String

mPath = "c:\yourpath\"

mFile = Dir(mPath & "*.CH")
If mFile <> "" Then
Name mPath & mFile As mPath & "ch.txt"
MsgBox "File has been renamed", , "Done"
Else
MsgBox "Could not rename file", , "Done"
End If

End Sub
~~~~~~~~~~~~~~

this will just get the first file in the directory with a CH extension...

Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote programming and Training
strive4peace2006 at yahoo.com
*
 
T

Tom Knox

Hi Crystal

That worked great!! It is amazing how simple it is when you know what you're
doing.

Thanks again for your help

Tom
 
S

strive4peace

you're welcome, Tom ;) happy to help

Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote programming and Training
strive4peace2006 at yahoo.com
*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top