Solved

VB Script to rename files then copy files to another folder

Posted on 2008-10-14
5
719 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

691 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