title: ESP8266 SPIFFS copy content of file, delete & rename
body: <p>I have a problem where I copy everything but the first line from file to another temporary file. Then I delete the original file and rename the temporary one to original. After every cycle I also append a line of data to the original file. The code works fine for the first time it runs, then content dissapears and only 1 line is in the original file.</p>
  
  <p><b>Original file:</b><br>
  line 1: aaa<br> 
  line 2: bbb<br>
  line 3: ccc<br>
  line 4: ddd<br>
  line 5: eee<br></p>
  
  <p>After I run the function for the first time:</p>
  
  <p><b>First cycle:</b><br>
  line 1: bbb<br>
  line 2: ccc<br>
  line 3: ddd<br>
  line 4: eee<br>
  line 5: fff<br></p>
  
  <p><b>After second cycle:</b><br>
  line 1: ggg<br></p>
  
  <p><b>Update:</b><br>
  Looks like that when I rename the file and open it with spiffs, esp doesn't see the content of the file so it just writes the line that gets added after the function ends.</p>
  
  <p><b>Update2:</b><br>
  Even if i comment out my function that adds lines to the file, the deleteFirstLine function doesn't work. I tried using the function on a file with 5 lines in it. Same result, first cycle is okay, then the second one nothing is in the file...</p>
  
  <p>Code that I use in arduino:</p>
  
  <pre><code>void deleteFirstLine(String filename){
      File original = SPIFFS.open(filename, "r");
      String name_ = original.name();
      Serial.println(name_);
      if (!original) {
        Serial.print("- failed to open file "); Serial.println(filename);
      }else{
        Serial.print("- DELETING FROM FILE "); Serial.println(filename);
        //We skip the first line
       original.readStringUntil('\n');
       File temporary = SPIFFS.open(TEMP_PATH, "w+");
       if(!temporary){
        Serial.println("-- failed to open temporary file "); 
       }else{
        while(original.available()){
          temporary.print(original.readStringUntil('\n')+"\n");
        }
        temporary.close(); 
       }
       original.close();    
  
       if(DEBUG == 1){   
           if(SPIFFS.remove(filename)){
              Serial.println("Old file succesfully deleted");
           }else{
              Serial.println("Couldn't delete file");
           }
           if(SPIFFS.rename(TEMP_PATH,filename)){
              Serial.println("Succesfully renamed");
           }else{
              Serial.println("Couldn't rename file");
           } 
        }else{
          SPIFFS.remove(filename);
          SPIFFS.rename(TEMP_PATH,filename);
        }
      } 
  }
  </code></pre>