• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4455
  • Last Modified:

VBScript to rename all files in a folder

I need a VBScript to rename all the files (that begin with the letters "test1") in a folder and then copy those renamed files to another folder.
0
Cad Coder
Asked:
Cad Coder
  • 3
  • 3
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok....is there a pattern to how you want these files renamed?  Or can we just rename them using random characters?....
0
 
Cad CoderDeveloperAuthor Commented:
I need to strip off the "test1" the name of the file (keep the rest of the name).  For example a file named "test1File1.doc" would be named "File1.doc" and then copied to another folder.  I need the script to find all the files that begin with "test1" and then strip off the "test1" and save to another folder.  Sorry I didnot make that clear.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
dim fso
set fso = createobject("Scripting.FilesystemObject")
dim folder
dim file

set folder = fso.GetFolder("C:\sourcefolder") 'put in the folder you want to look at
for each file in folder.files
   if file.name like "test1*" then
     fso.CopyFile "C:\sourcefolder\" & file.name , "C:\destinationfolder\" & mid(filename, 5)
   end if  
next

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Cad CoderDeveloperAuthor Commented:
angelIII,

I got an error on line 8 - sub or function not defined.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
sorry, I forgot a "." in the file.name in the mid() function call:

dim fso
set fso = createobject("Scripting.FilesystemObject")
dim folder
dim file

set folder = fso.GetFolder("C:\sourcefolder") 'put in the folder you want to look at
for each file in folder.files
   if file.name like "test1*" then
     fso.CopyFile "C:\sourcefolder\" & file.name , "C:\destinationfolder\" & mid(file.name, 5)
   end if  
next
0
 
Cad CoderDeveloperAuthor Commented:

Still get the same error after correcting the "mid(file.name, 5)".
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I see now, the problem is the LIKE which does not exists in the VB Script.
Also, the MID( ) should use ,6 and not ,5 :-)

dim fso
set fso = createobject("Scripting.FilesystemObject")
dim folder
dim file

set folder = fso.GetFolder("C:\sourcefolder") 'put in the folder you want to look at
for each file in folder.files
   if ucase(left(file.name, 5)) = "TEST1" then
     fso.CopyFile "C:\sourcefolder\" & file.name , "C:\destinationfolder\" & mid(file.name, 6)
   end if  
next
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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