Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
how to upload file in folder using cli in nodeJs

I am trying to make a CLI which upload only specific .extension file for Example if I want to upload .jpg file then only JPG file should be uploaded by making JPG folder

const { program } = require("commander");

const fs = require("fs");
const path = require("path");

program.version("0.0.1");

program
  .command("file")
  .alias("f")
  .description("Add filename with filepath")
  .action(() => {
    prompt(questions).then((answers) => {
      try {
        // compare extension
        const extension = path.extname(answers.fileName);
        const allowedExtension = ".jpg";

        if (allowedExtension !== extension) {
          console.log("Use only .jpg Extension file");
        } else {
          // make dir
          fs.mkdir(path.join(__dirname, "JPG"), { recursive: true }, (err) => {
            if (err) {
              return console.error(err);
            }

            // read file or uploaded file
            const file = fs.createReadStream(
              `${answers.filePath}/${answers.fileName}`
            );
            console.log(
              "Directory created successfully!",
              answers.fileName,
              answers.filePath
            );
          });
        }
      } catch (error) {
        console.log(error.message);
      }
    });
  });

program.parse(process.argv);

but don't know how to upload file by using CLI in the provided folder

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Upload it using writeFile function:

const { program } = require("commander");

const fs = require("fs");
const path = require("path");

program.version("0.0.1");

program
  .command("file")
  .alias("f")
  .description("Add filename with filepath")
  .action(() => {
    prompt(questions).then((answers) => {
      try {
        // compare extension
        const extension = path.extname(answers.fileName);
        const allowedExtension = ".jpg";

        if (allowedExtension !== extension) {
          console.log("Use only .jpg Extension file");
        } else {
          // make dir
          fs.mkdir(path.join(__dirname, "JPG"), { recursive: true }, (err) => {
            if (err) {
              return console.error(err);
            }

            const file = fs.createReadStream(
              `${answers.filePath}/${answers.fileName}`
            );
            fs.writeFile(`${answers.filePath}/${answers.fileName}`,file,(err) => console.log(err))
            console.log(
              "Directory created successfully!",
              answers.fileName,
              answers.filePath
            );
          });
        }
      } catch (error) {
        console.log(error.message);
      }
    });
  });

program.parse(process.argv);

about 4 years ago · Juan Pablo Isaza Report

0

I have never used nodeJs so this may or may not work. You should look up scp for cli transfer between two sources, you can install the node-scp module using npm(or so I read)

npm i node-scp

Then you need to go about importing it, defining source and destination paths, and then using it.

var local_folder_path = './local/dir';
var detination_folder_path = '/server/path';

send_folder_using_async_await(local_folder_path, detination_folder_path);

async function send_folder_using_async_await(folder_path, 
destination_path)
{
   try {
       const client = await scp(remote_server)
       await client.uploadDir(folder_path, destination_path)
       client.close()
   } catch (e) {
      console.log(e)
   }
}

Something like this.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!