批量处理文件名

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

参考

相关推荐
HWL56791 小时前
Express项目解决跨域问题
前端·后端·中间件·node.js·express
爱编程的小学究3 小时前
【node】如何把包发布到npm上
前端·npm·node.js
2501_915373884 小时前
全栈项目实战:Vue3+Node.js开发博客系统
node.js
狂野小青年5 小时前
npm 报错 gyp verb `which` failed Error: not found: python2 解决方案
前端·npm·node.js
鲁鲁5175 小时前
Windows 环境下安装 Node 和 npm
前端·npm·node.js
Adorable老犀牛6 小时前
npm install 报错
npm·node.js
Vone_6614 小时前
node.js 邮箱验证服务器搭建
运维·服务器·node.js
程序员拂雨16 小时前
HTTP和HTTPS模块
http·https·node.js
dgiij21 小时前
excel大表导入数据库
数据库·mysql·node.js·excel
x-cmd1 天前
[250512] Node.js 24 发布:ClangCL 构建,升级 V8 引擎、集成 npm 11
前端·javascript·windows·npm·node.js