批量处理文件名

最近在处理内部图片资源的时候,发现素材组给过来的资源文件名字比较特殊。比如一款游戏英文名是"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***71854 小时前
windows下安装并使用node.js
windows·node.js
EndingCoder13 小时前
OAuth 2.0与第三方登录
node.js·oauth·第三方登录
zhousenshan13 小时前
Nodejs基础知识100问
node.js
|晴 天|13 小时前
Vite 为何能取代 Webpack?新一代构建工具的崛起
前端·webpack·node.js
f***589813 小时前
最新最详细的配置Node.js环境教程
node.js
y***866916 小时前
JavaScript在Node.js中的Electron
javascript·electron·node.js
lpfasd12317 小时前
Windows下使用nvm-windows管理Node.js版本及npm配置全指南
windows·npm·node.js
p***h6431 天前
JavaScript在Node.js中的异步编程
开发语言·javascript·node.js
q***38511 天前
TypeScript 与后端开发Node.js
javascript·typescript·node.js
q***09801 天前
Windows 11 如何配置node.js
windows·node.js