Set Folder Output for ArcGIS Pro Python Script Tool

1027
4
Jump to solution
07-07-2023 11:01 AM
shildebrand
Occasional Contributor

Hi everyone,

I created a script that runs successfully in the ArcGIS Pro python window, but I would like to re-create this as a script tool to eventually be used for a geoprocessing service.  I am having problems setting the output folder in the script tool.  What is the correct data type to use when setting up the tool parameters?  I've tried "Folder", "Workspace", and "String" with no success. (See below)

shildebrand_0-1688752772008.png

 

Basically the script copies files from a file directory based off a user selection and a hyperlink to the file using the "LINK" field.  Any suggestions would be greatly appreciated!

Here is the original python window script that runs successfully:

import arcpy
import os
import shutil
aprx = arcpy.mp.ArcGISProject("CURRENT")
# Change to Name of Working Map
map = aprx.listMaps("Map")[0]
# Change to the location of the plan ref layer in your map
fc = map.listLayers()[0]
print(fc)
cursor = arcpy.SearchCursor(fc)
for row in cursor:
field = "LINK"
link = (row.getValue(field))
head, tail = os.path.split(str(link))
print ("Downloading: " + tail)
# Set Download Location
dest_path = r"H:\Data Requests\ExportFolder"
shutil.copy(link, dest_path)

print("Download Complete")

Here is the script tool code:

import arcpy
import os
import shutil

fc = arcpy.GetParameterAsText(0)
print(fc)
cursor = arcpy.SearchCursor(fc)
for row in cursor:
field = "LINK"
link = (row.getValue(field))
head, tail = os.path.split(str(link))
print ("Downloading: " + tail)
# Set Download Location
dest_path = arcpy.GetParameterAsText(1)
shutil.copy(link, dest_path)

print("Download Complete")

0 Kudos
1 Solution

Accepted Solutions
ChrisRingo
Occasional Contributor

Probably either Folder or Workspace (or even String) would work for the parameter type for "out_folder", but you'll want to change the direction to "Input". Even though you'll be writing files to that folder, it's still an input parameter.

Also, just in the interest of more elegant coding, you should move the statement "dest_path = arcpy.GetParameterAsText(1)" up to the top and out of the loop - it only needs to be executed once.

View solution in original post

4 Replies
ChrisRingo
Occasional Contributor

It would help get responses if you could format your code to make it more readable: https://community.esri.com/t5/community-help-documents/how-to-insert-code-in-your-post/ta-p/914552

0 Kudos
shildebrand
Occasional Contributor

Thanks Chris for the suggestion - yes that is better!

Here is the original python window script that runs successfully:

import arcpy
import os
import shutil
aprx = arcpy.mp.ArcGISProject("CURRENT")
# Change to Name of Working Map
map = aprx.listMaps("Map")[0]
# Change to the location of the plan ref layer in your map
fc = map.listLayers()[0]
print(fc)
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    field = "LINK"
    link = (row.getValue(field))
    head, tail = os.path.split(str(link))
    print ("Downloading: " + tail)
    # Set Download Location
    dest_path = r"H:\Data Requests\ExportFolder"
    shutil.copy(link, dest_path)

print("Download Complete")

 

Here is the script tool code:

import arcpy
import os
import shutil

fc = arcpy.GetParameterAsText(0)
print(fc)
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    field = "LINK"
    link = (row.getValue(field))
    head, tail = os.path.split(str(link))
    print ("Downloading: " + tail)
    # Set Download Location
    dest_path = arcpy.GetParameterAsText(1)
    shutil.copy(link, dest_path)

print("Download Complete")

 

0 Kudos
ChrisRingo
Occasional Contributor

Probably either Folder or Workspace (or even String) would work for the parameter type for "out_folder", but you'll want to change the direction to "Input". Even though you'll be writing files to that folder, it's still an input parameter.

Also, just in the interest of more elegant coding, you should move the statement "dest_path = arcpy.GetParameterAsText(1)" up to the top and out of the loop - it only needs to be executed once.

shildebrand
Occasional Contributor

Thank you!  Switching the folder to an input parameter did the trick!

0 Kudos