Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 ESRI ArcMap 10?

Thank you for your time

share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

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.

share|improve this answer
3  
Nice one. I think it might need to be tweaked depending on how the files should be named and if the file extension needs to be included. I would change this function to just return the next highest number, and then have other functions to format the filename as required. –  blah238 Jun 13 '12 at 18:38
1  
Excellent, this will do the trick! –  Beau Bouchard Jun 13 '12 at 19:16
1  
I shared this with a few other grad students, Thank you so much again, great answer –  Beau Bouchard Jun 14 '12 at 17:19
    
You're welcome! I'm glad I could help. If I get a moment I might fine tune it. –  Tanner Jun 14 '12 at 17:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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