I'm setting up a Script Tool (Arc 10.2.2) where the first parameter will be a CSV file chosen by the user, always from the same folder.
When the user clicks the first parameter's windows navigator drop down (to select the desired .csv) I want the ensuing catalog window to begin at the folder I specify.
I've already tried setting the folder path in the "Default" field of the Parameter Properties Window but that didn't work. This made the path appear in the Parameter field, but when the drop down is clicked that path is ignored and the user is shown an entirely different location.
I'm quite sure the proper validation code will achieve my desired functionality, I just don't know what that is. I have read many ESRI web pages on the topic but have yet to find the proper coding.
How do you force a Tool Parameter Input Field to Begin at a Specific Folder?
I seek the proper function and which module it should be in (i.e. which of the standard 3 validation modules: _init_(self)
, initializeParameters(self)
, or updateParameters(self)
.).
1 Answer 1
Since the user will always select a file from the same folder, then you don't need to have the file browser involved. This solution won't set a default directory; instead, it will populate a drop-down list of all the csv files that are within your desired directory.
In the tool's properties, instead of setting the parameter type to File, set it to String.
Then, in the validation code:
def initializeParameters(self):
dir_f = r'path\to\csv\directory'
fs = [os.path.join(dir_f, f) for f in os.listdir(dir_f) if f.endswith('.csv')]
self.params[0].filter.list = fs
return
Alternatively, you could display just the file names rather than their full paths, but when the tool executes, you would need to be sure to prepend the directory path.
-
Beautiful. Achieves exactly what I need. Much appreciated!Waterman– Waterman2018年05月16日 20:54:46 +00:00Commented May 16, 2018 at 20:54
Explore related questions
See similar questions with these tags.
ListFiles
ininitializeParameters(self)
will do what you need. For ArcPy questions please always include a code snippet that shows what you have tried.