I have a simple tool which outputs a file. I would like the filename to be a number which increments every time the tool is run.
How would I go about implementing this using the toolbox in ArcMap 10?
Assuming your tool is always outputting the output file in the same directory, this function will return the next highest-numbered file name:
import os
def getNextFilePath(output_folder):
highest_num = 0
for f in os.listdir(output_folder):
if os.path.isfile(os.path.join(output_folder, f)):
file_name = os.path.splitext(f)[0]
try:
file_num = int(file_name)
if file_num > highest_num:
highest_num = file_num
except ValueError:
'The file name "%s" is not an integer. Skipping' % file_name
output_file = os.path.join(output_folder, str(highest_num + 1))
return output_file
To get the next output file's path, simply print the function:
print getNextFilePath(<YOUR_OUTPUT_PATH>)
You could use this in your tool's ToolValidator class to generate this as default "output file" value, or you could include it in the tool's code itself, if you want the tool to simply accept an output folder for the generated file.