Node is no stranger to the front and back end developers, for the front end of the small white, Node is more high, although the same JavaScript, but node learning route is quite steep, for novice programmers may be difficult to learn to go in, but learning is still to learn, if it is with a problem to learn, Maybe it won’t be so boring and it will get twice the result with half the effort. Today we’ll take a look at how Node can help programmers solve problems quickly. Firstly, the problem is as follows: I have to deal with a large number of pictures at work, and the designer gave me pictures named in Chinese. As we all know, Chinese may appear garbled characters in the program, so I need to change my Chinese name to English.





const fs = require('fs');
Copy the code

Two fs apis are used: Readdir and fs.rename,fs.readdir(path, [callback(err,files)]) will read all files in the directory. Path is the path parameter, and the callback function has two parameters. Err is the error message. Is an array; Fs.rename (oldPath, newPath, [callback(err)]) renames a file. The three parameters are the original path, the newPath, and the callback function. Design input the parameter in the form of “Aud-Audi” in the command line, and convert “Audi” in the file name to “audi”. This requires additional parameters to be entered at the command line. Obtained from node’s global process.argv argument. Note that process.argv returns an array of arguments to the command-line script:

$ node rename 奥迪-audi
Copy the code

The return result is:

[ 
  'C:\\Program Files\\nodejs\\node.exe'.'D:\\test_projects\\rename'.'the audi - the audi' 
]
Copy the code

The first two elements are the node path and the js file path. The third element is the required parameter, so only the third element can be extracted. This parameter is then passed, and the file name read is renamed according to this rule and exported. Post the code below:

const fs = require('fs');
let path = '/';
letstr = process.argv[2]; Filename -- the name of the file to be replaced STR -- the replacement rule, obtained from the command linefunction replaceName(filename,str){
	str = str.split(The '-');
	let re = RegExp(str[0],'g'); // Replace the argument with a regularlet newname = filename.replace(re,str[1]);
	returnnewname; }; Fs.readdir (path,(err,files)=>{files = files.filter(element=>element.split()'. ') [1] = = ='jpg'); // Filter image file files.map(filename=>{let oldPath = path + filename;
		let newPath = path + replaceName(filename,str);

		fs.rename(oldPath,newPath,(err)=>{
			if(! err){ console.log(filename +' is done'); }})}); })Copy the code

That’s all the code, very simple. The renaming code is encapsulated as a function, which is called repeatedly when the file is read to rename the file, and finally exported to the original path to overwrite the original file. In more than one thing to note that in replaceName function using regular expressions, so that we can complete accurate replaced all the fields with the same, and if directly into a regular expression to replace variables is not effective, part of the regular will treat as variable expression parsing, in after some time, the query can create regular object, It is possible to pass parameters into a regular object, thus fulfilling the purpose of adding parameters to a regular expression. When using the command, enter the directory where the picture resides, enter the following command on the command line, and press Enter to execute the command. The Audi field in all file names will be replaced with Audi.

$node file path \\rename aud-audiCopy the code

conclusion

Code structure is very simple, knowledge points

  • fs.readdirRead files in a directory
  • fs.renameRename file
  • Command line parameters
  • How to add parameters to a regular expression