i want to create a new file in a folder with existing files with names in a numerical order eg. 1, 2, 3, 4…
i want to check what is the last nr and then create a file with one nr over that one.
i know i should use file_exists but i don’t know exactly how to use it, in a for loop maybe? but how?
would be nice if someone could give me a hint
I think this is your best bet (see revisions for previous versions):
As long as your filenames in the folder start with numbers, this should always write the highest numbered filename incremented by one, e.g.
If there is a file not starting with a number, the above approach won’t work, because numbers are sorted before characters and thus nothing would be written for e.g.
You can get around this by changing the pattern for glob to
/path/[0-9]*, which would then only match files starting with a number. That should be pretty solid then.Note
natsortbehaves different on different OS. The above works fine on my Windows machine, but you will want to check the resulting sort order to get it working for your specific machine.See the manual for further info on how to use
glob(),natsort()andpathinfo();