Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to get stats of a file?

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
0 Votes
11 REPLIES 11

Hi @barbossa - try adding checking if the file exists first. That way you will know if there is a path or file name error.

Author | ch, passion for improvement.

Best Answer
0 Votes

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.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

@barbossa - also check you have the FS import declared correctly. It may not be the issue but there were some changes with it for some SDK versions.

Author | ch, passion for improvement.

Best Answer
0 Votes

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
0 Votes

Hi @Guy_ ! I was able to use FS to create the file to append to. So I think FS was imported correctly. Please let me know if you something wrong in my code. Thanks! 

Best Answer
0 Votes

@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 Answer
0 Votes

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.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

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! 

Best Answer

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 

Best Answer
0 Votes

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.

Peter McLennan
Gondwana Software
Best Answer

Perhaps I saw different behaviour because I only tested on sim.

Peter McLennan
Gondwana Software
Best Answer
0 Votes