2

Having some issues getting the arcpy.Copy_management to work. Essentially I simply would like to be able to have a user select a folder which contains multiple shapefiles and copy them to another user selected fodler without hard coding the locations and filenames. Both folders would be obtained by the GetParametersAsText() function.

At this stage all I want to do is get the copy code correct and make a copy of each of the datasets in the dataset folder. I may need to check for differing things such as spatialReference but for now getting the copy code sorted would be great!

I have set my workspace to the dataset directory using:

arcpy.env.workspace = datasetFolder

datasetFolder = arcpy.GetParameterAsText(0)
outputFolder = arcpy.GetParameterAsText(1)

The datasetFolder contains 3 shapefiles and I'm a bit lost on the parameters to use to copy each of them to the outputFolder directory. It surely isn't as simple as:

arcpy.Copy_management(datasetFolder, outputFolder)

All the files have been put into a list called inputFC.

Should the above Copy_management code work or is there a particular way to code the copy line so each files gets copied over?

  • What data types (e.g. string, workspace, etc) did you specify for the input and output parameters in the script tool wizard? – Aaron Apr 17 '16 at 8:36
  • What happens when you run your four lines of code choosing test input and output folders? – PolyGeo Apr 17 '16 at 8:36
  • @aaron this could be my issue. I have it set as folder for both. Would you suggest for a selection of random gis data files? Boundaries etc... – Dave.m Apr 17 '16 at 8:39
  • @Polygeo at this stage it copies the first one over but when it gets to the 2nd file if seems ot try and make a file called "datasetFolder" – Dave.m Apr 17 '16 at 8:41
  • @Midavalo thanks I'll give it a go! Could you explain the following portion of the code for me please: (fc, "{}\\{}".format(outputFolder, fc) – Dave.m Apr 17 '16 at 9:03
2

If you want to make a copy of everything in one folder as another folder using ArcPy then this should work:

import arcpy

datasetFolder = arcpy.GetParameterAsText(0)
outputFolder = arcpy.GetParameterAsText(1)

arcpy.Copy_management(datasetFolder,outputFolder)
2

Use arcpy.ListFeatureClasses() to get a list of shapefiles in your datasetFolder and then loop through this list writing each one to your outputFolder, something like this:

import arcpy

datasetFolder = arcpy.GetParameterAsText(0)
outputFolder = arcpy.GetParameterAsText(1)

arcpy.env.workspace = datasetFolder

featureClasses = arcpy.ListFeatureClasses()

for fc in featureClasses:
    arcpy.Copy_management(fc, "{}\\{}".format(outputFolder, fc))

See List Feature Classes - ArcGIS Desktop Help.

1

You can also use the shutil.copytree method as a free and open source solution. Using this method will allow you to copy anything (i.e. vector data, raster data, file geodatabases, etc) from source to destination and avoid a for loop, which can be computationally expensive. Note that the raw_input() functions replace arcpy.GetParameterAsText(0) to provide a level of user input.

import shutil, os

inws = raw_input("Enter the path of your input folder: ") # e.g. C:\temp\my_shps
outws = raw_input("Enter the path of your output folder: ") # e.g. C:\temp

# Copy a folder shutil method
shutil.copytree(inws, os.path.join(outws, 'copied_folder'))
  • Thanks for the info mate. I haven't come across shutil.copytree before. I've got my issue sorted now but will look into your suggestion also for future reference. – Dave.m Apr 17 '16 at 12:14
  • 1
    If you are copying everything in a folder why not just do that using ArcPy's Copy instead of importing an additional module? arcpy.Copy_management(inws, os.path.join(outws, out_folder_name)) – PolyGeo Apr 17 '16 at 21:31
  • Good point @PolyGeo. I guess I'm always trying to port things over to FOSS, although in the previous example I still had arcpy.GetParameterAsText() functions included. I edited the post to include a true FOSS solution and removed the arcpy module altogether. – Aaron Apr 17 '16 at 22:25

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.