×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Rename some files present in the folder with other filename that present in other folder in VBscript

Rename some files present in the folder with other filename that present in other folder in VBscript

Rename some files present in the folder with other filename that present in other folder in VBscript

(OP)
I have a folder 1 where there are two CSV files present with name "Head.csv" & "Col.csv". I want to rename all those CSV files that are present in folder 1. The suffix that I wanted to add to each CSV file is another filename that exists in the folder2.

Filename 1 = Actual CSV File that I want to rename Filename 2 = wanted to add this filename as Suffix. This file is present in the other folder.

Output of Filename: Filename1 + _ + FileName2 + .csv

Take for example in folder 1 "Head.csv" & "Col.csv" exist while in folder 2, the file exist with name general.txt. The filename in folder 2 can be any name.

Ex:- Head_general.csv

CODE --> vbscript

Option Explicit

Dim ofso, ofolder1,ofolder2,objFile, folderName1,folderName2 
Dim File,sNewFile,a

folderName1 = "C:\Users\ShantanuGupta\Desktop\DRUM\Folder1"  ' .csv file
folderName2 = "C:\Users\ShantanuGupta\Desktop\DRUM\Folder2" ' .txt file with different filename

Set ofso = CreateObject("Scripting.FileSystemObject")  
Set ofolder1 = ofso.GetFolder(folderName1)
Set ofolder2 = ofso.GetFolder(folderName2)
Set objFile  = oFolder2.Files 
filesuffix = ofso.GetBaseName(oFolder2.Files)

For Each File In oFolder1.Files
     sNewFile = File.Name       
     If instr(sNewfile, "Head.csv") > 0 THEN
        File.Name = Replace(File.Name, "Head.csv", "Head_" & filesuffix & ".csv")       
     End If
     If instr(sNewfile, "Col.csv") > 0 THEN
        File.Name = Replace(File.Name, "Col.csv", "Col_" & filesuffix & ".csv")
     End If
 Next 
Error Coming with Type Mismatch 'GetBaseName'.

Any help???

Files attached Here: https://drive.google.com/drive/folders/1jogr2malt5...

RE: Rename some files present in the folder with other filename that present in other folder in VBscript

Hi stg26,

IMO the error is following:
oFolder2.Files gives you a collection of all the files found in the given folder.
You cannot apply the function GetBaseName() on the whole collection.
You have to select only one file from the collection and then apply the function GetBaseName() on that one file.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close