7 Replies Latest reply on Jan 7, 2016 10:28 AM by Intel_Peter

    create new files

    KimLorentz

      How do I create a new file everytime I ask to start logging data to the sd card and close when asked to stop logging?

       

      my current code recreate the file everytime and all data that was there is gone.

      // Create .txt file
      var fs = require('fs');
      var ws = fs.createWriteStream('/media/sdcard/GPS.txt');
      
        • 1. Re: create new files
          Bylos

          Hi,

          It seems that fs.createWriteStream() opens the file with write-only access by default ('w')

          you should try to specify the append flag 'a' in the options to be able to add data at the end of your log file :

          var fs = require('fs'); 
          var ws = fs.createWriteStream('/media/sdcard/GPS.txt', {'flags': 'a'});
          

           

          File System Node.js v5.3.0 Manual & Documentation

          http://stackoverflow.com/questions/3459476/how-to-append-to-a-file-in-node

           

          Regards,

          • 2. Re: create new files
            KimLorentz

            I tryed the flags: a command and it is working, tnx.

            Now I only need it to start a new file everytime I press a button, will try to read the links but I am a noob at programming

             

            I was thinking gps_DATE_NR.txt (DD/MM/YY)

            For example gps_010216_01.txt

            • 3. Re: create new files
              Intel_Peter

              Hello KimLorentz,

               

              This script might also be of help for you:

               

              Edison/NewFilePerDate.js at master · MakersTeam/Edison · GitHub

               

              Starting a new file every time you press a button shouldn't be that hard. You will have to set an interruption to stop any process that is running at that time and then capture the data you need in the new file. The mraa library supports interruptions, this is its official example on JavaScript:

               

              mraa/isr.js at master · intel-iot-devkit/mraa · GitHub

               

              Peter.

              • 4. Re: create new files
                KimLorentz

                I tryed using the code. But everytime I restart the javascript it only restart logging on the same file, it does not start a new one.

                 

                var fs = require('fs');
                var dat = new Date();
                var day = dat.getDate();
                var month = dat.getMonth() + 1;
                var year = dat.getFullYear();
                var fn = "";
                var files = fs.readdirSync(__dirname);
                var fillen = files.length - 1;
                var ac = [];
                var rd = day + "_"  + month + "_" + year;
                fn = rd + "_1";
                var bc = fn.split('');
                var gen = false;
                var mf = 0;
                for(var c = 0; c < fillen; c++)
                {
                  if(gen == true)
                  {
                  if(mf < ac[bc.length-1])
                  {
                  mf = parseInt(ac[(bc.length - 1)]);
                  gen = false;
                  }
                  }
                  ac = files[c].split('');
                  for(var c1 = 0; c1 < (bc.length - 1); c1++)
                  {
                  if(ac.length != bc.length)
                  break;
                  if(ac[c1] != bc[c1])
                  break;
                  else 
                  {
                  if(ac[bc.length-2] == bc[(bc.length - 2)])
                  {
                  gen = true;
                  break;
                  }
                  }
                  }
                }
                var fnam = rd + "_" + (mf+1);
                var ws = fs.createWriteStream('/media/sdcard/' + fnam + '.txt');
                console.log(fnam);
                
                
                //ws.end();
                
                
                setInterval(function()
                {
                    console.log('Write to sd card');
                    console.log(fnam);
                    ws.write('test' + '\n');
                
                
                }, 500);
                
                • 5. Re: create new files
                  Intel_Peter

                  Hi KimLorentz,

                   

                  I found the issue in the code and updated the source, there was an "=" missing on line 28 (15 in your post). It should work without issue now. The issue was that the script would not work unless there was another file in the directory, it should be resolved now but let me know.

                   

                  Peter.

                  • 6. Re: create new files
                    KimLorentz

                    I tryed the new code, still not working.

                     

                    I want to save to sd card on media and not on Edison

                     

                    var fs = require('fs');
                    var dat = new Date();
                    var day = dat.getDate();
                    var month = dat.getMonth() + 1;
                    var year = dat.getFullYear();
                    var fn = "";
                    var files = fs.readdirSync(__dirname);
                    var fillen = files.length - 1;
                    var ac = [];
                    var rd = day + "_"  + month + "_" + year;
                    fn = rd + "_1";
                    var bc = fn.split('');
                    var gen = false;
                    var mf = 0;
                    for(var c = 0; c <= fillen; c++)
                    {
                        if(gen == true)
                            if(mf < ac[bc.length-1])
                            {
                                mf = parseInt(ac[(bc.length - 1)]);
                                gen = false;
                            }
                        ac = files[c].split('');
                        for(var c1 = 0; c1 < (bc.length - 1); c1++)
                        {
                            if(ac.length != bc.length)
                                break;
                            if(ac[c1] != bc[c1])
                                break;
                            else
                                if(ac[bc.length-2] == bc[(bc.length - 2)])
                                {
                                    gen = true;
                                    break;
                                }
                        }
                    }
                    var fnam = '/media/sdcard/'+ rd + "_" + (mf+1) + '.txt';
                    var ws = fs.createWriteStream(fnam);
                    //ws.write('test' + '/n');
                    //console.log(fnam);
                    //ws.end();
                    
                    
                    setInterval(function()
                    {
                        console.log('Write to sd card');
                        console.log(fnam);
                        ws.write('test' + '\n');
                    
                    
                    }, 500);
                    
                    • 7. Re: create new files
                      Intel_Peter

                      Have you tried running the script from the SD card? The script creates the new files in the directory it is stored, the line you modified, 38 in your last post, won't change the place where it is stored, it would only modify the name of the file.

                       

                      I noticed you added some lines, what do you want to achieve with that modification? I might be able to help you.

                       

                      Peter.