Solved

VB Script to rename files then copy files to another folder

Posted on 2008-10-14
5
715 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

24 Experts available now in Live!

Get 1:1 Help Now