zoukankan      html  css  js  c++  java
  • nodejs 遍历文件夹下所有的图片改名为中文

     安装依赖

    $ npm init -y && npm i fs-extra globby request -S

    main.js

    const fs      = require('node-fs-extra')
    const globby  = require('globby')
    const request = require('request')
    
    // 简单的GET请求获取翻译结果
    const _request = (text, cb) => {
    	request({
    	    url: encodeURI(`http://119.23.22.136:6635/baidu_transapi.php?text=${text}&type=tuofeng`),
    	}, function (err, response, body) {
    		// 如果翻译异常那么直接中断
    		if (err)
    			// 抛出异常吧
    			throw new Error(`${err.message} ///////////////// ${text} ///////////////// ${body}`)
    		// 必须有内容返回并且请求码为200才可以回调
    		if (body && response.statusCode === 200)
    			// 回调
    	    	cb && cb(body)
    	})
    }
    
    // 从字符串中区分出名字和后缀
    const get = name => {
    	const len = name.lastIndexOf('.')
    	return { name: name.substr(0, len), ext: name.substr(len) }
    }
    
    // 遍历当前文件夹下所有的文件
    (async () => {
    	// 筛选当前文件夹下的文件类型
    	const names = await globby(['*.png|*.jpg|*.gif'])
    	// 开始遍历改名
    	for (let [index, filename] of names.entries()) {
    	    // 获取文件名和前缀
    	    const { name, ext } = get(filename)
    	    // 发送请求
    	    _request(name, ch => {
    	    	// 如果翻译结果不为空并且不为原本的值
    	    	if (ch && ch != name) {
    		    	// 那么修改文件名
    		    	fs.rename(filename, ch + ext, err => {
    		    		// 如果出现异常,那么直接中止
    			    	if (err) 
    			    		// 抛出异常吧
    			    		throw new Error(`${err.message} ///////////////// ${filename} ///////////////// ${ch}`)
    			    })
    			}
    	    })
    	}
    })()
    
  • 相关阅读:
    [转]Understanding Integration Services Package Configurations
    [转]SSIS: Execute Package via Stored Procedure
    [转]Loading and Running a Local Package Programmatically
    [转]Mapping Stored Procedure Parameters in SSIS OLE DB Source Editor
    [转]Data Flow How-to Topics (SSIS)
    [转]Advanced Oracle SQL Developer Features
    [转]SSIS包的调用方式
    [转]oracle 11g 忘记 默认用户密码
    [转]Insert, Update, and Delete Destination table with SSIS
    [转]JS判断访问设备、客户端操作系统类型
  • 原文地址:https://www.cnblogs.com/CyLee/p/9995083.html
Copyright © 2011-2022 走看看