In bestehende Datei schreiben mit Trigger

61 views
Skip to first unread message

Hans Zahnd

unread,
Aug 23, 2018, 4:46:25 AM8/23/18
to mementodatabase
Hallo zusammen,

ich habe eine Frage, wie ich in eine Datei schreibe habe ich schon herausgefunden aber
wie kann ich an einer bestehenden Datei nach der letzten Zeile anhängen und nicht immer die alte Datei überschreiben ?
(mit Trigger von Memento Database)

 var entries = lib().entries();
if (entries.length > 0) {
   text1 = entries[0].field("Text 1");
   text2 = entries[0].field("Text 2");
   text3 = entries[0].field("Text 3");  
   text4 = entries[0].field("Dateiname");
   datum = moment().format("dddd_MMMM_Do_YYYY_h:mm:ss");
}

f = file("/storage/emulated/0/Download/" + datum +".txt", "a"); // Open myfile.txt on the SD card
                                // If no file, it will be created
f.writeLine(text1);             // Write "one" as a line to the file
f.writeLine(text2);
f.writeLine(text3);
f.close();                      // Close & save. Until closed,
                                //   the file is still empty
var a = f.readLines();          // Read all lines into array



Besten Dank

Hans

Bill Crews

unread,
Aug 23, 2018, 9:14:38 AM8/23/18
to Hans Zahnd, mementodatabase
The only way I can think of would be to copy the existing file in the script before you add your new lines to it, and without closing it, write your new lines to it, like this (after opening the file) ...

var txt = f.readLines();
for (var x = 0; x < txt.length; x++)
   writeLine(txt[x]);

// Then your new lines
   

Hans Zahnd

unread,
Aug 23, 2018, 11:10:44 AM8/23/18
to mementodatabase
Hallo Bill 

Besten Dank für deine Antwort leider geht es noch nicht es kommt eine Fehlermeldung mein Code sieht folgendermaßen aus:

Screenshot_2018-08-23_170340.jpg

var entries = lib().entries();
if (entries.length > 0) {
   text1 = entries[0].field("Text 1");
   text2 = entries[0].field("Text 2");
   text3 = entries[0].field("Text 3");  
   text4 = entries[0].field("Dateiname");
   datum = moment().format("dddd_MMMM_Do_YYYY_h:mm:ss");
}

f = file("/storage/emulated/0/Download/housi.txt", "a"); // Open myfile.txt on the SD card
var txt = f.readLines();
for (var x = 0; x < txt.length; x++)
   f.writeLine(txt[x]);                                // If no file, it will be created
f.writeLine(text1);             // Write "one" as a line to the file
f.writeLine(text2);
f.writeLine(text3);
f.close();                      // Close & save. Until closed,
                                //   the file is still empty

Bill Crews

unread,
Aug 23, 2018, 5:52:24 PM8/23/18
to Hans Zahnd, mementodatabase
I'm not sure which line is line 13, but the message was that the file is already open for writing only, so it didn't like the call to readLines(). You need to open it for reading and writing ("rw"?). According to the wiki, there's only one argument to file(). But I admit that the message suggests that it did pay attention to your second argument, so maybe you know something the wiki doesn't. How did you know about the second argument? I may have to update the wiki. In the meantime, try "rw".

Hans Zahnd

unread,
Aug 24, 2018, 3:27:52 AM8/24/18
to mementodatabase
Hallo

Ich habe jetzt rausgefunden wie es funktioniert das Argument hat kein Einfluss.
Zuerst muss die Datei gelesen werden dann geschlossen und anschließend  var txt = f.readLines(); und
dann wieder Datei öffnen ....

Das funktioniert mit einer vorhandenen Datei aber eine neue kann ich nicht erstellen.
Kann ich eine if Abfrage machen ob die Datei vorhanden ?

Hier mein Code

var entries = lib().entries();
if (entries.length > 0) {
   text1 = entries[0].field("Text 1");
   text2 = entries[0].field("Text 2");
   text3 = entries[0].field("Text 3");  
   text4 = entries[0].field("Dateiname");
   datum = moment().format("dddd_MMMM_Do_YYYY_h:mm:ss");
}

f = file("/storage/emulated/0/Download/" + text4 + ".txt");
f.close();
var txt = f.readLines();
f = file("/storage/emulated/0/Download/" + text4 + ".txt");  
for (var x = 0; x < txt.length; x++)    
    f.writeLine(txt[x]);                                
f.writeLine(text1);             
f.writeLine(text2);
f.writeLine(text3);
f.close(); 

Gruss Hans

Bill Crews

unread,
Aug 24, 2018, 9:22:23 AM8/24/18
to Hans Zahnd, mementodatabase
Wow! So, file() does, I guess, take only one argument. But doing a readLines() on a closed file? And then having it behave differently after reopening the file than it did during the first open?

Excellent sleuthing! I wonder if it's accidental (a bug) or a sign of something else we don't know.

Hans Zahnd

unread,
Aug 25, 2018, 3:32:41 AM8/25/18
to mementodatabase
Hallo 

kann man prüfen ob eine Datei vorhanden ist ? 
Mit einer if Abfrage im script.

Bill Crews

unread,
Aug 25, 2018, 8:01:29 AM8/25/18
to Hans Zahnd, mementodatabase
I know of no way to find out if a file exists or not without trying to open it, and since opening a filename causes a new file to be created if it doesn't exist, using it will ensure that one exists whether it previously existed or not.
Reply all
Reply to author
Forward
0 new messages