Create unique SD filename automatically after reset, without extra hardware

I use an Arduino temperature and relative humidity logging package where I have to be able to disconnect the Arduino from a 9V power source to record the T and RH in various places. I wanted to be able to reset/restart the Arduino without losing any data already recorded on the SD card, and wanted to be able to do this without removing the SD card, and without using a real time clock hardware add-on or a connection to the outside world. Since I could not find any code to do this within this forum, or elsewhere on the internet, I wrote some filename creation code from scratch. I hope it might be of use to others. The attached zip file is the complete sketch, with plenty of comments.

Introduction within sketch is:

/*
Arduino temperature and relative humidty (DHT11) datalogger using SD card.

Each time the Arduino is reset/powered up, a new data log file is created with a
unique filename, leaving all other data log files intact on the SD card. To create the
unique filename, no extra hardware such as a real time clock or other connection
to the outside world is needed.

The code to create each unique filename does not use C language pointers, keeping
code easy to understand and adapt for other SD-related uses. The code also avoids using
any type of random number as the basis for the filename. All files on the SD card are
sequence numbered and thus are chronological.

Any suitable SD card can be used - no "initialisation" of a formatted SD card is necessary.

The hardware:
Arduino + Ethernet shield with SD card slot
DHT11 temperature and relative humidity sensor and 4k7 res
(resistor connected between DHT11 pins 1 (Vcc) and 2 (Data)
Green and red LEDs and 2x330R res

trhloggr.zip (3.82 KB)

you could use a numbered filename like 000000.txt, and on startup look for the highest numbered file, increment it by one, create and open

OR, you can have a file (number.txt) in which the first free number is mentioned.