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!