Node.js of batch replacement pictures for read and write files _node.js

Source: Internet
Author: User
Tags glob

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.

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.