Hello world!

Source: Internet
Author: User

As a newcomer, I hope the gods have more guidance.

New study Nodejs related content, today saw the file IO operation, path optimization, URL processing related content, think of, to find an example of these content is integrated, but also I learn Nodejs "Hello World" it!

varFs=require (' FS ');varPath=require (' Path '); Fs.readdir (__dirname,function(err,files) {if(ERR)Throwerr;  for(i=0;i<files.length;i++)    {        if(Path.extname (files[i]) = = '. jpg ') {F_dirname=Path.dirname (Files[i]); F_basename=Path.basename (Files[i]); F_newpath=path.join (f_dirname, '/image/'), F_basename); //console.log (' f_dirname= ' +f_dirname+ ' \ n ' + ' f_basename= ' +f_basename+ ' \ n ' + ' f_newpath= ' +f_newpath);Fs.rename (Files[i],f_newpath,function(Error) {if(!error) Console.log (' Mission complete! ');}); }    }}

This code uses the file IO-related module FS, path optimization module path, using Readdir to read all the files in the current directory, for loop traversal file, with the Extname function in the path optimization module to pick out all ' JPG ' suffix file, use the Rename function rename in the file IO module to modify its path to within the image folder.

When editing this code, in the next and learned that rename modify the path method, can not create a new folder, if the path does not exist will not be active to create a file, but will throw an error, tell you that the path does not exist. The comment out of the paragraph is to forget the new Image folder caused the program to not run, but the error left behind ...

The first day study Nodejs, harvest not much, after every day to write a little, hope to see the great God to a little guide ~ Thank you!

Hello world!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.