1

I'm trying to remove a list of files that I have in a textfile. I'm essentially trying to do this:

del 二月.mp3 /f

It doesn't seem to work because of the UTF-8 characters. I searched around a lot and haven't found a way to get around this. Anyone got a solution for this?

I'm trying to delete 10,000+ files from a folder using a text file list I have of those files. The files each have Japanese characters in the name.

Thank you :)

Vladz0r
  • 27
  • 3
  • 3
    `rm` is not a standard command in a Windows batch file, is there some alternative environment that you haven't reported in your question? – Compo Feb 03 '17 at 18:41
  • 1
    make sure your editor is using UTF8 encoding when creating your BAT file. Or use the short name of the file, that you can obtain with `dir /x` – PA. Feb 03 '17 at 19:59
  • I meant to write del instead of rm. I made sure my bat file is in UTF-8 encoding. I'm trying to delete 10,000+ files from a list, and the files have Japanese characters in them, so I have to use some sort of scripting to do it. – Vladz0r Feb 03 '17 at 22:05
  • Have a look on [this question and its answers](http://stackoverflow.com/questions/3780378/how-to-display-japanese-kanji-inside-a-cmd-window-under-windows) – geisterfurz007 Feb 04 '17 at 19:06

1 Answers1

2

Following batch script and sample text file show that CMD/Batch is capable to work with file names from UCS-2 Unicode subset providing that

(CJK Ideographs and Central European accented Latin letters are used in following examples, both explicitly used in a batch command and read from a saved list of such file names.)

Note that the weird first line in bat script (@foo.bar >NUL 2>&1) is there as cmd is UTF-8 aware but isn't BOM aware (cf byte order mark):

  • if script is saved without BOM then this line would be performed with no effect while
  • if script is saved with BOM then this line would be listed prefixed with some character(s), see  in the following output.

Note that the first line in txt file list is empty for the same reason (cmd isn't BOM aware) so for /F loop would ignore this empty line if BOM is not present otherwise an attempt to do something with such file raises File Not Found error (this message could be suppressed using 2>NUL).

42030220.bat script:

@foo.bar >NUL 2>&1
@echo OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
chcp 65001
echo next command: for /F "usebackq delims=" %%G …
for /F "usebackq delims=" %%G in ("D:\bat\files\unASCII\42030220.txt") do (
    dir /B /S "%%~G"
    REM
    REM use `del /S "%%~G"` instead of `dir /B /S "%%~G"` 
)
echo next command: dir /B /S /A:-D "D:\bat\Unusual Names\CJK\中文(繁體)\*"
dir /B /S /A:-D "D:\bat\Unusual Names\CJK\中文(繁體)\*"

42030220.txt file list (note leading empty line):

 
二月.mp3
ěščřžýáíé.txt
装备女印度舞娘时装上衣.rtf

Output:

d:\bat> d:\bat\SO\42030220.bat

d:\bat> @foo.bar  1>NUL 2>&1
Active code page: 65001
next command: for /F "usebackq delims=" %G …
File Not Found
d:\bat\Unusual Names\CJK\中文(繁體)\二月.mp3
d:\bat\UnASCII Names\ěščřžýáíé.txt
d:\bat\Unusual Names\CJK\中文(繁體)\装备女印度舞娘时装上衣.rtf
next command: dir /B /S /A:-D "D:\bat\Unusual Names\CJK\中文(繁體)\*"
D:\bat\Unusual Names\CJK\中文(繁體)\chinese traditional.txt
D:\bat\Unusual Names\CJK\中文(繁體)\二月.mp3
D:\bat\Unusual Names\CJK\中文(繁體)\装备女印度舞娘时装上衣.rtf

d:\bat>
JosefZ
  • 28,460
  • 5
  • 44
  • 83