How to check if a 'Storage ' entry exists? #1427
Replies: 20 comments
-
Posted at 2020-07-29 by @gfwilliams Or a 3rd is However I'd say |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-07 by gerardwr 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
` |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-09 by gerardwr This works for me. It's based on the fact that
The output is: [ |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-09 by @MaBecker Thanks for sharing. This is what I decided to use for my projects:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-09 by gerardwr OK, thanks, but all of my files have a .txt extension, and those are not recognised by your code :(
Output is:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-09 by @fanoush there are two storage file apis/types of files, the 'more advanced' ones are done via basic ones by appending numbered suffixes Storage.read matches Storage.write, then there is StorageFile created by Storage.open, those are different. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-09 by @MaBecker Correct, because searching with Using StorageFile will add chunk numbering. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-09 by gerardwr Fair enough @MaBecker but
Output is:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-09 by gerardwr When I check for files without an added chunk number (f.i. file ABC.txt) it is recognised perfectly with your code, output is:
As @fanoush explained there are 2 methods to store a file, I happened to use the files with the added chunk number. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-09 by gerardwr Tested my code once more to verify that both
Output is:
The mix of the 2 storage versions does not feel like a blessing anymore :) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-17 by @gfwilliams The main issue here is the two distinct file types. If you want to check two you could do:
But I guess I wonder what you're trying to do here? If you are writing the code you presumably know which type of file you're trying to create, so you'd know what to check for? Given the issues around StorageFile I may end up ignoring them in |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-17 by gerardwr When I started using Espruino a few weeks ago, I was "fresh" re. Espruino. I found the extensive description really confusing. So I just picked one. Now being a somewhat "less fresh" user I still wonder why there are 2 different file types. I read and reread the documentation, and still have no idea why the differences. It's probably me ..... Just making sure, my posts were not meant as a gripe, but an attempt to add to the discusion that @MaBecker kicked of. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-18 by @fanoush
The 'more advanced' one is done via basic one. The basic one has less overhead but cannot append to existing file. See also https://www.espruino.com/Reference#StorageFile
Maybe there should be Storage.list and StorageFile.list? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-18 by @MaBecker
Good point, what about having an option for this? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-18 by @gfwilliams I'll update the reference, but as @fanoush says, StorageFile is built on top of the standard storage functionality, which makes it harder to separate the two. Normal
The issue is |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-18 by @fanoush
oh, you can't have static method of StorageFile type without having any instance? like there is Date.now() vs Date().getHour() |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-18 by gerardwr Thanks for the extensive explanation, sounds like a perfect addition for the reference. It's clear for me now. Coming from other languages it feels "natural" to start with an "open" command, so that's the reason why I chose This says it all!
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-19 by @gfwilliams
Well, you can, but I guess there's a lot to be said for just making |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-19 by @fanoush OK, I am not JS guru, I looked at it from c++/java/c# point of view and was thinking it is related to StorageFile so can be static method there but true that it does not fit there 100% too. Or it may if also open() would move there so Storage and StorageFile would become logically separated APIs - you would use StorageFile.list and StorageFile.open returning new instance not knowing about Storage API at all. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-08-19 by @gfwilliams
Yes, that would definitely be the tidiest solution. Then it's just how to make it backwards-compatible enough that it didn't cause problems. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2020-07-29 by @MaBecker
Running some test and figured out that there are at least two possibilities to check if a file/entry exists
Are there any pros and cons for those variants?
Beta Was this translation helpful? Give feedback.
All reactions