+ Reply to Thread
Results 1 to 6 of 6

Move Folder to existing destination

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Post Move Folder to existing destination

    I have a function to move/rename a file or folder. I have been using this in many projects for a long time without any problem. Until now.

    This is the first time I have tried to use it to move a folder to another location that is not empty.

    Below is what I am using to test the error:
    Please Login or Register  to view this content.
    I am including the function code at the bottom of this post.

    Here's my question. What would be the best approach to amend this function so that it will allow (or achieve the same end result as) moving a folder to an existing non-empty folder?

    To make this more challenging - I impose the following limitations: In interests of both performance and maximum compatibility I want to use native VBA only. By that I mean: No use of FSO, APIs or Shell


    Here's the function. (Credit to Ken Puls MVP for the original version of fnblnExistsFileFolder) (I've added line numbering to make it easier to refer to specific lines)
    Please Login or Register  to view this content.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Move Folder to existing destination

    I can think of a way to achieve the end result but I'm not sure if it is the most efficient.

    I would be interested if anybody has a better approach or suggestions to make.

    • We can determine if the destination dir contains items or not by an error check after line 180. (If error occurs then the dest contains items. So it will require an alternative action instead of line 280)
    • Moving a source folder to (overwrite) an existing folder has the same effect as 1) move all child items from the source dir to the dest dir then 2) RmDir the (now-empty) source dir

  3. #3
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Move Folder to existing destination

    What would be the best approach?
    - my approach would be write a new function to move the files and call that if the folder exists already
    - you may want to ask the user if files should be overwritten etc and modify the approach accordingly

    The new funcion would do the following
    Loop through the files in the (old) folder and for each one
    1. build newFile string for each file
    2. build oldFile string for each file
    3. kill newFile (prevent risk of code failing)
    4. move the file

    Useful code snippets

    To kill
    Please Login or Register  to view this content.
    Moving a file is done by renaming the full filepath (folderpath & filename & extension)
    The code is simple
    Please Login or Register  to view this content.
    Here is a sub that you could turn into a function and adapt as necessary
    credit https://stackoverflow.com/questions/...lder-using-vba
    Please Login or Register  to view this content.
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Move Folder to existing destination

    Good points kev_ (Reps+1)

    Agree with prompting user if destination file already exists.

    My one concern would be that this function is only checking/moving files within the source folder tree. What if the source dir contained files & folders (and more files within those subfolders)?

    So perhaps I need to look at a recursive function to return all files & folders within the source dir before moving these to the dest dir.

  5. #5
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Move Folder to existing destination

    My one concern would be that this function is only checking/moving files within the source folder tree. What if the source dir contained files & folders (and more files within those subfolders)?
    @mc84excel
    - agreed
    - and then it gets much more complicated
    - would need to check if subFolder already exists before moving anything otherwise need to create it etc
    - should file overwrites be carried out all the way down the tree?

    My recommendation is to add a condition:

    IF destination folder already exists then do NOT move files if source folder contains subFolders

    OR
    - only move the files

    If half the structure is already there, there could be some unexpected fallout
    - probably safer to do things piecemeal

    But they are your files - if you can live with the risk then
    - move the files (overwriting as you go if any files alreaddy exist) to an existing folder\subFolder
    - when code finds subFolder that does not exist call original function and move that subFolder
    Last edited by kev_; 03-13-2019 at 04:18 AM.

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Move Folder to existing destination

    Thanks for the thoughts kev_, I ended up taking a different approach. If the optional argument for OverWriteAll is set, it will delete any existing before moving. If the optional argument is not set, a MsgBox will appear for each existing file giving the option to overwrite the destination file or not (Basically trying to recreate the warning message/option that appears when you move files in Windows Explorer).

    I am marking this thread solved. For the curious, my solution can be seen in template code in a recent upload.

    Thread URL = http://www.excelforum.com/excel-prog...-userform.html
    Module name = 't_FileFolder_190321'
    Function name = pMoveDirToExistDir (It's called from another function in the same module)
    Last edited by mc84excel; 04-04-2019 at 03:00 AM.

+ 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. [SOLVED] Move the existing (opened) workbook to a new folder location
    By subbby in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-16-2016, 04:31 PM
  2. Specifying a folder destination
    By kor123 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-21-2016, 11:26 AM
  3. [SOLVED] Copy certain files from Source folder to Destination folder
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-16-2015, 08:56 PM
  4. When i Copy Folder to new destination i lose customised folder icon/picture
    By sternboy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-29-2012, 09:30 PM
  5. name existing on the destination folder
    By LeFootie in forum Excel General
    Replies: 3
    Last Post: 07-05-2012, 11:09 AM
  6. Move destination row
    By rhudgins in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-27-2010, 04:33 PM
  7. Tweaking existing Macro to give info by DESTINATION
    By rushdenx1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-06-2010, 12:23 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