批量处理文件名

最近在处理内部图片资源的时候,发现素材组给过来的资源文件名字比较特殊。比如一款游戏英文名是"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"

参考

相关推荐
Q_Q5110082851 小时前
python的软件工程与项目管理课程组学习系统
spring boot·python·django·flask·node.js·php·软件工程
outsider_友人A3 小时前
前端也想写后端(3)中间件Middleware、管道Pipes、拦截器Interceptors
前端·node.js·nestjs
爱加班的猫1 天前
Node.js 中 require 函数的原理深度解析
前端·node.js
冲!!1 天前
使用nvm查看/安装node版本
前端·node.js·node·nvm
萌萌哒草头将军2 天前
Node.js v24.6.0 新功能速览 🚀🚀🚀
前端·javascript·node.js
行星0082 天前
mac 通过homebrew 安装和使用nvm
macos·npm·node.js
kngines2 天前
【Node.js从 0 到 1:入门实战与项目驱动】1.3 Node.js 的应用场景(附案例与代码实现)
node.js
xrkhy3 天前
nvm安装详细教程(卸载旧的nodejs,安装nvm、node、npm、cnpm、yarn及环境变量配置)
前端·npm·node.js
专注API从业者3 天前
Python/Node.js 调用taobao API:构建实时商品详情数据采集服务
大数据·前端·数据库·node.js
Q_Q19632884753 天前
python基于Hadoop的超市数据分析系统
开发语言·hadoop·spring boot·python·django·flask·node.js