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
Error Coming with Type Mismatch 'GetBaseName'.
Any help???
Files attached Here:
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
Any help???
Files attached Here: