7

I have a single folder, let's call it Folder1, which contains multiple folders; FolderA, FolderB, FolderC.....FolderZ.

Folders A-Z each contain multiple file geodatabases. Folders A-Z do not contain any other file type or any other folders.

I need to copy all of the file geodatabases which are in folders A - Z into a single folder elsewhere on my local drive.

I am trying to do this in ModelBuilder as I have very little experience of python.

I thought that I would be able to easily achieve this by using a model (attached image "MainModel")and a submodel ("SubModel")

My main model has a folder variable set as the input parameter (this would be Folder1). This feeds into an Iterate Workspaces iterator. I have set this to only look for folders (here I want it to return FolderA, FolderB, FolderC....FolderZ).

MainModel

I use the output workspace from here to feed into my submodel as a parameter.

The submodel has a folder parameter as the input (this is the "Workspace" from MainModel, in theory this should be FolderA, FolderB etc....). This then feeds into an iterate workspaces iterator and looks for FileGDB's. The "Worksapce" output then feeds into the copy tool which copies the geodatabase into a predefined folder on my local drive.

SubModel

I can run the SubModel on its own and it works fine, i.e. I can point it to Folder A and it will copy all of the file geodatabases from FolderA into another folder. The problem arises when I try and work from the MainModel.

In MainModel, I can connect the "Workspace" output into the SubModel and it let's me select the "Folder" variable for this. However, the model doesn't fill with colour, and if I open the tool parameter window I see the error (attached "Error840" file).

Error840

I've tried changing the variables in each model to be folders or workspaces or a combination, and I've tried setting the iterators to look only for folders or only for GDB's or I've even left the "Workspace type" field blank. But nothing seems to work.

The only thing I've managed to do where I've had partial success is using a single model (attached "AlternateModel"). This uses the recursive option in the iterator properties and has Folder1 as the input Folder variable parameter.

AlternateModel

This copies all the geodatabases as required from all the folder (A-Z), however, it also copies all the folders (A-Z).

Is there something simple which I am missing?

7

Looks like you have found a bug in ModelBuilder, but fortunately there is a solution!

In your master model insert a calculate value tool, Workspace is a precondition to it. In the Calculate tool type the expression as shown below and make sure output data type is set to folder. You can then connect that to your sub-model, so it all works.

Using Calculate Value to change data type

6

I created what I believe to be the functioning standalone python script to do this. Here is the test file structure I created:

Testing folder structure

Here is the python script to copy each FGDB from the parent and subsequent folders C:\Folder1 to another directory I created called C:\Folder1Copy

import arcpy
import os

#Set parent directory
arcpy.env.workspace = r'C:\Folder1'

#List the subfolders in the parent directory 
workspaces = arcpy.ListWorkspaces("*", "Folder")

for workspace in workspaces:
    #Set new environment to list workspaces
    arcpy.env.workspace = workspace
    fgdbs = arcpy.ListWorkspaces("*", "FileGDB")

    #Iterate through each FileGDB contained within and copy to destination 
    for fgdb in fgdbs:
        #parsing the name from the full path of each FGDB
        #name = os.path.basename(fgdb).rstrip(os.path.splitext(fgdb)[1])
        name = os.path.basename(fgdb)[:-4]
        arcpy.Copy_management(fgdb, "C:/Folder1Copy/{0}.gdb".format(name))

Here was the final output:

Copied FGDBs in new directory

  • OP asked for a ModelBuilder solution, but good to have a Python solution as well. My first instinct would have been to use arpcy.da.Walk, but I have found it to be quite inconsistent, so +1 for use of ListWorkspaces. – Cindy Jayakumar Sep 18 '13 at 5:16
  • Many thanks for the python script, this would in fact be a much neater way to do what I'm trying to do. I've tried your script but it doesn't seem to fully work. I have a test folder with 2 folders which each contain GDB's. The script only seems to copy the GDB's from one of these folder. I've added some print statements into your script to look at the lists you're generating. The 1st list "workspaces" is returning all the folders, however, the 2nd list "fgdbs" is only returning the fgdb's from the 2nd folder. Any ideas? – Dan_h_b Sep 18 '13 at 9:01
  • I'm not a python guru but I believe its the line that defines the variable name that is causing the problem. I have commented out the line and placed one in that works. – Hornbydd Sep 18 '13 at 12:07
  • Great suggestions! As far as I can tell, parsing the name variable works with my method and @Hornbydd method (although I am no expert and used code from the esri resource centre - Parse Path). OP, I ran into the same scenario when the fgdb's from one workspace were copied but the others were not. The problem was that the code does not check for duplicate names. If two fgdb's in separate folders have the same names, the first will be copied and the other will not be. Make unique fgdb names for all and try again. – Adam Sep 18 '13 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.