[SOLVED] Arduino Yún: Sketch to create unique filename

Hallo all,

I'm trying to do something that seems to me as rather simple, but I'm struggling to make it work.
The sketch should write a file to the Linux machine, where it will be handled by a Python script that will delete the file when it's done. To prevent the file to be overwritten every now and than, I want to generate a rather unique filename using for instance the millis() function. This turns over ever 50 days or so, and this should be more than enough.

What I tried to do is something like this:

void SendToPython(){
  Process p;

  char t[] = {millis()};  // Should be used to make the filename unique, 
                          // by replacing 'TestFile' with this number.

  char f[] = {"/SPOOL/INPUT/msg_TestFile.txt"};

  // Create a new file
  Serial.println("Creating file");
  File dataFile = FileSystem.open(f , FILE_WRITE);
  if (dataFile) {
      dataFile.print("Write some string to the file");
    }
    dataFile.close();
  }  
  else { // if the file isn't open, pop up an error:
    Serial.println("Error opening messagefile in '/SPOOL/INPUT/'");
  } 

  // Ensure the last bit of data is sent.
  Serial.flush();
  Serial.println("__________________________end message____________________________ ");
}

As long as I create the file with the char-string as I do above, all is fine.
When I try to insert the value of millis() into the string, in all possible ways I could imagine, I keep getting conversion errors.

How could I handle this the best please?

Friendly greetings,
Rens

Posting code that doesn't do what you want and doesn't produce an error does nothing. Without an error it is hard to imagine what "all possible ways I could imagine" would be.

Make the simplest example that produces a conversion error and you think should give you what you want and someone may be able to help.

You could use sprintf when to create the file name.
For example

char newfile[50];
sprintf(newfile, "/mnt/sd/File%04d.txt", millis());

Or use an increasing number like this:

http://forum.arduino.cc/index.php?topic=204750.msg1507809#msg1507809

Erni:
You could use sprintf when to create the file name.
For example

char newfile[50];

sprintf(newfile, "/mnt/sd/File%04d.txt", millis());

This did the trick for me.
Thank you very much. :slight_smile:

And a liiiiitle bit better:

char newfile[50];
sprintf(newfile, "/mnt/sd/File%04u.txt", millis());

Otherwise I get a filename like "File-8723.txt" every now and then.