NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Ejercicio 9

// Required external modules.
fs = require('fs');
path = require('path');
bb = require('bluebird');
os = require('os'); // Needed for the end-of-line constant, in order to be ed54dd822d4ba184e018f67ff52381b744959c77
// system-independent.

// Get a copy of the arguments array.
var argv = process.argv;
// Get the name of this JavaScript file, without the extension.
var scriptName = path.basename(argv[1], ".js");
// Current file name.
var filename = "";
// Information to be printed at the end.
var smallestFile = "";
var largestFile = "";
var smallestLength = 0;
var largestLength = 0;
var totalLength = 0;
var myFiles=[],names=[]
var totalBytes = 0, max =0, min = 0, minPos = 0, minPos = 0
var sizes = []
// Needed in the onFulfilled handler.
var numProcessed = 2;

/*********************************************************************/
/* Auxiliary functions. */
/*********************************************************************/

function onError(err) {
// If the file does not exist, print an error message.
console.log("Please use the name of an existant file!!");
}

function atTheEnd() {
console.log( "The shortest file is %s with %d bytes.", names[0], min );
console.log( "The largest file is %s with %d bytes.", names[1], max );
console.log( "The aggregate length of all files is %d bytes.", totalBytes );
}

//Useless functions
function processFileTest(data){
console.log("%d",data.length)

}
function processFile2(num) {
return function(data) {
console.log(process.argv[3+num])
console.log("%s: %d bytes", filename, data.length);

var myLength = data.length;
// Update overall results, if needed.
if (smallestFile == "" || myLength < smallestLength) {
smallestFile = filename;
smallestLength = myLength;
}
if (largestFile == "" || myLength > largestLength) {
largestFile = filename;
largestLength = myLength;
}
totalLength += myLength;

// Print the summary information when the last file is processed.
numProcessed++;
}
}

//My util functions
function onErrorFinal(){
console.log("Error leyendo algun archivo")
}
function processFile(num){
return function(data){
console.log("%s: %d bytes", process.argv[num+2], data.length);

var myLength = data.length;
totalBytes += myLength

if(myLength>max){
maxPos = num+2
max=myLength
}
if(myLength<min || min<=0){
minPos=num+2
min=myLength
}
console.log("Total de momento --> %d", totalBytes)
numProcessed++;
}
}

/********************************************************************/
/* Main programme. */
/********************************************************************/

// Check whether the needed arguments have been received or not.
if (argv.length<3) {
// If not, print usage instructions and exit.
console.log("Usage: node " + scriptName + " files");
console.log("Print the length of every file received as argument.");
console.log("If multiple files are given, print also their aggregate length.");
process.exit();
}

// Convert fs.readFile() into a promise.
readPromise = bb.promisify(fs.readFile,fs);

//MY CODE ----------------------
var numFiles = process.argv.length-2, allLength=0
for(var i = 0;i<numFiles;i++){
console.log("Documento ",process.argv[2+i]," leido")
names[i]=process.argv[2+i]
myFiles[i]=bb.promisify(fs.readFile)(names[i],"utf8")
}
for(var i=0;i<numFiles;i++){
myFiles[i].then(processFile(i), onError)
}

bb.all(myFiles).then(atTheEnd,onErrorFinal);

//MY CODE ENDS -----------------


// Invoke the promises and wait for their results.
//for (var i=2; i<argv.length; i++) {
// Set the current filename.
//filename=argv[i];
// Use the promise.
//readPromise(argv[i], "utf8").then(processFile,onError);
//}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.