Rename File with partial name

giovanniroi

Registered User.
Local time
Today, 01:09
Joined
Oct 23, 2010
Messages
26
I need to make a VBA Code to rename a file using only a section of the name.
The problem is that the first part of the name change any day and I need to ignore this part and rename the file.

The second part of the filename ".0651.TXT" don't change.

I need to do:

RENAME C.\temp\*.0651.TXT , C:\temp\AUDIT.0651.TXT

How can I make this code?

Thank's for support.
Regards

Giovanni Roi
 
g,

You can use the Dir function to get the filename.

FileName = Dir("C:\temp\*.0651.TXT")

Then --> NAME FileName As "C:\temp\AUDIT.0651.TXT"

hth,
Wayne
 
g,

You can use the Dir function to get the filename.

FileName = Dir("C:\temp\*.0651.TXT")

Then --> NAME FileName As "C:\temp\AUDIT.0651.TXT"

hth,
Wayne

You could use FSO ----->

Code:
Dim objFSO
dim theFOLDER
dim theFILE
dim meFile
Set objFSO = CreateObject("Scripting.FileSystemObject")

'You should check that the file doesn't already exist as well..
if objFSO.FileExists("C:\temp\AUDIT.0651.TXT") then
  set meFile = objFSO.GetFile("C:\temp\AUDIT.0651.TXT")
  mefile.delete
end if

set thefolder = objfso.getfolder("c:\temp")
for each thefile in thefolder.files
     if UCASE(right(thefile.path,9)) = ".0651.TXT" then

 
         objFSO.MoveFile thefile.path, "C:\temp\AUDIT.0651.TXT"
     end if
next
 
Last edited:
Dear Wayne
Tank's for the solution that solve my problem.

Regards

Giovanni Roi
 
g,

In all honesty, the Dir() command is a little "old school", but it works.

The Geezer's solution is a bit more current.

Wayne
 

Users who are viewing this thread

Back
Top Bottom