• 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.

Files Move to specific folder

Abhijeet

Active Member
Hi

I have macro this macro move file to specific folder but this macro move single file & every time change the file path so i want what ever in column A has mention file path move that file to new path that is mention in Column B please tell me how to do this.
 

Attachments

  • Files move macro.xlsm
    14.8 KB · Views: 1
For single cell i do this but please tell me dynamic range what changes need to do this
Code:
' old file name
    filenm = Cells(2, 1).Value
    'new File Name
    newfolder = Cells(2, 2).Value
 
Hi
I am solve my problem but can any one please tell me i am correct or not.Output give correct but i am new for doing this coding part so please tell me
Code:
Function file_exists(fl_path As String) As String
    If Dir(fl_path) <> "" And fl_path <> "" Then
        file_exists = "Exists"
    Else
        file_exists = "Not Exists"
    End If
End Function


Function folder_exists(fld_path As String) As String

    If Len(Dir(fld_path, vbDirectory)) <> 0 And fld_path <> "" Then
        folder_exists = "Exists"
    Else
        folder_exists = "Not Exists"
    End If
   
End Function


Sub move_file()

    Dim filenm As String
    Dim newfolder As String
    Dim newpath As String
    Dim fld As Object
    Dim i As Long, LRow As Long
    LRow = ActiveSheet.Range("a" & Rows.Count).End(xlUp).Row
For i = 2 To LRow
    ' old file name
    filenm = Range("a" & i)
    'new File Name
    newfolder = Range("b" & i) ' please add "\" as the end
    ' new path
    ' add \ at the end of folder
    If VBA.Right(newfolder, 1) <> "\" Then newfolder = newfolder & "\"
    ' new path of file
    newpath = newfolder & VBA.Right(filenm, Len(filenm) - InStrRev(filenm, "\"))
   
    ' add some control check to avoid crashes
   
    ' check if file exists which we want to move
    If file_exists(filenm) <> "Exists" Then
        MsgBox "File does not exists so can not be moved ", vbInformation, "Note:"
        Exit Sub
    End If
   
    ' check if file already exits at destination folder
    If file_exists(newpath) = "Exists" Then
        MsgBox "File already exists at destination folder so can not be moved ", vbInformation, "Note:"
        Exit Sub
    End If

    ' check if  destination folder exists
    If folder_exists(newfolder) <> "Exists" Then
        MsgBox "Destination folder does not exists. Please create the folder first", vbInformation, "Note:"
        Exit Sub
    End If
   
    'move it finally

    Set fld = CreateObject("Scripting.FileSystemObject")
    fld.Movefile filenm, newfolder
  Next i

End Sub
 
Hi

Can u please help me i have this macro in Column C New File Path where i want to move the files but i have problem is if sub folder is not created in given path then create sub folder based on column E has mention folder names. Please tell me how to do this
 

Attachments

  • Files move macro.xlsm
    24.4 KB · Views: 2
Back
Top