在 Windows XP 上使用国际字符批量重命名文件

发布于 2024-07-04 07:21:37 字数 927 浏览 4 评论 0原文

我有一大堆文件,文件名使用可爱的瑞典字母 å åö。 由于各种原因,我现在需要将它们转换为 [a-zA-Z] 范围。 只需删除该范围之外的任何内容就相当容易。 给我带来麻烦的是我想用 a 替换 å,用 o 替换 ö ,很快。

这是最糟糕的字符集问题。

我有一组测试文件:

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

我将我的脚本基于这一行,将其结果通过管道传输到各种命令中

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

奇怪的是,如果我将其结果(即普通的 for 循环)打印到我得到的文件中这个输出:

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

所以在我的文件名到达其他工具之前就发生了一些奇怪的事情(我一直在尝试使用来自名为 GnuWin32 的 Windows 的 sed 端口来执行此操作,但到目前为止还没有运气)并对这些字符进行替换也没有帮助。

你会如何解决这个问题? 我对任何类型的工具、命令行或其他工具持开放态度......

编辑:这是一个一次性问题,所以我正在寻找一种快速且丑陋的修复方法

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.

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:

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

指尖凝香 2024-07-11 07:21:37

我会在 C++、C# 或 Java 环境中编写此代码,在这些环境中我确信您可以正确地从路径中获取 Unicode 字符。 命令行工具总是不确定的,尤其是在 Cygwin 之外。

那么代码就是简单的查找/替换或正则表达式/替换。 如果你能命名一种语言,那么编写代码就会很容易。

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. It's always uncertain with command-line tools, especially out of 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.

泪是无色的血 2024-07-11 07:21:37

我会编写一个 vbscript (WSH) 来扫描目录,然后将文件名发送到一个函数,该函数将文件名分解为单独的字母,然后对瑞典文件名执行 SELECT CASE 并将其替换为您想要的文件名。 或者,该函数可以不这样做,而是通过一堆 REPLACE() 函数将其删除,将输出重新分配给输入字符串。 最后,它会使用新值重命名该文件。

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. Or, instead of doing that the function could just drop it thru a bunch of REPLACE() functions, reassigning the output to the input string. At the end it then renames the file with the new value.

娜些时光,永不杰束 2024-07-11 07:21:37

您可以使用此代码(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 can use this code (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.

梦巷 2024-07-11 07:21:37

如果您以 UNICODE 模式打开 cmd.exe,您可能会更幸运。 使用“cmd /U”。

其他人建议使用真正的编程语言。 没关系,特别是如果您有一种非常熟悉的语言。 我 C# 团队的朋友说 C# 3.0(使用 Linq)非常适合创建像这样的快速小程序。 他大部分时间已经停止编写批处理文件。

就我个人而言,我会选择 PowerShell。 这个问题可以在命令行上用一行代码解决。 我将

编辑:这不是一行,但也不是很多代码。 另外,StackOverflow 似乎不喜欢语法“$_.Name”,并将 _ 呈现为 _。

$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 might have more luck in cmd.exe if you opened it in UNICODE mode. Use "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. He has stopped writing batch files most of the time.

Personally, I would choose 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 _.

$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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文