简体   繁体   English

在 Windows XP 上批量重命名具有国际字符的文件

[英]Batch renaming of files with international chars on Windows XP

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.由于各种原因,我现在需要将这些转换为 [a-zA-Z] 范围。 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.给我带来麻烦的是,我想用a替换å ,用o替换ö等等。

This is charset troubles at their worst.这是最糟糕的字符集问题。

I have a set of test files:我有一组测试文件:

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) 

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:奇怪的是,如果我将这个结果(即普通的 for 循环)打印到一个文件中,我会得到这个输出:

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

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.因此,在它们到达其他工具之前,我的文件名发生了一些奇怪的事情(我一直在尝试使用 GnuWin32 中的 Windows sed 端口来执行此操作,但到目前为止没有运气)并且对这些字符进行替换并没有帮助。

How would you solve this problem?你会如何解决这个问题? I'm open to any type of tools, commandline or otherwise…我对任何类型的工具、命令行或其他方式持开放态度……

EDIT: This is a one time problem, so I'm looking for a quick 'n ugly fix编辑:这是一个一次性的问题,所以我正在寻找一个快速的“丑陋的修复”

You might have more luck in cmd.exe if you opened it in UNICODE mode.如果您在 UNICODE 模式下打开 cmd.exe,您可能会更幸运。 Use "cmd /U".使用“cmd /U”。

Others have proposed using a real programming language.其他人建议使用真正的编程语言。 That's fine, especially if you have a language you are very comfortable with.这很好,特别是如果你有一种你非常熟悉的语言。 My friend on the C# team says that C# 3.0 (with Linq) is well-suited to whipping up quick, small programs like this.我在 C# 团队的朋友说 C# 3.0(带有 Linq)非常适合开发这样的快速、小程序。 He has stopped writing batch files most of the time.大多数时候他已经停止编写批处理文件。

Personally, I would choose PowerShell.就个人而言,我会选择 PowerShell。 This problem can be solved right on the command line, and in a single line.这个问题可以直接在命令行上解决,也可以在一行中解决。 I'll患病的

EDIT: it's not one line, but it's not a lot of code, either.编辑:它不是一行,但也不是很多代码。 Also, it looks like StackOverflow doesn't like the syntax "$_.Name", and renders the _ as &#95.此外,看起来 StackOverflow 不喜欢语法“$_.Name”,并将 _ 呈现为 &#95。

$mapping = @{ 
    "å" = "a"
    "ä" = "a"
    "ö" = "o"
}

Get-ChildItem -Recurse . *.txt | Foreach-Object { 
    $newname = $_.Name      
    foreach  ($l in $mapping.Keys) {
        $newname = $newname.Replace( $l, $mapping[$l] )
        $newname = $newname.Replace( $l.ToUpper(), $mapping[$l].ToUpper() )
    }
    Rename-Item -WhatIf $_.FullName $newname    # remove the -WhatIf when you're ready to do it for real.
}

You can use this code (Python)您可以使用此代码(Python)

Rename international files重命名国际文件

# -*- 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.您可以评论“移动”行以检查它是否正常工作,然后删除评论以进行重命名。

I would write this in C++, C#, or Java -- environments where I know for certain that you can get the Unicode characters out of a path properly.我会用 C++、C# 或 Java 编写它——我确信在这些环境中您可以正确地从路径中获取 Unicode 字符。 It's always uncertain with command-line tools, especially out of Cygwin.命令行工具总是不确定的,尤其是在 Cygwin 之外。

Then the code is a simple find/replace or regex/replace.然后代码是一个简单的查找/替换或正则表达式/替换。 If you can name a language it would be easy to write the code.如果您可以命名一种语言,则编写代码将很容易。

I'd write a vbscript (WSH) to scan the directories, then send the filenames to a function that breaks up the filenames into their individual letters, then does a SELECT CASE on the Swedish ones and replaces them with the ones you want.我会写一个 vbscript (WSH) 来扫描目录,然后将文件名发送到一个函数,该函数将文件名分解为单独的字母,然后对瑞典语进行 SELECT CASE 并将它们替换为您想要的。 Or, instead of doing that the function could just drop it thru a bunch of REPLACE() functions, reassigning the output to the input string.或者,该函数不是这样做,而是通过一堆 REPLACE() 函数将其删除,将输出重新分配给输入字符串。 At the end it then renames the file with the new value.最后,它会使用新值重命名文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM