批量处理文件名

最近在处理内部图片资源的时候,发现素材组给过来的资源文件名字比较特殊。比如一款游戏英文名是"xxxx-ball",但是图片名字却是叫做"Xxxx Ball 1.png",需要处理成类似slug形式。因为存在大量文件处理,用人工方式处理是很低效。本文主要针对这种情况,快速列举几种资源处理方式,原理大致相同

1、node处理

node 复制代码
const fs = require('fs');
const path = require('path');

// 指定源目录和目标目录
const sourcePath = 'C:\\Users\\40485\\Desktop\\deal';
const destinationPath = 'C:\\Users\\40485\\Desktop\\deal1';

// 确保目标目录存在
if (!fs.existsSync(destinationPath)) {
    fs.mkdirSync(destinationPath, { recursive: true });
}

// 递归处理源目录中的文件和子文件夹
const processDirectory = (sourceDir, destinationDir) => {
    const files = fs.readdirSync(sourceDir);

    files.forEach(file => {
        const filePath = path.join(sourceDir, file);
        const stats = fs.statSync(filePath);

        if (stats.isDirectory()) {
            // 如果是目录,则递归处理子目录
            const newDestinationDir = path.join(destinationDir, file);
            fs.mkdirSync(newDestinationDir, { recursive: true });
            processDirectory(filePath, newDestinationDir);
        } else {
            // 如果是文件,则进行文件名处理和复制
            const originalName = file;
            let newName = originalName.replace(/\d/g, '')
                                        .replace(/\s+/g, '-')
                                        .replace(/[^a-z_.-]/ig, '')
                                        .replace(/-+/g, '-')
                                        .toLowerCase();

            const newFilePath = path.join(destinationDir, newName);
            fs.copyFileSync(filePath, newFilePath);
        }
    });
};

processDirectory(sourcePath, destinationPath);

2、PowerShell脚本

ini 复制代码
# 指定源目录和目标目录

$sourcePath = "C:\Users\40485\Desktop\deal"
$destinationPath = "C:\Users\40485\Desktop\deal1"

# 确保目标目录存在

if (-not (Test-Path -Path $destinationPath)) {
New-Item -Path $destinationPath -ItemType Directory
}

Get-ChildItem -Path $sourcePath -Recurse | ForEach-Object {
$originalName = $_.Name
 $newName = $originalName -replace '\d', '' -replace '\s+', '-' -replace '[^a-z_.-]', '' -replace '-+', '-'
$newName = $newName.ToLower()

    # 生成新的目标路径
    $relativePath = $_.FullName.Substring($sourcePath.Length)
    $newPath = Join-Path -Path $destinationPath -ChildPath $relativePath
    $newPath = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($newPath), $newName)

    # 确保目标子目录存在
    $newDirectory = [System.IO.Path]::GetDirectoryName($newPath)
    if (-not (Test-Path -Path $newDirectory)) {
        New-Item -Path $newDirectory -ItemType Directory
    }

    # 复制文件到新目录并重命名
    Copy-Item -Path $_.FullName -Destination $newPath -Force

}

注意:用另外个目录处理,主要是为了避免出现同名(但是英文字母有大小写之分),仍被当成同一个文件

3、bash

sh 复制代码
#!/bin/bash

# 指定源目录和目标目录
sourcePath="C:/Users/40485/Desktop/deal"
destinationPath="C:/Users/40485/Desktop/deal1"

# 确保目标目录存在
if [ ! -d "$destinationPath" ]; then
    mkdir -p "$destinationPath"
fi

# 递归处理源目录中的文件和子文件夹
process_directory() {
    local sourceDir="$1"
    local destinationDir="$2"

    for file in "$sourceDir"/*; do
        if [ -d "$file" ]; then
            # 如果是目录,则递归处理子目录
            local subdir="$destinationDir/$(basename "$file")"
            mkdir -p "$subdir"
            process_directory "$file" "$subdir"
        else
            # 如果是文件,则进行文件名处理和复制
            local originalName=$(basename "$file")
            local newName=$(echo "$originalName" | sed -E 's/[0-9]+//g; s/\s+/-/g; s/[^a-zA-Z0-9_.-]//g; s/-+/-/g; s/.*/\L&/')
            cp "$file" "$destinationDir/$newName"
        fi
    done
}

# 开始处理源目录
process_directory "$sourcePath" "$destinationPath"

参考

相关推荐
艾小码12 小时前
手把手教你实现一个EventEmitter,彻底告别复杂事件管理!
前端·javascript·node.js
前端小哲13 小时前
MCP从入门到实战
node.js·ai编程
dasseinzumtode14 小时前
nestJS 使用ExcelJS 实现数据的excel导出功能
前端·后端·node.js
梅孔立19 小时前
服务器不支持node.js16以上版本安装?用Docker轻松部署Node.js 20+环境运行Strapi项目
服务器·docker·node.js
XiaoMu_00119 小时前
基于Node.js和Three.js的3D模型网页预览器
javascript·3d·node.js
卿·静19 小时前
Node.js对接即梦AI实现“千军万马”视频
前端·javascript·人工智能·后端·node.js
lvlv_feifei19 小时前
N8N macOS (Apple Silicon) 完整安装配置教程
node.js·workflow
Cosmoshhhyyy1 天前
Node.js 18+安装及Claude国内镜像使用、idea中claude插件下载指南
node.js
徐_三岁2 天前
关于npm的钩子函数
前端·npm·node.js
不买Huracan不改名2 天前
安装Codex(需要用npm)
前端·npm·node.js