<p>I have a whole bunch of files with filenames using our lovely Swedish letters <strong>å å</strong> and <strong>ö</strong>.
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 <strong>å</strong> with <strong>a</strong>, <strong>ö</strong> with <strong>o</strong> and so on. </p>

<p>This is charset troubles at their worst.</p>

<p>I have a set of test files:</p>

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

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

<pre><code>for %%X in (files\*.txt) do (echo %%X) 
</code></pre>

<p>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:</p>

<pre><code>files\Copy of New Text Documen †„” t.txt
files\fofo.txt
files\New Text Document.txt
files\worstcase †„”Ž™‚.txt
</code></pre>

<p>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.</p>

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

<p><strong>EDIT:</strong> This is a one time problem, so I'm looking for a quick 'n ugly fix</p>

[{'answer_id': 56924, 'author': 'Jason Cohen', 'author_id': 4926, 'author_profile': 'https://Stackoverflow.com/users/4926', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 56938, 'author': 'busse', 'author_id': 5702, 'author_profile': 'https://Stackoverflow.com/users/5702', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 57049, 'author': 'Jay Bazuzi', 'author_id': 5314, 'author_profile': 'https://Stackoverflow.com/users/5314', 'pm_score': 1, 'selected': False, 'text': '<p>You might have more luck in cmd.exe if you opened it in UNICODE mode.  Use "cmd /U".</p>\n\n<p>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.</p>\n\n<p>Personally, I would choose PowerShell.  This problem can be solved right on the command line, and in a single line.  I\'ll</p>\n\n<p>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 &amp;#95.</p>\n\n<pre><code>$mapping = @{ \n    "å" = "a"\n    "ä" = "a"\n    "ö" = "o"\n}\n\nGet-ChildItem -Recurse . *.txt | Foreach-Object { \n    $newname = $_.Name      \n    foreach  ($l in $mapping.Keys) {\n        $newname = $newname.Replace( $l, $mapping[$l] )\n        $newname = $newname.Replace( $l.ToUpper(), $mapping[$l].ToUpper() )\n    }\n    Rename-Item -WhatIf $_.FullName $newname    # remove the -WhatIf when you\'re ready to do it for real.\n}\n</code></pre>\n'}, {'answer_id': 57359, 'author': 'PabloG', 'author_id': 394, 'author_profile': 'https://Stackoverflow.com/users/394', 'pm_score': 2, 'selected': True, 'text': '<p>You can use this code (Python)</p>\n\n<h1>Rename international files</h1>\n\n<pre><code># -*- coding: cp1252 -*-\n\nimport os, shutil\n\nbase_dir = "g:\\\\awk\\\\"    # Base Directory (includes subdirectories)\nchar_table_1 = "áéíóúñ"\nchar_table_2 = "aeioun"\n\nadirs = os.walk (base_dir)\n\nfor adir in adirs:\n    dir = adir[0] + "\\\\"          # Directory\n    # print "\\nDir : " + dir\n\n    for file in adir[2]:    # List of files\n        if os.access(dir + file, os.R_OK):\n            file2 = file\n            for i in range (0, len(char_table_1)):\n                file2 = file2.replace (char_table_1[i], char_table_2[i])\n\n            if file2 &lt;&gt; file:\n                # Different, rename\n                print dir + file, " =&gt; ", file2\n                shutil.move (dir + file, dir + file2)\n\n###\n</code></pre>\n\n<p>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.</p>\n'}]
['https://Stackoverflow.com/questions/56913', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/914/']