• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

copy specific file from specific folder to specific destination folder

amitssxscs

New Member
In column A I have file name
In column B I have source folder path
In column C i have destination folder path

Need a macro

To copy files of column A
From source path (columnB)
To destination path (columnC)

till last row
 
Code:
Option Explicit

Sub CopyFile()
Dim fso
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String
Dim i As Integer
Dim lastrow As Integer

ThisWorkbook.Worksheets("Sheet1").Activate

'Find The Last Row
lastrow = Range("A1000").End(xlUp).Row

'Source Folder Path
sSFolder = ThisWorkbook.Worksheets("Sheet1").Range("B2").Value

'Destination Folder Path
sDFolder = ThisWorkbook.Worksheets("Sheet1").Range("C2").Value

Set fso = CreateObject("Scripting.filesystemobject")

For i = 2 To lastrow

    If Not fso.fileexists(sSFolder & Worksheets("Sheet1").Range("A" & i).Value) Then
            MsgBox "Specfied File Not Found", vbInformation, "Not Found"

    ElseIf Not fso.fileexists(sDFolder & Worksheets("Sheet1").Range("A" & i).Value) Then
         fso.CopyFile (sSFolder & Worksheets("Sheet1").Range("A" & i).Value), sDFolder, True
        
    Else
        MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
    End If
    
Next i

End Sub



Regard
Rahul Shewale
 
THank You Rahul for help, this is showing "Specfied File Not Found"
though all files are there in the path, entries for my column A,B and C are respectively
Abracon-New-Specfications-PCN-M1110.pdf
C:\Users\kumar145\Downloads\tti
C:\Users\kumar145\Downloads\tti\A
 
Hi @amitssxscs ,

Backslash.missing in end

C:\Users\Downloads\Copy one folder to another folder\Source Folder\
C:\Users\Downloads\Copy one folder to another folder\Destination Path\

File Names
Book1.xlsx
Book2.xlsx
Book3.xlsx


Regard
Rahul Shewale
 
It worked but
all file are in same folder so source path is same and destination is different in img destination is A,B,C and D,
and four .pdf files am trying to copy in A,B,C and D folder
this code copied all four files in folder A, and folder B,C and D are empty

64834
 
Rahul's code contains all ingredients which need some modifications so as to meet your requirements. Following code is untested but I have added comments to it so it should be easier for you to check and adopt should it run into issues.

Code:
Option Explicit

Sub CopyFileRev2()
Dim fso As Object
Dim sSFolder As String, sDFolder As String, sFile As String
Dim i As Long, lastrow As Long

ThisWorkbook.Worksheets("Sheet1").Activate

'Find The Last Row
lastrow = Range("A" & ThisWorkbook.Worksheets("Sheet1").Rows.Count).End(xlUp).Row

'Create FileSystem Object
Set fso = CreateObject("Scripting.filesystemobject")

For i = 2 To lastrow
    'Source Folder Path
    sSFolder = ThisWorkbook.Worksheets("Sheet1").Range("B" & i).Value
    'Destination Folder Path
    sDFolder = ThisWorkbook.Worksheets("Sheet1").Range("C" & i).Value
    'FileName
    sFile = ThisWorkbook.Worksheets("Sheet1").Range("A" & i).Value
   
    'Check if source file exists or not
    If Not fso.fileexists(sSFolder & sFile) Then
        MsgBox "Specified File Not Found" & sFile, vbInformation, "Not Found"
    Else
        'We found the file so lets copy it but before copying see if it already exists
        If Not fso.fileexists(sDFolder & sFile) Then
            fso.CopyFile (sSFolder & sFile), sDFolder, True
        Else
            MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
        End If
    End If
   
Next i

End Sub
 
Thank you Shrivallabha , its working perfectly fine and I understood with the help of comment
THank you Rahul your code was helpful but as I am new so was not able to fix by myself.
Finally thank you both.
 
Back
Top