Solved

VB Script to rename files then copy files to another folder

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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