批量处理文件名

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

参考

相关推荐
engineer_why10 分钟前
【elpis专栏】里程碑一:elpis-core 引擎的理解
node.js
Littlewith15 小时前
Node.js:创建第一个应用
服务器·开发语言·后端·学习·node.js
墨菲安全15 小时前
Node.js Windows下路径遍历漏洞
windows·node.js·路径遍历漏洞
码间舞15 小时前
【面试官】:NodeJs事件循环你了解多少?我笑了,让我喝口水慢慢给你说来......
后端·node.js
Komorebi_99991 天前
webpack相关
前端·webpack·node.js
半桶水专家1 天前
webpack和vite对比
前端·webpack·node.js
Q_Q19632884751 天前
python的抗洪救灾管理系统
开发语言·spring boot·python·django·flask·node.js·php
i小溪2 天前
Fanuc机床 CNC_STATINFO 方法返回值说明
物联网·node.js
梦想CAD控件2 天前
在线CAD实现形位公差标注(在线编辑DWG)
前端·javascript·node.js
HHW2 天前
大文件上传难题?前端优雅解决方案全解析!
前端·node.js