[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 723
  • Last Modified:

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!
0
mostym
Asked:
mostym
  • 3
  • 2
1 Solution
 
David LeeCommented:
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
 
mostymAuthor Commented:
BlueDevilFan,

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

Thanks!  It's looking great.
0
 
David LeeCommented:
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
 
mostymAuthor Commented:
Perfect!  Thanks.
0
 
David LeeCommented:
You're welcome.  Glad I could help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now