oc
A Windows batch OpenCC wrapper similar to tc but uses OpenCC’s rapidly pre-compiled binaries instead of the outdated op
It’s rather personal and messy so not too much explanation will be given—
By default, it converts the filename and the contents of the file into traditional Chinese (Republic of China–orientated); pass -r for rename tasks only; a plenty of alias switches are drafted for other schemes as seen in the code; use -c to call those schemes (must be in their official names) I so rarely use that I didn’t even bother to make mnemonic aliases for them.
By default, after contents conversion, a scheme name tail tag will be appended to the filename of the converted file; pass -n to try ‘no-tail mode’; pass -o to designate the output filename.
A slimmed down version dedicated to rename tasks only and a .REG file used for context menu injections are attached beneath the main batch.
@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
set "a="
set "r="
set "n="
set "i="
set "odp="
set "on="
set "ox="
set "d="
set "ddp="
set "dx="
set "m="
set "c=s2twp"
set "nn="
set "nd="
set "int="
set "r2="
set "r32="
set "str=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"
for /l %%i in (1,1,32) do (
set /a int=!random!%%63
for %%j in (!int!) do set "r32=!r32!!str:~%%j,1!"
)
for /l %%i in (1,1,2) do (
set /a int=!random!%%63
for %%j in (!int!) do set "r2=!r2!!str:~%%j,1!"
)
:a
if /i "%~1"=="" goto p
set "a=%~1"
if /i "!a:~0,1!"=="-" (
if /i "%a%"=="-r" set "r=1"
if /i "%a%"=="-n" set "n=1"
if /i "%a%"=="-j2t" set "c=jp2t"
if /i "%a%"=="-jp2t" set "c=jp2t"
if /i "%a%"=="-jpt" set "c=jp2t"
if /i "%a%"=="-jptt" set "c=jp2t"
if /i "%a%"=="-jt" set "c=jp2t"
if /i "%a%"=="-jtt" set "c=jp2t"
if /i "%a%"=="-s2tc" set "c=s2t"
if /i "%a%"=="-stc" set "c=s2t"
if /i "%a%"=="-sttc" set "c=s2t"
if /i "%a%"=="-s2t" set "c=s2tw"
if /i "%a%"=="-s2tw" set "c=s2tw"
if /i "%a%"=="-st" set "c=s2tw"
if /i "%a%"=="-stt" set "c=s2tw"
if /i "%a%"=="-sttw" set "c=s2tw"
if /i "%a%"=="-t" set "c=s2tw"
if /i "%a%"=="-tt" set "c=s2tw"
if /i "%a%"=="-j" set "c=t2jp"
if /i "%a%"=="-jp" set "c=t2jp"
if /i "%a%"=="-t2j" set "c=t2jp"
if /i "%a%"=="-t2jp" set "c=t2jp"
if /i "%a%"=="-tj" set "c=t2jp"
if /i "%a%"=="-tjp" set "c=t2jp"
if /i "%a%"=="-ttj" set "c=t2jp"
if /i "%a%"=="-ttjp" set "c=t2jp"
if /i "%a%"=="-t2sc" set "c=t2s"
if /i "%a%"=="-tsc" set "c=t2s"
if /i "%a%"=="-ttsc" set "c=t2s"
if /i "%a%"=="-t2tw" set "c=t2tw"
if /i "%a%"=="-tttw" set "c=t2tw"
if /i "%a%"=="-ttw" set "c=t2tw"
if /i "%a%"=="-tw2s" set "c=tw2s"
if /i "%a%"=="-tws" set "c=tw2s"
if /i "%a%"=="-twts" set "c=tw2s"
if /i "%a%"=="-2s" set "c=tw2sp"
if /i "%a%"=="-2sp" set "c=tw2sp"
if /i "%a%"=="-s" set "c=tw2sp"
if /i "%a%"=="-t2s" set "c=tw2sp"
if /i "%a%"=="-t2sp" set "c=tw2sp"
if /i "%a%"=="-ts" set "c=tw2sp"
if /i "%a%"=="-tsp" set "c=tw2sp"
if /i "%a%"=="-ttsp" set "c=tw2sp"
if /i "%a%"=="-tw2sp" set "c=tw2sp"
if /i "%a%"=="-twsp" set "c=tw2sp"
if /i "%a%"=="-twtsp" set "c=tw2sp"
if /i "%a%"=="-c" (
set "c=%~2" & shift
echo !c! | findstr /i "hk2s hk2t jp2t s2hk s2t s2tw s2twp t2hk t2jp t2s t2tw tw2s tw2sp tw2t" >nul
if errorlevel 1 (
set "c=s2twp"
) else (
set "m=-c %c%.json"
)
)
if /i "%a%"=="-o" set "d=%~2" & set "ddp=%~dp2" & set "dx=%~x2" & shift
) else (
if not defined i set "i=%~1"
)
shift
goto a
:p
if not defined i goto e
set "m=-c %c%.json"
for %%a in ("%i%") do set "odp=%%~dpa" & set "on=%%~na" & set "ox=%%~xa"
echo "%on%">%r32%.txt
opencc --path C:\_c\oc\ %m% -i %r32%.txt -o %r32%%c%.txt
for /f "delims=" %%b in (%r32%%c%.txt) do set "nn=%%~b"
del %r32%.txt
del %r32%%c%.txt
if /i "%r%"=="1" (
ren "%i%" "%nn%%ox%"
goto e
)
if defined d (
if not exist "%ddp%" md "%ddp%"
if /i "%dx%"=="%ox%" (
set "nd=%d%"
) else (
set "nd=%d%%ox%"
)
if exist "!nd!" (
for %%a in ("!nd!") do set "nd=%%~dpna-%c%%%~xa"
)
if exist "!nd!" (
for %%a in ("!nd!") do set "nd=%%~dpna-%r2%%%~xa"
)
) else (
set "nd=%odp%%nn%-%c%%ox%"
)
opencc --path C:\_c\oc\ %m% -i "%i%" -o "%nd%"
if not defined d if /i "%n%"=="1" (
ren "%nd%" "%nn%%ox%"
)
:e
endlocal
@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
set "a="
set "i="
set "on="
set "ox="
set "m="
set "c=s2twp"
set "nn="
set "int="
set "r32="
set "str=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"
for /l %%i in (1,1,32) do (
set /a int=!random!%%63
for %%j in (!int!) do set "r32=!r32!!str:~%%j,1!"
)
:a
if /i "%~1"=="" goto p
set "a=%~1"
if /i "!a:~0,1!"=="-" (
if /i "%a%"=="-j2t" set "c=jp2t"
if /i "%a%"=="-jp2t" set "c=jp2t"
if /i "%a%"=="-jpt" set "c=jp2t"
if /i "%a%"=="-jptt" set "c=jp2t"
if /i "%a%"=="-jt" set "c=jp2t"
if /i "%a%"=="-jtt" set "c=jp2t"
if /i "%a%"=="-s2tc" set "c=s2t"
if /i "%a%"=="-stc" set "c=s2t"
if /i "%a%"=="-sttc" set "c=s2t"
if /i "%a%"=="-s2t" set "c=s2tw"
if /i "%a%"=="-s2tw" set "c=s2tw"
if /i "%a%"=="-st" set "c=s2tw"
if /i "%a%"=="-stt" set "c=s2tw"
if /i "%a%"=="-sttw" set "c=s2tw"
if /i "%a%"=="-t" set "c=s2tw"
if /i "%a%"=="-tt" set "c=s2tw"
if /i "%a%"=="-j" set "c=t2jp"
if /i "%a%"=="-jp" set "c=t2jp"
if /i "%a%"=="-t2j" set "c=t2jp"
if /i "%a%"=="-t2jp" set "c=t2jp"
if /i "%a%"=="-tj" set "c=t2jp"
if /i "%a%"=="-tjp" set "c=t2jp"
if /i "%a%"=="-ttj" set "c=t2jp"
if /i "%a%"=="-ttjp" set "c=t2jp"
if /i "%a%"=="-t2sc" set "c=t2s"
if /i "%a%"=="-tsc" set "c=t2s"
if /i "%a%"=="-ttsc" set "c=t2s"
if /i "%a%"=="-t2tw" set "c=t2tw"
if /i "%a%"=="-tttw" set "c=t2tw"
if /i "%a%"=="-ttw" set "c=t2tw"
if /i "%a%"=="-tw2s" set "c=tw2s"
if /i "%a%"=="-tws" set "c=tw2s"
if /i "%a%"=="-twts" set "c=tw2s"
if /i "%a%"=="-2s" set "c=tw2sp"
if /i "%a%"=="-2sp" set "c=tw2sp"
if /i "%a%"=="-s" set "c=tw2sp"
if /i "%a%"=="-t2s" set "c=tw2sp"
if /i "%a%"=="-t2sp" set "c=tw2sp"
if /i "%a%"=="-ts" set "c=tw2sp"
if /i "%a%"=="-tsp" set "c=tw2sp"
if /i "%a%"=="-ttsp" set "c=tw2sp"
if /i "%a%"=="-tw2sp" set "c=tw2sp"
if /i "%a%"=="-twsp" set "c=tw2sp"
if /i "%a%"=="-twtsp" set "c=tw2sp"
if /i "%a%"=="-c" (
set "c=%~2" & shift
echo !c! | findstr /i "hk2s hk2t jp2t s2hk s2t s2tw s2twp t2hk t2jp t2s t2tw tw2s tw2sp tw2t" >nul
if errorlevel 1 (
set "c=s2twp"
) else (
set "m=-c %c%.json"
)
)
) else (
if not defined i set "i=%~1"
)
shift
goto a
:p
if not defined i goto e
set "m=-c %c%.json"
for %%a in ("%i%") do set "on=%%~na" & set "ox=%%~xa"
echo "%on%">%r32%.txt
opencc --path C:\_c\oc\ %m% -i %r32%.txt -o %r32%%c%.txt
for /f "delims=" %%b in (%r32%%c%.txt) do set "nn=%%~b"
del %r32%.txt
del %r32%%c%.txt
ren "%i%" "%nn%%ox%"
:e
endlocal
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\OpenCC]
"MUIVerb"="OpenCC"
"SubCommands"=""
[HKEY_CLASSES_ROOT\*\shell\OpenCC\shell]
[HKEY_CLASSES_ROOT\*\shell\OpenCC\shell\1twpr]
@="Convert the filename into traditional Chinese"
[HKEY_CLASSES_ROOT\*\shell\OpenCC\shell\1twpr\command]
@="\"C:\\_c\\renc.bat\" \"%1\""
[HKEY_CLASSES_ROOT\*\shell\OpenCC\shell\2sr]
@="Convert the filename into simplified Chinese"
"CommandFlags"=dword:00000040
[HKEY_CLASSES_ROOT\*\shell\OpenCC\shell\2sr\command]
@="\"C:\\_c\\renc.bat\" \"%1\" -s"
[HKEY_CLASSES_ROOT\*\shell\OpenCC\shell\3twpc]
@="Convert the contents into traditional Chinese"
[HKEY_CLASSES_ROOT\*\shell\OpenCC\shell\3twpc\command]
@="\"C:\\_c\\oc.bat\" \"%1\""
[HKEY_CLASSES_ROOT\*\shell\OpenCC\shell\4sc]
@="Convert the contents into simplified Chinese"
"CommandFlags"=dword:00000040
[HKEY_CLASSES_ROOT\*\shell\OpenCC\shell\4sc\command]
@="\"C:\\_c\\oc.bat\" \"%1\" -s"
[HKEY_CLASSES_ROOT\*\shell\OpenCC\shell\5twpcn]
@="Convert the contents into traditional Chinese (try no-tail)"
[HKEY_CLASSES_ROOT\*\shell\OpenCC\shell\5twpcn\command]
@="\"C:\\_c\\oc.bat\" \"%1\" -n"
[HKEY_CLASSES_ROOT\*\shell\OpenCC\shell\6scn]
@="Convert the contents into simplified Chinese (try no-tail)"
[HKEY_CLASSES_ROOT\*\shell\OpenCC\shell\6scn\command]
@="\"C:\\_c\\oc.bat\" \"%1\" -s -n"