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?
Best Answer
Fitbit Product Experts Alumni are retired members of the Fitbit Product Expert Program. Learn more
Fitbit Product Experts Alumni are retired members of the Fitbit Product Expert Program. Learn more
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?
Best Answer
Fitbit Product Experts Alumni are retired members of the Fitbit Product Expert Program. Learn more
@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.
Best AnswerRandom 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.
Best AnswerThank 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
Best AnswerI 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.
Perhaps I saw different behaviour because I only tested on sim.
Best Answer