+ Reply to Thread
Results 1 to 15 of 15

Copy matching files/folders (using unique ID) from one directory to another

  1. #1
    Registered User
    Join Date
    06-04-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    27

    Copy matching files/folders (using unique ID) from one directory to another

    Good morning all,

    Hopefully my question title was not too vague, however please let me explain the issue in more detail...

    I have two directories, both with many subfolders, with each of these subfolders being assigned a unique ID reference number and containing an Excel file with the same corresponding ID number. Some of the Excel files do not belong to a subfolder and are placed directly into the parent folder. To clarify, please find an example below;


    Main (Parent) Folders ---- Subfolder ---- File(s)

    Finance ---- 5000 ---- 5000 - Financev1.xlsx
    Management ---- 5001 ---- 5001 - Mangementv1.xlsx
    Customer Service ---- 5002 ---- 5002 - CustServicev1.xlsx

    Example path: C:\My Documents\Finance\5000\5000-Financev1.xlsx

    To make matters more confusing, many of these type of files are on another drive (Z:\) and have been incorrectly named/filed (although generally always including their respective 4 digit ID reference in the title) and therefore the search must sometimes only look up a portion of the filename.

    I have unfortunately been tasked with copying all of the folders containing the Excel files from Z:\ to C:\ while keeping all of the files contained in each folder.

    The folders containing the Excel files already exist on the Z:\ drive while an empty folder of the same name already exists on the C:\ drive - the lookup must therefore look through all of the folders on the Z:\ drive and, if the folder/file matches the corresponding folder name on the C:\ drive, then make a copy of it while keeping any subfolders/files in place. It should also preferably not delete, overwrite or remove any files.

    If anyone could help me out with this, I'd be extremely grateful. I'm very new to VBA and have done many searches on the internet for what I am looking for, but unfortunately have been unable to successfully implement any of the codes I have found. If anyone requires additional clarification on any of the above, please let me know and I will try to provide a clear explanation.

    Thank you all in advance.

    Tom

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Copy matching files/folders (using unique ID) from one directory to another

    How many files are there in the Z drive folder that don't need copying? If it's not too many, and all of the files start with the 4 digit ID I would approach this by first copying the whole folder from Z to C and then searching for all files in the folder on C drive. Sort by name and you should get all the files with numbers at the top.

    It's probably easier to copy the files across and delete the ones you don't want with VBA for what it's worth, then you don't need to deal with creating the subfolder structure and can just cycle through all the files/folders and look at each filename.

    It would help if you could provide a bigger list of example filenames, especially if they are incorrectly named/filed to work out what the condition needs to be to filter out the files.

    If it's any help, I made this macro to copy my outlook folder structure and files to a network drive, which isn't too disimilar to what you are looking to do:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-04-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Copy matching files/folders (using unique ID) from one directory to another

    Hi yudlugar,

    Thank you very much for your response - apologies for the delay in my reply.

    What I have done is copied both the source folder (found in Z and the target folder (found in C:\) to my own documents to allow me to test without making permanent changes, as the two drives are used by the entire company and any overwriting of data would land me in a lot of trouble !

    To answer your question in more detail, there are about 600 folders that would need to be copied. The source folders (let’s call these A) are named with the area of business they represent e.g. Finance, Management, Retail etc. Within each of these folders, there are many subfolders which are all named in the same format – “XXXX - <Area Name>_<Project Name>”, for example: “3400 – Finance_Licencesv2” – this folder contains the relevant Excel file.

    As mentioned before, there are some older Excel files which do not have a folder determining their area. I have moved all of these into one folder now, therefore maybe a separate amended search script could/should be used for these?

    The target folders (let’s call these B) are named for every project ID ever created e.g. 1000, 1001, 1002, 1003 etc – they are only named with the ID, no other words are included. What I would need to do is search the folders in A using the project ID and, if matched, copy the entire subfolder (“XXXX - <Area Name>_<Project Name>”) to the relevant folder in B.

    So, for example’s sake, the entire “3400 – Finance_Licencesv2” folder in A would be copied into the “3400” folder in B due to the matching ID reference.

    To throw another spanner into the works, and if it would not be too much extra effort, it would be helpful if a new folder was created in the target folder called “Close Down Forms”. An example to include this additional request would be;

    Folder “3400 – Finance_Licencesv2” in A would be matched to folder “3400” in B. It would then create a new folder called “Close Down Forms” within folder “3400” and copy “3400 – Finance_Licencesv2” along with all of its enclosed files/folders into this.

    I hope I have made my request a little more understandable although I am not the best at explaining things . If the macro could just look up the ID reference then that should be fine – there are a few incorrectly named files/folders but not enough to not go through them manually (which is probably best anyway to ensure they are correct).

    If you need any further info, please let me know.

    Many thanks,

    Tom
    Last edited by TommyK25; 06-06-2013 at 07:02 AM.

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Copy matching files/folders (using unique ID) from one directory to another

    I haven't really tested it but I think what you want is:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-04-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Copy matching files/folders (using unique ID) from one directory to another

    Thanks very much for that, yudlugar!

    It put my PC out of action for about 20 minutes , unfortunately however I received an error RE: the line I have highlighted in red;

    Please Login or Register  to view this content.
    Sorry but I am very new with VBA - please could you confirm whether there was any other manual inputs required in your code other than those I have highlighted in yellow?

    Many thanks,

    Tom

  6. #6
    Registered User
    Join Date
    06-04-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Copy matching files/folders (using unique ID) from one directory to another

    Apologies, I did not mention the error I received which was: Run-time error '76': Path not found.

    Tom

  7. #7
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Copy matching files/folders (using unique ID) from one directory to another

    Sorry, I made a bit of an error on that one, it should be
    Please Login or Register  to view this content.
    I think. Was it working up to that point (i.e. did it move anything?)

  8. #8
    Registered User
    Join Date
    06-04-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Copy matching files/folders (using unique ID) from one directory to another

    No worries! No it doesn't look like any files were moved when I ran it.

    Should I be inputting a name of the folder I want it to create (which would be called "Close Down Forms")?

    Thanks,

    Tom

  9. #9
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Copy matching files/folders (using unique ID) from one directory to another

    Ah I forgot about that bit:
    Please Login or Register  to view this content.
    I'm not sure though, I tested it as best I could with some dummy folders and it seemed to pick up the files and copy them across. If it got as far as the line you mention then it should have copied something by that point.

    Also, I think this is pushing the limits of what could be called "excel vba"! Are the files at least .xls?

  10. #10
    Registered User
    Join Date
    06-04-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Copy matching files/folders (using unique ID) from one directory to another

    Thanks very much for re-pasting the code with the updated Folder Name entry, yudlugar.

    Yes all of the files contained in the folders are .xlsx - I did think it may be a little unreasonable to request such a code on an Excel/VBA forum, however I currently have a limited knowledge of Excel while zero knoiwledge of Command Prompt (I had previously searched various forums where several methods using RoboCopy, PowerShell, .BAT etc. were offered but unfortunately I couldn't get my head around any of them!).

    Thank you for all of your help with this, it's very much appreciated. I will try the updated code you have provided and re-post once I have done. I may also try it on a smaller batch of files to begin with as the time it takes to finish is between 20 & 30 minutes on my horrendously slow laptop.

    Will keep you posted...

    Tom

  11. #11
    Registered User
    Join Date
    06-04-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Copy matching files/folders (using unique ID) from one directory to another

    Just a quick update - I tried the code using some test files e.g "TEST A" containing folders "1" & "2" and "TEST B" with the same folders. Any Excel files that I place in 1 & 2 in TEST A are pulled through and copied into TEST B 1 & 2 which is great.

    Unfortunately when I try to implement it using a copy of the actual data that will be copied, it doesn't work. It runs for around 30 mins before giving a runtime error, highlighting the line below;

    Please Login or Register  to view this content.
    I can't seem to work out why this is happening, especially as the test run seems to work fine. Is anyone aware of anything that could be blocking it? Interestingly it seems to lock onto two folders, change their name slightly (adding some additional words) and places several empty folders with names of other projects (these have no relation to one another).

    Many thanks,

    Tom

  12. #12
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Copy matching files/folders (using unique ID) from one directory to another

    Does it work if you put the test folders in the same drive/location as the actual data?

  13. #13
    Registered User
    Join Date
    06-04-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Copy matching files/folders (using unique ID) from one directory to another

    Afternoon, yudlugar - apologies for taking so long to reply.

    It does work when using a test folders in each of the dummy data locations (providing the filepath of the test folders in the macro you provided). Do you think the problem is likely more to do with the filename following the 4 digit ID number? Apologies I cannot be of more help, although I expect I will learn quite a lot through (hopefully) resolving this....

  14. #14
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Copy matching files/folders (using unique ID) from one directory to another

    I'm pretty stumped at this. The only thing I can think of is if you are trying to create a folder with a folder name that already exists. I can't see how that would happen though as it should be putting everything in a new folder called closed down forms.

    When you get to having an error, try highlighting what is right of the equals sign on the line with the error (Dest_Folder & "\" & f.name), right click and then add watch. click ok on the dialog box and you should get a watch window that tells you the name of the folder you are trying to create. Have a look at this and see if there is any reason you couldn't make that folder (eg. the folder path is not valid, there is a weird symbol etc). See if you can manually create the folder VBA is trying to.

  15. #15
    Registered User
    Join Date
    06-04-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Copy matching files/folders (using unique ID) from one directory to another

    Hi yudlugar,

    I have done what you said and the error was 'Run-time error 76: Path not found'. When added to watch, the following is provided;

    Watch : : Error : "Path not found" : Variant/String : Module1.Copy_Folders

    It seems to take around 30-40 minutes to run before this happens, and no folders/files are copied or created in the target folder. Is there any way of making the search more specific, such as looking only at the first 4 characters in each folder?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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