//Declare variables
var path = "Server";
var fs = require('fs');
var files = fs.readdirSync(path);
var filenames = [files.length];

//Remove space from strings
for(var i=0; i<files.length; i++)  
	if(/\s/.test(files[i]))
	   filenames[i] = files[i].replace(/\s/g, '');
    else
	   filenames[i] = files[i];

//Extract json files to local folder
for(var i=0; i<files.length; i++)   {

	//Print filenames
	//console.log(files[i] + " = "+filenames[i]);
    
    fs.copyFile(path+'/'+files[i], 'data/'+filenames[i], (err) => {
		if (err) throw err;
	});

};



















/*
var xmlhttp = new XMLHttpRequest();
var url = "http://136.145.231.34/";

xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        var myArr = JSON.parse(this.responseText);
        myFunction(myArr);
    }
};
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(arr) {
    var out = "";
    var i;
    for(i = 0; i < arr.length; i++) {
        out += '<a href="' + arr[i].url + '">' +
        arr[i].display + '</a><br>';
    }
    document.getElementById("id01").innerHTML = out;
}


src="http://code.jquery.com/jquery-1.11.0.min.js";

(function() {
          var URL = "/Json/";
          $.getJSON( URL, {
            format: "json"
          })
            .done(function( data ) {
              $.each( data.items, function( i, item ) {
              
                //copyfile.js
                const fs = require('fs');

                // destination will be created or overwritten by default.
                fs.copyFile('#.json', '/JsonFiles/#.json', (err) => {
                  if (err) throw err;
                  console.log('File was copied to destination');
                });
              
                if ( i === 3 ) {
                  return false;
                }
              });
            });
        })();


*/

/*
$.ajax({
  url: 'http://136.145.231.34',
  data: data,
  success: null,
  dataType: "json"
});




$.get( "ajax/test.html", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
});


// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.get( "AROMATIZACIÓN.json", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
  });
 
// Perform other work here ...
 
// Set another completion function for the request above
jqxhr.always(function() {
  alert( "second finished" );
});



ftp://ubuntu:BE#u3bFh@136.145.231.34/

    host: '136.145.231.34',
    username: 'ubuntu',
    password: 'BE#u3bFh'
	jquery get json
*/