脚本如何批量转小写大写

wen 实用脚本 26

本文目录导读:

脚本如何批量转小写大写

  1. Python 脚本 (最灵活)
  2. Bash/Linux 脚本 (适合文本文件)
  3. PowerShell 脚本 (Windows)
  4. 文本文件批量处理 (带备份)
  5. 在线批量转换 (无需安装)
  6. 使用建议

Python 脚本 (最灵活)

# 批量转换文件内容大小写
import os
def batch_convert(file_path, operation='lower'):
    """批量转换文件内容大小写"""
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()
    if operation == 'lower':
        new_content = content.lower()
    elif operation == 'upper':
        new_content = content.upper()
    elif operation == 'capitalize':
        new_content = content.capitalize()
    elif operation == 'title':
        new_content = content.title()
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(new_content)
# 批量处理目录下所有txt文件
def process_directory(directory, operation='lower'):
    for filename in os.listdir(directory):
        if filename.endswith('.txt'):
            file_path = os.path.join(directory, filename)
            batch_convert(file_path, operation)
            print(f"已转换: {filename}")
# 使用示例
process_directory('./files', 'lower')  # 改为小写
# process_directory('./files', 'upper')  # 改为大写

Bash/Linux 脚本 (适合文本文件)

#!/bin/bash
# 批量将文件内容转为小写
for file in *.txt; do
    if [ -f "$file" ]; then
        tr '[:upper:]' '[:lower:]' < "$file" > "${file%.txt}_lower.txt"
        echo "已转换: $file → ${file%.txt}_lower.txt"
    fi
done
# 批量将文件名转为小写
for file in *; do
    if [ -f "$file" ]; then
        mv "$file" "$(echo $file | tr '[:upper:]' '[:lower:]')"
    fi
done
# 批量修改文件名为大写
for file in *; do
    if [ -f "$file" ]; then
        mv "$file" "$(echo $file | tr '[:lower:]' '[:upper:]')"
    fi
done

PowerShell 脚本 (Windows)

# 批量转换文件内容为小写
Get-ChildItem -Path "C:\files" -Filter *.txt | ForEach-Object {
    $content = Get-Content $_.FullName -Raw
    $lowerContent = $content.ToLower()
    Set-Content -Path $_.FullName -Value $lowerContent
    Write-Host "已转换: $($_.Name)"
}
# 批量转换文件名为小写
Get-ChildItem -Path "C:\files" | Where-Object {!$_.PSIsContainer} | ForEach-Object {
    $newName = $_.Name.ToLower()
    Rename-Item -Path $_.FullName -NewName $newName
    Write-Host "已重命名: $($_.Name) -> $newName"
}

文本文件批量处理 (带备份)

import os
import shutil
from pathlib import Path
def safe_batch_convert(directory, operation='lower', backup=True):
    """安全批量转换并支持备份"""
    dir_path = Path(directory)
    # 创建备份目录
    if backup:
        backup_dir = dir_path / 'backup'
        backup_dir.mkdir(exist_ok=True)
    for file_path in dir_path.glob('*.txt'):
        if file_path.is_file():
            # 备份原文件
            if backup:
                shutil.copy2(file_path, backup_dir / file_path.name)
            # 读取内容
            with open(file_path, 'r', encoding='utf-8') as f:
                content = f.read()
            # 转换
            if operation == 'lower':
                new_content = content.lower()
            elif operation == 'upper':
                new_content = content.upper()
            # 写入新内容
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write(new_content)
            print(f"✅ 已转换: {file_path.name}")
# 使用
safe_batch_convert('./files', 'lower', backup=True)

在线批量转换 (无需安装)

对于临时需求,可以使用在线工具:

  • Convert Case - 支持多种转换方式
  • Text Mechanic - 批量文本处理
  • 在excel中使用 =LOWER(A1)=UPPER(A1) 公式

使用建议

  1. 先备份重要文件:在批量操作前做好备份
  2. 测试少量文件:先用1-2个文件测试效果
  3. 选择合适工具
    • 转换 → Python/Bash/PowerShell
    • 文件名转换 → PowerShell/Bash
    • 一次性快速转换 → 在线工具/Excel

需要更具体的场景实现吗?

抱歉,评论功能暂时关闭!