Link to home
Start Free TrialLog in
Avatar of mostym
mostymFlag for United States of America

asked on

VB Script to rename files then copy files to another folder

Hi,

I have a folder with a bunch of randomly generated files.

28282.SRP
RNE.000078.RNE
RNE.000073.RNE
RNE.000073.RNE
232191.CLB

I need a script that will rename all .RNE files to .RNE.MTR and copy them to another folder, overwriting them if they exist in the destination folder.

Thanks!
Avatar of David Lee
David Lee
Flag of United States of America image

Hi, mostym

This should do it.
Dim objFSO, objSrcFolder, objDstFolder, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Change the folder path on the following two lines'
Set objSrcFolder = objFSO.GetFolder("C:\My Source Folder Path")
Set objDstFolder = objFSO.GetFolder("C:\My Destination Folder Path")
For Each objFile In objSrcFolder.Files
    If objFSO.GetExtensionName(objFile.Name) = "RNE" Then
        objFSO.CopyFile objFile.path, objDstFolder.Path & "\" & objFile.name & ".MTR", True
    End If
Next
Set objFSO = Nothing
Set objSrcFolder = Nothing
Set objDstFolder = Nothing
Set objFile = Nothing
WScript.Echo "Done!"

Open in new window

Avatar of mostym

ASKER

BlueDevilFan,

Can we rename the files first, then copy them to the desination folder?

Thanks!  It's looking great.
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mostym

ASKER

Perfect!  Thanks.
You're welcome.  Glad I could help.