Node file batch renaming method example, node example
In an actual requirement, You need to rename a batch of files (such as text and images) by number. I am familiar with node's fs file operations and wrote a script to modify file names in batches.
Requirement
Existing slice files
You need to modify the file name in batches to change it to a uniform prefix name and auto-increment index.
The simplest human operation is to rename files one by one, but write a node script based on the DRY (Don't repeat yourself) principle.
Research
To perform file operations in node, you need to understand the fs module.
There are two methods in the fs module: synchronous and asynchronous.
Read files
// Remote fs.readfile('test.txt ', 'utf-8' (err, data) =>{ if (err) {throw err;} console. log (data) ;}); // synchronize let data = fs.readFileSync('test.txt '); console. log (data );
Asynchronous File Reading parameters: file path, encoding method, callback function
Write files
fs.writeFile('test2.txt', 'this is text', { 'flag': 'w' }, err => { if (err) { throw err; } console.log('saved');});
Write File parameters: target file, write content, write form, callback function
Flag write method:
R: Reading files
W: write files
A: append
Create directory
fs.mkdir('dir', (err) => { if (err) { throw err; } console.log('make dir success');});
Dir is the name of the new directory.
Read directory
fs.readdir('dir',(err, files) => { if (err) { throw err; } console.log(files);});
Dir is the name of the read directory, and files is the array of files or directory names under the Directory
Get File Information
fs.stat('test.txt', (err, stats)=> { console.log(stats.isFile()); //true})
Stats method after obtaining file information:
| Method |
Description |
| Stats. isFile () |
Is it a file? |
| Stats. isDirectory () |
Is it a directory? |
| Stats. isBlockDevice () |
Block device? |
| Stats. isCharacterDevice () |
Character device or not |
| Stats. isSymbolicLink () |
Soft link or not |
| Stats. isFIFO () |
Unix fifo command pipeline? |
| Stats. isSocket () |
Socket? |
Create read stream
let stream = fs.createReadStream('test.txt');
Create write stream
let stream = fs.createWriteStreamr('test_copy.txt');
Development
Development ideas:
- Read Source directory
- Check whether the storage directory exists. If the storage directory does not exist, create a new directory.
- Copy a file
- Determine whether the copied content is a file
- Create read stream
- Create write stream
- Link MPs queue and Write File Content
Let fs = require ('fs'), src = 'src', dist = 'dist', args = process. argv. slice (2), filename = 'image', index = 0; // show helpif (args. length = 0 | args [0]. match ('-- help') {console. log ('-- help \ n \ t-src file source \ n \ t-dist file destination \ n \ t-n file name \ n \ t-I file name index \ n '); return false;} args. forEach (item, I) =>{ if (item. match ('-src') {src = args [I + 1];} else if (item. match ('-dist') {dist = args [I + 1];} else if (item. match ('-n') {filename = args [I + 1];} else if (item. match ('-I') {index = args [I + 1] ;}}); fs. readdir (src, (err, files) =>{ if (err) {console. log (err);} else {fs. exists (dist, exist => {if (exist) {copyFile (files, src, dist, filename, index);} else {fs. mkdir (dist, () =>{ copyFile (files, src, dist, filename, index) ;}}) ;}}); function copyFile (files, src, dist, filename, index) {files. forEach (n => {let readStream, writeStream, arr = n. split ('. '), oldPath = src +'/'+ n, newPath = dist +'/'+ filename + index + '. '+ arr [arr. length-1]; fs. stat (oldPath, (err, stats) =>{ if (err) {console. log (err);} else if (stats. isFile () {readStream = fs. createReadStream (oldPath); writeStream = fs. createWriteStream (newPath); readStream. pipe (writeStream) ;}}); index ++ ;})}
Effect
Summary
Node provides many modules to help us develop functions with different requirements, so that javascript is not limited to the browser, but it is helpful to understand these modules by writing some scripts by yourself, at the same time, it can also improve office efficiency.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.