Problem: Folder A has a large number of picture files, you need to replace a picture with another picture, but the name remains unchanged.
The manual approach is as follows:
1 browser Open picture->2) Save as->3) destination folder->4) find a picture->5) replace->6) ok
Then, repeat step 2 and the following steps. Carelessness will leave out or repeat.
So troublesome, use node.js to handle change how? The code is as follows:
var fs=require (' FS '),
cp=require (' child_process ');
var url= ' moriarty.jpg '
var rs=fs.readfilesync (url,{encoding: ' Hex ', flag: ' R '})
var write= (e) =>{
Fs.writefilesync (e,rs,{encoding: ' Hex ', flag: ' W '})
}
var buf_files=cp.execsync (' ls 160906/*.jpg ')
var Arr_files=buf_files.tostring (). Trim (). Split (/\s+/)
Arr_files.foreach (write)
It can also be done with the Glob and FS modules: The code is as follows:
var glob=require (' Glob '),
fs=require (' fs ')
var url= ' moriarty.jpg '
var rs=fs.readfilesync (url,{ Encoding: ' Hex ', flag: ' R '})
var write= (e) =>{fs.writefilesync
(e,rs,{encoding: ' Hex ', flag: ' W '})
}
glob (' 160906/*.jpg ', (err,files) =>{
Files.foreach (write)
})
The effect of the following figure:
File Original Picture:
File New picture: (Picture filename unchanged)
The above node.js to read and write files of the bulk replacement of the image of the implementation of the method is to share the whole of the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.