Solved

VB Script to rename files then copy files to another folder

Posted on 2008-10-14
5
713 Views
Last Modified: 2012-08-14
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!
0
Comment
Question by:mostym
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 22713780
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

0
 

Author Comment

by:mostym
ID: 22714112
BlueDevilFan,

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

Thanks!  It's looking great.
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 22714732
Changing the name first cause a problem with the loop.  How about copy the file and then changing the name?  The result is the same, it just reverses the order of things.  The modified code below does this.
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

        objFile.Name = objFile.Name & ".MTR"

    End If

Next

Set objFSO = Nothing

Set objSrcFolder = Nothing

Set objDstFolder = Nothing

Set objFile = Nothing

WScript.Echo "Done!"

Open in new window

0
 

Author Closing Comment

by:mostym
ID: 31506046
Perfect!  Thanks.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 22715267
You're welcome.  Glad I could help.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now