开发者

How Can I Rar a File with a Unicode Name in Batch

I wrote a small batch file to rar all contents in all subfolders within a folder. It works fine except for that the file names should be in ASCII. Below is the working small code:

@REM ------- BEGIN ra开发者_JAVA技巧rthem.bat ----------------
@setlocal
@echo off
echo --------------------------------------------------
echo Starting to rar files
echo --------------------------------------------------
echo.
echo.
echo.
set path="C:\Program Files\WinRAR\";%path%
for /D /r %%G in ("*") do (
    echo Storing files in %%G
    echo --------------------------------------------------

    cd %%G

    for %%I in (*.*) do (
        rar a -x*.rar -x*.zip -m0 -id[c] "%%~nI.rar" "%%I"
        echo Done archiving %%~nI%%~xI
    )

    echo --------------------------------------------------
    echo Done archiving %%~nG
    echo --------------------------------------------------
    echo.
    echo.
    cd ..
)

echo Finished!
pause
REM ------- END rarthem.bat ------------------

I think the problem happens when the file name is parsed to WinRAR as WinRAR has no problem archiving files with Unicode names.

Edit: When parsing the file name to WinRAR, the file name gets modified so when WinRAR tries to look for the file under that name, it can't find it. For example a file: téxt.pdf will become text.pdf when parsed.

A small side question: I've not tried using 7zip, would it be easier to achieve the same thing with 7zip?

Many thanks for help.


RAR.exe processes its command-line in the OEM character set (i.e. not unicode). The only way I know of to pass a unicode name to it is via a list file, when also using the argument to specify that the list file is unicode. However, that only works for files inside the archive (EDIT: or files you want to add to the archive). For the archive name itself I don't know of a solution except...

If you use WinRAR.exe instead of RAR.exe then you can pass unicode filenames on the command-line and they work fine. You will see a GUI progress window but other than that (which may or may not matter to you) WinRAR.exe is suitable for running from batch scripts.

Whether a batch script itself can cope with unicode I do not know, but if that is the only problem remaining I would switch to using VBScript or JScript instead of a batch file. (Which is worth doing anyway, IMO. I'm not a big fan of VBScript and JScript but at least they don't have completely insane, arcane semantics and limitations like batch does. :))

By the way, if you do use WinRAR.exe you might want to get the recent WinRAR 4 (beta 2 currently) as it includes the ability to specify the working directory, previously only possible with rar.exe. That can be essential if you need to add files from read-only directories.

If you want to learn about the list files I mentioned, check the RAR.exe text-file document or the WinRAR.exe built-in online help for all the details.

Hope that helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜