文件重命名一批在Windows XP的国际字符 [英] Batch renaming of files with international chars on Windows XP

查看:170
本文介绍了文件重命名一批在Windows XP的国际字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一大堆的文件使用我们可爱的瑞典字母的 A A 文件名和 0
由于各种原因,我现在需要把这些转换为[A-ZA-Z]范围内。就在这个范围之外消除任何是相当容易的。这是造成我的麻烦的事情是,我想替换 0 0 并等等。

I have a whole bunch of files with filenames using our lovely Swedish letters å å and ö. For various reasons I now need to convert these to an [a-zA-Z] range. Just removing anything outside this range is fairly easy. The thing that's causing me trouble is that I'd like to replace å with a, ö with o and so on.

这是在最糟糕的字符集的麻烦。

This is charset troubles at their worst.

我有一组测试文件:

files\Copy of New Text Documen åäö t.txt
files\fofo.txt
files\New Text Document.txt
files\worstcase åäöÅÄÖéÉ.txt

我在我的基础上的脚本这条线,管道它的成果转化为各种命令

I'm basing my script on this line, piping it's results into various commands

for %%X in (files\*.txt) do (echo %%X) 

的奇怪的事情是,如果我打印的这个结果到一个文件中(for循环是平原)我得到这样的输出:

The wierd thing is that if I print the results of this (the plain for-loop that is) into a file I get this output:

files\Copy of New Text Documen †„" t.txt
files\fofo.txt
files\New Text Document.txt
files\worstcase †„"Ž™‚.txt

因此​​,一些奇怪的是发生在我的文件名,他们甚至达到其他工具之前(我一直在试图做到这一点利用的东西用于Windows的sed的口叫的GnuWin32但至今没有运气),并在替换这些字符没有帮助的。

So something wierd is happening to my filenames before they even reach the other tools (I've been trying to do this using a sed port for Windows from something called GnuWin32 but no luck so far) and doing the replace on these characters doesn't help either.

你会如何解决这个问题?我接受任何类型的工具,命令行或以其他方式...

How would you solve this problem? I'm open to any type of tools, commandline or otherwise…

编辑:这是一个时间的问题,所以我在寻找一个快速'东经丑陋修复

This is a one time problem, so I'm looking for a quick 'n ugly fix

推荐答案

您可以使用此code(蟒蛇)

You can use this code (Python)

# -*- coding: cp1252 -*-

import os, shutil

base_dir = "g:\\awk\\"    # Base Directory (includes subdirectories)
char_table_1 = "áéíóúñ"
char_table_2 = "aeioun"

adirs = os.walk (base_dir)

for adir in adirs:
    dir = adir[0] + "\\"          # Directory
    # print "\nDir : " + dir

    for file in adir[2]:    # List of files
        if os.access(dir + file, os.R_OK):
            file2 = file
            for i in range (0, len(char_table_1)):
                file2 = file2.replace (char_table_1[i], char_table_2[i])

            if file2 <> file:
                # Different, rename
                print dir + file, " => ", file2
                shutil.move (dir + file, dir + file2)

###

您必须改变你的编码和您的字符表(我测试了这个剧本与西班牙的文件和正常工作)。您可以发表评论移动行检查,如果它的工作好了,后来又删除注释做了重新命名。

You have to change your encoding and your char tables (I tested this script with Spanish files and works fine). You can comment the "move" line to check if it's working ok, and remove the comment later to do the renaming.

这篇关于文件重命名一批在Windows XP的国际字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆