Substitute %d in given URL (UrlPattern) with successive numbers, until the resulting URL (which can be just a filename) doesn't exist.
We start with number = 0 and do Url := Format(UrlPattern, [Number]) , until we find non-existing URL. Example UrlPattern is 'screenshot_%d.png' , by saving to this UrlPattern you're relatively sure that each save goes to a new file.
Since we use standard Format function, you can use any Format placeholder syntax. E.g. use 'screenshot_%.04d.png' to have a number padded with zeros, with results like 'screenshot_0005.png' .
Note that it's possible on every OS that some other program, or a second copy of your own program, will write to the resulting URL between FileNameAutoInc determined it doesn't exist and you opened the file. So using this cannot guarantee that you really always write to a new file – as always, be prepared for anything happening in parallel when dealing with a filesystem.
The overloaded version with separate UrlPrefix and UrlSuffixWithPattern replaces %d in UrlSuffixWithPattern, and then glues (unmodified) UrlPrefix with (processed) UrlSuffixWithPattern. This is useful when you have a user-specified path, where you don't want to perform %d substitution. Alternative is to wrap the string in SUnformattable.
Example usage to save a screenshot:
ScreenshotUrl := FileNameAutoInc(SaveScreenPath, 'screenshot_%d.png');
Note that to save a screenshot in the most cross-platform way possible, we advise using Window.Container.SaveScreenToDefaultFile instead, it will use SaveScreenPath or more elebarate mechanism to work on all platforms.
Example usage to save anything else to user config:
SaveSomethingUrl := FileNameAutoInc(ApplicationConfig('save_something_%d.png'));
Note that it will be replaced soon by SaveSomethingUrl := FileNameAutoInc('castle-config:/', 'save_something_%d.png') which also deals with the (unlikely, but still) possibility that ApplicationConfig will contain a percent sign.
|