Great, I was just looking for some "file exists" code ;-)
The suggestions work OK for filenames without extension like "ABC". But I want to check for file "news.txt" and all variants report "false/missing".
This is my code
s = require('Storage');
file = { name : 'news.txt', exist : 0};
print(s.list());
// variant I
if (s.list().find(e => (e == file.name))) {
file.exist = 1;
console.log("I",file.name,"exists");
} else {
file.exist = 1;
console.log("I",file.name,"is missing ");
}
// variant II
if (s.read(file.name)) {
file.exist = 1;
console.log("II",file.name,"exists");
} else {
file.exist = 1;
console.log("II",file.name,"is missing ");
}
// variant III
print("III",s.list(/^file.name/));
// variant IV
print("IV",s.read(file.name)!==undefined);
// variant V
print("V",s.read(file.name));
Output is
`
[
"news.txt\1",
"weath0.txt\1",
"weath1.txt\1",
"stocks.txt\1",
"messages.txt\1",
"ABC",
"news"
]
I news.txt is missing
II news.txt is missing
III [ ]
IV false
V undefined
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Great, I was just looking for some "file exists" code ;-)
The suggestions work OK for filenames without extension like "ABC". But I want to check for file "news.txt" and all variants report "false/missing".
This is my code
Output is
`