1

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

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

  • 2
    [oFolder2.Files](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/18b41306(v=vs.84)) returns a collection, you need to iterate the collection to get each file, even if there is only one file. – Flakes Dec 05 '21 at 10:03
  • @flakes if I iterate then how I can add that suffix to the file. – user17538713 Dec 05 '21 at 10:13
  • Even if you iterate, there is only one file, so it wont get overwritten. `For each objFile in oFolder2.Files : filesuffix = ofso.GetBaseName(objFile) : Next` – Flakes Dec 05 '21 at 10:20
  • You just need to replace the `filesuffix = ofso.GetBaseName(oFolder2.Files)` line with the above. – Flakes Dec 05 '21 at 10:28

1 Answers1

0

This might work, although I'm not entirely sure how many files will be in 'folder2' but you get the idea:

Option Explicit

Dim objFile, sNewFile, filesuffix

Dim folderName1 : folderName1 = "C:\Users\ShantanuGupta\Desktop\DRUM\Folder1"
Dim folderName2 : folderName2 = "C:\Users\ShantanuGupta\Desktop\DRUM\Folder2"

Dim ofso : Set ofso = CreateObject("Scripting.FileSystemObject")  
Dim ofolder1 : Set ofolder1 = ofso.GetFolder(folderName1)
Dim ofolder2 : Set ofolder2 = ofso.GetFolder(folderName2)

For Each objFile in ofolder2.Files
    filesuffix = ofso.GetBaseName(objFile)
Next

For Each objFile In oFolder1.Files
     sNewFile = ofso.GetBaseName(objFile.Name)   
     
     If StrComp(sNewfile,"Head",1) = 0 THEN
        objFile.Name = Replace(sNewFile, sNewFile, sNewFile & "_" & filesuffix & ".csv")        
     End If
     If StrComp(sNewfile,"Col",1) = 0 THEN
        objFile.Name = Replace(sNewFile, sNewFile, sNewFile & "_" & filesuffix & ".csv")
     End If
Next
 
Set ofolder1 = Nothing
Set ofolder2 = Nothing
Set ofso = Nothing
Captain_Planet
  • 1,228
  • 1
  • 12
  • 28