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
Solved

VB Script to rename files then copy files to another folder

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

861 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