12-15-2022 18:21
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

12-15-2022 18:21
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hello! I am trying to get the size of a file created by my app. Below is my code:
const filePath = '/private/data/' + my_file_name;
const stats = fs.statSync(filePath)
console.log(`${new Date()} app: stats.size = ${stats.size};`);
However, I keep getting an error saying "Error: Couldn't stat file: /private/data/my_file_name".
Am I missing something?

12-15-2022 20:51
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


12-15-2022 21:31
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


12-15-2022 21:31
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
According to this, you may not need to specify the filename.
Presumably the file has no extension.
You can list the files in a directory to see what's there.
Gondwana Software

12-15-2022 21:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


12-16-2022 10:18
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

12-16-2022 10:18
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Thank you Peter!
I first created a file for append, then listed all files. After a file is listed, I check whether the file exists, and the output is false. Code snippets:
I created created a file to append
this.logFile = fs.openSync('1671213445052.bin', 'a') // e.g 1671213445052.bin
When I listed all files in /private/data/:
listFiles() {
let iterator
let dir = fs.listDirSync('/private/data')
while ((iterator = dir.next()) && !iterator.done) {
const fileName = iterator.value
console.log(`app: fileName = ${fileName}. ${fileName} exists? ${fs.existsSync(fileName)}; ${'/private/data/'+fileName} exists? ${fs.existsSync('/private/data/' + fileName)}`);
try {
const stats = fs.statSync(fileName)
console.log(`${new Date()} app: stats.size = ${stats.size}`);
} catch (error) {
console.log(`${new Date()} app: stats error = ${error}`);
}
try {
const statsFullPath = fs.statSync('/private/data/' + fileName)
console.log(`${new Date()} app: statsFullPath.size = ${statsFullPath.size}`);
} catch (error) {
console.log(`${new Date()} app: statsFullPath error = ${error}`);
}
}
}
Oddly, the log prints
app: fileName = 1671213445052.bin. 1671213902043-ceda.bin exists? false; /private/data/1671213902043-ceda.bin exists? false
app: stats error = Error: Couldn't stat file: 1671213902043-ceda.bin"
app: statsFullPath error = Error: Couldn't stat file: /private/data/1671213902043-ceda.bin"
Why does the app think the file does not exist after it lists the file?

12-16-2022 10:20
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

12-16-2022 10:39
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


12-16-2022 10:39
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
@barbossa- the only thing that might have a bearing, did you close the file before trying to list or get information about it?
It is weird that the dir reports once fileName = 1671213445052.bin. then immediately fileName = 1671213902043-ceda.bin, without an iteration of the loop. Don't know how that is possible.
Author | ch, passion for improvement.

12-16-2022 11:08
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


12-16-2022 11:08
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Random thought: if you run it in the Fitbit Sim, you can browse the app's storage within your OS's file system. On Windows, it's something like C:\Users\Peter\AppData\Roaming\Fitbit OS Simulator\atlas\app\apps.
Gondwana Software

12-16-2022 11:15
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

12-16-2022 11:15
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Oh I did not close the file before checking whether it exists or its stats!
After calling closeSync on the file, I am able to get the stats. Thank you!
12-16-2022 11:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

12-16-2022 11:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Thank you Peter! Turns out I need to call closeSync before getting stats. Surprisingly, for a file to be listed (listDirSync), we do not need to call closeSync

12-16-2022 11:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


12-16-2022 11:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
I just tried your code. Even without closing the file, I was able to get stats (0 length). I wonder if other stuff might be going on too...
console.log() seems to silently drop output if too much. I split the long console.log() line into two, and magically it displayed correctly whereas previously it displayed nothing.
I suspect some of the output is a result of previous attempts, which have resulted in other files being present. This shouldn't matter by could explain the strange filenames (especially in conjunction with console.log misbehaving). If you uninstall and reinstall, the previous files should be deleted.
Gondwana Software
12-16-2022 12:55
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


12-16-2022 12:55
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Perhaps I saw different behaviour because I only tested on sim.
Gondwana Software

