Search code examples
utf-8batch-filefile-rename

file names uses utf8


I have a text file named rename.txt as follows:

34aa85ff2fb46b29fba2283a7b889480306295.flv|بسوی-زیبایی.flv
bb32ca4604217660ab7b6df3938cd0df306294.flv|صدای-تو.flv
b4b802c0182ebfd4fbba9c5ad2ab3904306286.flv|بسوی-حق.flv

when i used bulk rename utility to rename flv files on the left of the | to the corresponding ones to the right i got:

bulk rename utility scshot

bulk rename result

Then I tried using a batch file to rename files but the same problem in the cmd:

@echo off
for /f "tokens=*" %%a in (rename.txt) do (
  echo %%a
)
pause

cmd output:

cmd output

Note: i can rename the files manually in windows:

expected result

How can i get the expected result guys?


Solution

  • The following worked for me:

    @ECHO OFF
    CHCP 65001
    FOR /F "tokens=1,2 delims=|" %%a IN (rename.txt) DO (
      RENAME "%%a" "%%b"
    )
    

    Make sure that the text file (rename.txt) doesn't contain the UTF-8 signature (otherwise called Byte Order Mark, BOM) at the beginning, because the FOR /F command tries to treat it as a character and attaches it to the first file's name. As a result the first file doesn't get renamed (but all others do).

    If you can't get rid of the BOM, just put an empty line at the beginning, so that the BOM stays on that line while other lines, which contain the names, remain "clean". If you add that empty line and do not change the above script, it will produce an error when trying to process the empty line. It shouldn't be a problem, but if you would like to eliminate it, you could do it like this:

    @ECHO OFF
    CHCP 65001
    FOR /F "tokens=1,2 delims=|" %%a IN (rename.txt) DO (
      IF NOT "%%b" == "" RENAME "%%a" "%%b"
    )