Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: VBA Dir File Copy and Paste: Check if file exists then copy file from one directory to another. Bat File

  1. #1
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0

    VBA Dir File Copy and Paste: Check if file exists then copy file from one directory to another. Bat File



    “Moderator” Notice
    September 2020
    Given up with this “OP” , Avinash around September 2020. https://excelfox.com/forum/showthrea...ll=1#post14972
    https://excelfox.com/forum/showthrea...ll=1#post14972

    I am no longer monitoring what its doings. It had curiosity appeal for a while, but even that has worn off me now!
    It’s getting worse by the Day. Its still doing whatever it is that it is doing and getting Replies and answers at excelforum.com and likely a few places I don’t know about.







    C:\Users\WolfieeeStyle\Desktop\CD.xlsx

    C:\Users\WolfieeeStyle\Desktop\DF.xlsx


    my file is located here
    i need a vba code that if the cd.xlsx & DF.xlsx file is not present C:\Users\WolfieeeStyle\Desktop\CD.xlsx &C:\Users\WolfieeeStyle\Desktop\DF.xlsx here then a msg box should came mentioning error that file is not present

    i need vba code of the same
    so plz help me in solving these problem

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg
    https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgzMCQUIQgrbec400jl4AaABAg
    https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgwhVTFaD469mW9wO194AaABAg.9gJzxwFcnPU9gORqKw5t W_
    https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugyb8nmKKoXvcdM58gV4AaABAg
    https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgwvvXcl1oa79xS7BAV4AaABAg
    https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgxvIFArksPprylHXYZ4AaABAg
    https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg
    https://www.youtube.com/watch?v=v_1iqtOnUMg&lc=UgxUbeYSvsBH2Gianox4AaABAg.9VYH-07VTyW9gJV5fDAZNe
    https://www.youtube.com/watch?v=v_1iqtOnUMg&lc=UgxLtKj969oiIu7zNb94AaABAg
    https://www.youtube.com/watch?v=v_1iqtOnUMg&lc=UgyhQ73u0C3V4bEPhYB4AaABAg
    https://www.youtube.com/watch?v=v_1iqtOnUMg&lc=UgzIElpI5OFExnUyrk14AaABAg.9fsvd9zwZii9gMUka-NbIZ
    https://www.youtube.com/watch?v=jdPeMPT98QU
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 09-22-2023 at 05:14 PM.

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    Lots of ways to do this is, https://lmgtfy.com/?q=vba+check+if+file+exists ( https://lmgtfy.com/?qtype=search&q=v...if+file+exists )


    For example
    Dir Function
    ( https://docs.microsoft.com/en-us/off...p/dir-function )

    If _ “MyFile.xls _ is at _ “C\Desktop” _ Then _ Dir(“ C\Desktop\MyFile.xls”, vbNormal ) = “MyFile.xls

    If _ “MyFile.xls _is Not at_ C\Desktop _ Then _Dir(“ C\Desktop\MyFile.xls” , vbNormal ) = “”



    Code:
    ' http://www.excelfox.com/forum/showthread.php/2423-Check-file-is-present-or-not
    Sub VBACheckIfFileExists() '    https://lmgtfy.com/?q=vba+check+if+file+exists  (  https://lmgtfy.com/?qtype=search&q=vba+check+if+file+exists  )
    Dim StrDirBack As String
     Let StrDirBack = Dir(ThisWorkbook.path & "\" & ThisWorkbook.Name, vbNormal)
        If StrDirBack = "" Then
         MsgBox prompt:=ThisWorkbook.Name & "  is not at  " & ThisWorkbook.FullName
        Else
         MsgBox prompt:=ThisWorkbook.Name & "  is at  " & ThisWorkbook.FullName
         MsgBox prompt:=Dir(ThisWorkbook.path & "\" & ThisWorkbook.Name, vbNormal) & "  is at  " & ThisWorkbook.FullName
        End If
     
     Let StrDirBack = Dir(ThisWorkbook.FullName, vbNormal)
        If StrDirBack = "" Then
         MsgBox prompt:=ThisWorkbook.Name & "  is not at  " & ThisWorkbook.FullName
        Else
         MsgBox prompt:=ThisWorkbook.Name & "  is at  " & ThisWorkbook.FullName
         MsgBox prompt:=Dir(ThisWorkbook.FullName, vbNormal) & "  is at  " & ThisWorkbook.FullName
        End If
    
     Let StrDirBack = Dir("C:\Users\WolfieeeStyle\Desktop\CD.xlsx", vbNormal)
        If StrDirBack = "" Then
         MsgBox prompt:="CD.xlsx  is not at  C:\Users\WolfieeeStyle\Desktop\CD.xlsx"
        Else
         MsgBox prompt:="CD.xlsx  is at  C:\Users\WolfieeeStyle\Desktop\CD.xlsx"
         MsgBox prompt:=Dir("C:\Users\WolfieeeStyle\Desktop\CD.xlsx", vbNormal) & "  is at  C:\Users\WolfieeeStyle\Desktop\CD.xlsx"
        End If
    
     Let StrDirBack = Dir("C:\Users\WolfieeeStyle\Desktop\DF.xlsx", vbNormal)
        If StrDirBack = "" Then
         MsgBox prompt:="DF.xlsx  is not at  C:\Users\WolfieeeStyle\Desktop\DF.xlsx"
        Else
         MsgBox prompt:="DF.xlsx  is at  C:\Users\WolfieeeStyle\Desktop\DF.xlsx"
         MsgBox prompt:=Dir("C:\Users\WolfieeeStyle\Desktop\DF.xlsx", vbNormal) & "  is at  C:\Users\WolfieeeStyle\Desktop\DF.xlsx"
        End If
    
    End Sub

    Alan

    Last edited by DocAElstein; 03-05-2020 at 01:14 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  3. #3
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Code:
    Sub VBACheckIfFileExists()
    Dim StrDirBack As String
     Let StrDirBack = Dir("C:\Users\WolfieeeStyle\Desktop\FundsCheck.xlsb", vbNormal)
        If StrDirBack = "" Then
         MsgBox prompt:="Download2"
        Else
         
        End If
    
     Let StrDirBack = Dir("C:\Users\WolfieeeStyle\Desktop\1.xls", vbNormal)
        If StrDirBack = "" Then
         MsgBox prompt:="Download1"
        Else
         
        End If
    
    End Sub

    Awesome Doc Sir thats what i like about u
    Pefect code


    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://eileenslounge.com/viewtopic.php?f=27&t=35521&p=276185#p276185
    https://eileenslounge.com/viewtopic.php?p=276185#p276185
    https://eileenslounge.com/viewtopic.php?p=276185#p276185
    https://eileenslounge.com/viewtopic.php?p=276673#p276673
    https://eileenslounge.com/viewtopic.php?p=276751#p276751
    https://eileenslounge.com/viewtopic.php?p=276754#p276754
    https://eileenslounge.com/viewtopic.php?f=30&t=35100&p=274367#p274367
    https://eileenslounge.com/viewtopic.php?p=274368#p274368
    https://eileenslounge.com/viewtopic.php?p=274370#p274370
    https://eileenslounge.com/viewtopic.php?p=274578#p274578
    https://eileenslounge.com/viewtopic.php?p=274577#p274577
    https://eileenslounge.com/viewtopic.php?p=274474#p274474
    https://eileenslounge.com/viewtopic.php?p=274579#p274579
    https://www.excelfox.com/forum/showthread.php/261-Scrolling-Marquee-text-on-Userform?p=864&viewfull=1#post864
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://eileenslounge.com/viewtopic.php?f=27&t=35521&p=276185#p276185
    https://eileenslounge.com/viewtopic.php?p=276185#p276185
    https://eileenslounge.com/viewtopic.php?p=276185#p276185
    https://eileenslounge.com/viewtopic.php?p=276673#p276673
    https://eileenslounge.com/viewtopic.php?p=276751#p276751
    https://eileenslounge.com/viewtopic.php?p=276754#p276754
    https://eileenslounge.com/viewtopic.php?f=30&t=35100&p=274367#p274367
    https://eileenslounge.com/viewtopic.php?p=274368#p274368
    https://eileenslounge.com/viewtopic.php?p=274370#p274370
    https://eileenslounge.com/viewtopic.php?p=274578#p274578
    https://eileenslounge.com/viewtopic.php?p=274577#p274577
    https://eileenslounge.com/viewtopic.php?p=274474#p274474
    https://eileenslounge.com/viewtopic.php?p=274579#p274579
    https://www.excelfox.com/forum/showthread.php/261-Scrolling-Marquee-text-on-Userform?p=864&viewfull=1#post864
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 04-07-2024 at 11:59 AM.

  4. #4
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    Your macro is good
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  5. #5
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Bcoz of u Yesterday u taught me about else so i done the same

  6. #6
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    This will also work
    Code:
    Sub VBACheckIfFileExists()
    Dim StrDirBack As String
     Let StrDirBack = Dir("C:\Users\WolfieeeStyle\Desktop\FundsCheck.xlsb", vbNormal)
        If StrDirBack = "" Then
         MsgBox prompt:="Download2"
        End If
    
     Let StrDirBack = Dir("C:\Users\WolfieeeStyle\Desktop\1.xls", vbNormal)
        If StrDirBack = "" Then
         MsgBox prompt:="Download1"
        End If
    End Sub
    And also this will work.
    Code:
    Sub VBACheckIfFileExists()
    Dim StrDirBack As String
     Let StrDirBack = Dir("C:\Users\WolfieeeStyle\Desktop\FundsCheck.xlsb", vbNormal)
        If StrDirBack = "" Then  MsgBox prompt:="Download2"
    
     Let StrDirBack = Dir("C:\Users\WolfieeeStyle\Desktop\1.xls", vbNormal)
        If StrDirBack = "" Then MsgBox prompt:="Download1"
    
    End Sub
    You can choose.

    I like to use Else and also EndIf

    It is just a personal choice.
    Just my personal preference

    With Else is good to add ' notes . Later I can remember what is all about
    Code:
    Sub VBACheckIfFileExists()
    Dim StrDirBack As String
     Let StrDirBack = Dir("C:\Users\WolfieeeStyle\Desktop\FundsCheck.xlsb", vbNormal)
        If StrDirBack = "" Then
         MsgBox prompt:="Download2"
        Else ' My StrDirBack is =  "FundsCheck.xlsb"   so ...
         ' ...  I do not do anything here
        End If
    
     Let StrDirBack = Dir("C:\Users\WolfieeeStyle\Desktop\1.xls", vbNormal)
        If StrDirBack = "" Then
         MsgBox prompt:="Download1"
        Else ' My StrDirBack is =  "1.xls"   so ...
         ' ...  I do not do anything here
        End If
    
    End Sub
    Code:
    Sub VBACheckIfFileExists()
    Dim StrDirBack As String
     Let StrDirBack = Dir("C:\Users\WolfieeeStyle\Desktop\FundsCheck.xlsb", vbNormal)
        If StrDirBack = "" Then ' My StrDirBack is =  ""  ===='_-
         MsgBox prompt:="Download2"
        Else ' My StrDirBack is =  "FundsCheck.xlsb"   so ...
         ' ...  I do not do anything here
        End If ' End  If StrDirBack = ""    =================='_-
    
     Let StrDirBack = Dir("C:\Users\WolfieeeStyle\Desktop\1.xls", vbNormal)
        If StrDirBack = "" Then  ' My StrDirBack is =  "" ====|||
         MsgBox prompt:="Download1"
        Else ' My StrDirBack is =  "1.xls"   so ...
         ' ...  I do not do anything here
        End If  ' End  If StrDirBack = ""   ==================|||
    
    End Sub
    But you can choose.
    It is free world. Stay cool and do what you want - do anything you wanna do https://listenonrepeat.com/watch/?v=...o_-_1977_45rpm
    Last edited by DocAElstein; 03-05-2020 at 02:39 AM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  7. #7
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Sure Sir & thnx for ur Great Support & for making my Journey Simpler

  8. #8
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0

    VBA Copy and Paste: Check if file exists then copy file from one directory to another

    vba is placed in a seperate file
    and the file which we have to copy is located in C:\Users\WolfieeeStyle\Desktop\Files and the file name is 1.xlsx
    so we have to copy that file and paste it to (C:\Users\WolfieeeStyle\Desktop\save it) this location only if file is not present
    and if file is present in (C:\Users\WolfieeeStyle\Desktop\save it) this location then we have to copy and paste the file to C:\Users\WolfieeeStyle\Desktop\save it\New folder

    so plz help me in solving this problem sir
    Last edited by DocAElstein; 03-05-2020 at 03:19 PM.

  9. #9
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10

    To search for:-
    vba copy file from one directory to another
    Try…-
    https://lmgtfy.com/?q=vba+copy+file+...ory+to+another
    https://lmgtfy.com/?qtype=search&q=v...36912822145674
    _.. then maybe https://www.rondebruin.nl/win/s3/win026.htm

    Copy one file
    For one file you can use the VBA FileCopy statement
    Code:
    Sub Copy_One_File() ' https://www.rondebruin.nl/win/s3/win026.htm    FileCopy statement  https://docs.microsoft.com/de-de/office/vba/language/reference/user-interface-help/filecopy-statement
     FileCopy Source:="C:\Users\WolfieeeStyle\Desktop\Files\1.xlsx", Destination:="C:\Users\WolfieeeStyle\Desktop\save it\1.xlsx"
     FileCopy Source:="C:\Users\WolfieeeStyle\Desktop\Files\1.xlsx", Destination:="C:\Users\WolfieeeStyle\Desktop\save it\New folder\1.xlsx"
    End Sub




    Combining the above with macros from here: http://www.excelfox.com/forum/showth...ll=1#post12560

    For example:-
    If the file with the next macro in it, is in the same Folder as the Folder with name .. save it
    C:\Users\WolfieeeStyle\Desktop\ save it\New folder
    save it New folder.JPG
    https://imgur.com/BUFvGRh

    Code:
    Sub VBACheckIfFileExists_ThenCopy()   '                           http://www.excelfox.com/forum/showthread.php/2424-VBA-Check-if-file-exists-then-copy-file-from-one-directory-to-another
    Dim StrDirBack As String   '  The file which we have to copy is located in C:\Users\WolfieeeStyle\Desktop\Files and the file name is 1.xlsx
    '          paste it to (C:\Users\WolfieeeStyle\Desktop\save it) this location only if file is not present
     Let StrDirBack = Dir(ThisWorkbook.Path & "\save it\1.xlsx", vbNormal)
        If StrDirBack = "" Then          ' so we have to copy that file and paste it to (C:\Users\WolfieeeStyle\Desktop\save it) this location only if file is not present
          FileCopy Source:=ThisWorkbook.Path & "\1.xlsx", Destination:=ThisWorkbook.Path & "\save it\1.xlsx"
        Else                              'and if file is present in (C:\Users\WolfieeeStyle\Desktop\save it) this location then we have to copy and paste the file to C:\Users\WolfieeeStyle\Desktop\save it\New folder
          FileCopy Source:=ThisWorkbook.Path & "\1.xlsx", Destination:=ThisWorkbook.Path & "\save it\New folder\1.xlsx"
        End If
    
    End Sub
    




    Copy and paste file , 1.xlsx , to a new location if it does not exist there, else copy and paste it to a different location.

    The source file to be copied has the name , 1.xlsx .
    It is in folder , Files , at the Desktop , C:\Users\WolfieeeStyle\Desktop
    So it has the full path and file name of:-
    C:\Users\WolfieeeStyle\Desktop\Files\1.xlsx

    The file needs to be pasted to one of two locations…
    The full path and file name for the file to be pasted ( the Destination ) is:-

    Either

    __ if file is not present at C:\Users\WolfieeeStyle\Desktop\save , it then paste it there:-
    C:\Users\WolfieeeStyle\Desktop\save it\1.xlsx

    __ Else if the file, 1.xlsx , is already present at C:\Users\WolfieeeStyle\Desktop\save it , then paste it at:
    C:\Users\WolfieeeStyle\Desktop\save it\New folder\1.xlsx


    Code:
    Sub VBACheckIfFileExists_Then_Copy()   '                           http://www.excelfox.com/forum/showthread.php/2424-VBA-Check-if-file-exists-then-copy-file-from-one-directory-to-another
    Dim StrDirBack As String   '
     Let StrDirBack = Dir("C:\Users\WolfieeeStyle\Desktop\save it\1.xlsx", vbNormal)  '   If  1.xlsx  is not present at  C:\Users\WolfieeeStyle\Desktop\save it   then  Dir(  )    will return  ""
        If StrDirBack = "" Then          ' copy  1.xlsx  to (C:\Users\WolfieeeStyle\Desktop\save it) only if file is not present at C:\Users\WolfieeeStyle\Desktop\save it
          'The file which we have to copy is located in C:\Users\WolfieeeStyle\Desktop\Files and the file name is 1.xlsx
          FileCopy Source:="C:\Users\WolfieeeStyle\Desktop\Files\1.xlsx", Destination:="C:\Users\WolfieeeStyle\Desktop\save it\1.xlsx"
        Else                              'and if file is present in (C:\Users\WolfieeeStyle\Desktop\save it) this location then we have to copy and paste the file to C:\Users\WolfieeeStyle\Desktop\save it\New folder
          'The file which we have to copy is located in C:\Users\WolfieeeStyle\Desktop\Files and the file name is 1.xlsx
          FileCopy Source:="C:\Users\WolfieeeStyle\Desktop\Files\1.xlsx", Destination:="C:\Users\WolfieeeStyle\Desktop\save it\New folder\1.xlsx"
        End If
    
    End Sub
    


    Simplified:-
    Code:
    Sub VBACheckIfFileExists_Then__Copy()
    Dim StrDirBack As String
     Let StrDirBack = Dir("C:\Users\WolfieeeStyle\Desktop\save it\1.xlsx", vbNormal)
        If StrDirBack = "" Then
          FileCopy "C:\Users\WolfieeeStyle\Desktop\Files\1.xlsx", "C:\Users\WolfieeeStyle\Desktop\save it\1.xlsx"
        Else
          FileCopy "C:\Users\WolfieeeStyle\Desktop\Files\1.xlsx", "C:\Users\WolfieeeStyle\Desktop\save it\New folder\1.xlsx"
        End If
    End Sub
    Code:
    Sub VBACheckIfFileExists_Then___Copy()
        If Dir("C:\Users\WolfieeeStyle\Desktop\save it\1.xlsx") = "" Then
          FileCopy "C:\Users\WolfieeeStyle\Desktop\Files\1.xlsx", "C:\Users\WolfieeeStyle\Desktop\save it\1.xlsx"
        Else
          FileCopy "C:\Users\WolfieeeStyle\Desktop\Files\1.xlsx", "C:\Users\WolfieeeStyle\Desktop\save it\New folder\1.xlsx"
        End If
    End Sub






    Alan






    macros in first worksheets code module of process.xlsm ,
    Right Mouse Click First Tab View Code.jpg : https://imgur.com/8cu1Mbn
    Right Mouse Click First Tab View Code.jpg
    Attached Files Attached Files
    Last edited by DocAElstein; 03-05-2020 at 05:37 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  10. #10
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0

    copy paste a file if its not present & if its present then do nothing

    my file name is 4.xlsx
    i have to copy this file and paste it to another path( if 4.xlsx file doesn't exist then copy paste it to C:\Users\WolfieeeStyle\Desktop/sholtan , but if file is there in the path C:\Users\WolfieeeStyle\Desktop/sholtan then do nothing)
    i want to do this by vba so plz have a look sir and help me in solving this problem sir
    file is located at C:\Users\WolfieeeStyle\Desktop/4.xlsx and we have to copy this file and paste it to C:\Users\WolfieeeStyle\Desktop/sholtan

Similar Threads

  1. Replies: 85
    Last Post: 06-09-2020, 05:58 PM
  2. Replies: 4
    Last Post: 05-08-2014, 10:12 PM
  3. Replies: 7
    Last Post: 08-28-2013, 12:57 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •