+ Reply to Thread
Results 1 to 20 of 20

File Copy Failing if Path Doesn't Exist

  1. #1
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    File Copy Failing if Path Doesn't Exist

    Hello,

    I have the following code which copies files from a list of paths in column A and puts them in a path which is in column B. The issue I am having is if the path doesn't exist in column B where the copies are to be placed the macro bombs out. Is there something simple I can add to my code which works when the folders exists as it is to create the needed folders if they don't exist?


    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: File Copy Failing if Path Doesn't Exist

    an example of column A and Column B below. However it won't end up on the C drive. It will end up on a network drive "M".

    c:\Users\anthony.martin\Desktop\New folder\200 Lafayette - Office CAPEX 2017.09.pdf C:\Users\anthony.martin\Desktop\New folder\Test\2017\Q3\0-Properties\200 Lafayette-48010\200 Lafayette - Office CAPEX 2017.09.pdf
    c:\Users\anthony.martin\Desktop\New folder\200 Lafayette - Retail CAPEX 2017.09.pdf C:\Users\anthony.martin\Desktop\New folder\Test\2017\Q3\0-Properties\200 Lafayette-48010\200 Lafayette - Retail CAPEX 2017.09.pdf

  3. #3
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: File Copy Failing if Path Doesn't Exist

    Change this
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: File Copy Failing if Path Doesn't Exist

    Quote Originally Posted by smpita View Post
    Change this
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.
    Or if you want to be notified of what it missed, change it to this
    Please Login or Register  to view this content.
    Last edited by smpita; 12-28-2017 at 04:04 PM.

  5. #5
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: File Copy Failing if Path Doesn't Exist

    will this change create the folders in the path if they are missing so that it copies the file? I don't want it to skip files. I want it to create the missing folders to place the copy of the pdf into the string of folders.

  6. #6
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: File Copy Failing if Path Doesn't Exist

    It will not. My apologies for not reading your post completely and misunderstanding the request. Hang tight.

  7. #7
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: File Copy Failing if Path Doesn't Exist

    no need for an apology. I appreciate your help.

  8. #8
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: File Copy Failing if Path Doesn't Exist

    Try this
    Please Login or Register  to view this content.
    Note: There's no way to guarantee that the directory can be made, so this can still fail. It will tell you if it can't make the directory. If it can make the directory, it assumes it can also write the file. I didn't write any checks to safeguard against insufficient space/other reasons it can't write files at the destination.
    Last edited by smpita; 12-28-2017 at 05:28 PM. Reason: Forgot pathExists function

  9. #9
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: File Copy Failing if Path Doesn't Exist

    I am getting a byreft argument type mismatch compile error on this line at the (FileDestination) in the If section shown below.

    If makeDir(getDirName(FileDestination)) Then FileCopy FileSource, FileDestination

  10. #10
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: File Copy Failing if Path Doesn't Exist

    Change the top of your Sub to this and let me know if it continues.
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: File Copy Failing if Path Doesn't Exist

    Thank you. Now I am getting a compile error for user-defined type not defined in the function section in the Dim section below.

    Public Function getDirName(sFilePath As String) As String

    ' Get the directory

    Dim filesystem As New FileSystemObject

  12. #12
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: File Copy Failing if Path Doesn't Exist

    Within Excel you need to set a reference to the VB script run-time library. The relevant file is usually located at \Windows\System32\scrrun.dll or \Windows\SysWOW64\scrrun.dll
    • To reference this file, load the Visual Basic Editor (ALT+F11)
    • Select Tools > References from the drop-down menu
    • A listbox of available references will be displayed
    • Tick the check-box next to 'Microsoft Scripting Runtime'
    • The full name and path of the scrrun.dll file will be displayed below the listbox
    • Click on the OK button.

    Note: This is a setting for your installation of Excel. If other people want to use your macro, they will need to follow these steps to set the reference as well.

  13. #13
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: File Copy Failing if Path Doesn't Exist

    Thank you. Now it is saying pathexists subs or functions is not defined under the make directory function.

    Public Function makeDir(sDirPath As String) As Boolean

    ' Function to recursively make a directory if it doesn't exist

    Dim vDirPart As Variant
    Dim sRecurDir As String

    If pathExists(sDirPath) Then

  14. #14
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: File Copy Failing if Path Doesn't Exist

    Thanks for all of your help with this. I really appreciate it.

  15. #15
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: File Copy Failing if Path Doesn't Exist

    Quote Originally Posted by amartin575 View Post
    Thank you. Now it is saying pathexists subs or functions is not defined under the make directory function.

    Public Function makeDir(sDirPath As String) As Boolean

    ' Function to recursively make a directory if it doesn't exist

    Dim vDirPart As Variant
    Dim sRecurDir As String

    If pathExists(sDirPath) Then
    Yeah in my "Try this" post, I missed a function when I copy-pasted. I added it via edit but it seems you were quicker than me. It is in the above code block (for later people) but here it is again, isolated for easy copy.
    Please Login or Register  to view this content.

  16. #16
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: File Copy Failing if Path Doesn't Exist

    you are the bomb smpita! worked like a champ and finished within seconds!

  17. #17
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: File Copy Failing if Path Doesn't Exist

    Thanks again!

  18. #18
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: File Copy Failing if Path Doesn't Exist

    Hello smpita, So the code that I thought worked partially works. It only moves the file if the path doesn't exist. It then creates the folders and puts a copy of the file in that location. The code doesn't put a copy to the location if the specified path already exists. Is there an ElseIf that I can place in this code to get it to work for paths that already exist?

    Please Login or Register  to view this content.

  19. #19
    Forum Contributor
    Join Date
    10-24-2012
    Location
    Chicago
    MS-Off Ver
    O365
    Posts
    311

    Re: File Copy Failing if Path Doesn't Exist

    nevermind I think I figured it out by adding an Else to it.

  20. #20
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: File Copy Failing if Path Doesn't Exist

    The actual issue is here
    Please Login or Register  to view this content.
    If you change it to this, it should fix the issue.
    Please Login or Register  to view this content.
    When the directory existed, it would exit from the function without setting the boolean, defaulting the boolean to a false in the following IF conditional.

    Please Login or Register  to view this content.
    This change sets the boolean to True when the directory exists and allows the IF conditional to pass, subsequently executing the FileCopy command.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. If file path doesn't exist excel freezes
    By Harribone in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2015, 01:36 PM
  2. [SOLVED] Syntax Compile Error - How to work around this (file path doesn't exist)
    By Harribone in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-18-2015, 08:19 AM
  3. [SOLVED] Look for a File and create one if it doesn't exist
    By Titansmasher in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-08-2012, 07:43 PM
  4. [SOLVED] VBA Loop - Does file exist (Y/N) at the path specified?
    By always_learning in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-12-2012, 07:01 PM
  5. Macro to check dynamic file path and create if does not exist
    By Matso in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-30-2011, 01:56 AM
  6. Shared File doesn't exist
    By rvancura32 in forum Excel General
    Replies: 0
    Last Post: 02-09-2009, 01:19 PM
  7. Replies: 4
    Last Post: 06-18-2006, 01:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1