Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

stg26

Programmer
Dec 5, 2021
1
0
0
AU
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:
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:
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top