Advertisement
chava_vm

Unir con FS (NodeJS)

Apr 9th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require("fs");
  2. const path = require("path");
  3.  
  4. var archivos = new Array;
  5. const ext = path.extname(process.argv[2]); //Obtiene solamente la última parte de la extensión, si es que se trata de una extensión compuesta
  6.  
  7. try{
  8.  
  9.     for(i = 2; i <= process.argv.length - 1; i++){
  10.  
  11.         archivos[i-2] = fs.readFileSync(process.argv[i]);
  12.  
  13.     }
  14.  
  15.     var nuevoArchivo = fs.openSync(`union${ext}`, 'w'); //Crea un nuevo archivo y regresa el fd
  16.  
  17.     archivos.forEach((element, index) => {
  18.         fs.write(nuevoArchivo, element, err =>{
  19.             if (err) throw err;
  20.  
  21.             console.log("Archivo " + index + " unido");
  22.         })
  23.     });
  24.        
  25.    
  26. } catch(e){
  27.    
  28.         console.error(e);
  29.    
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement