我查了一下,好像说是编码问题,批处理默认输出ANSI编码,而我的txt文本是utf8编码的。执行替换后,文本被转成了ANSI,所以中文就乱码了。请教,以下代码该怎么修改?
附:
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /b "*.txt"') do (
for /f "skip=2 delims=" %%a in ('find /v "%%" "%%i"') do (
set "word1=%%a"
set "word1=!word1:替换前=替换后!"
set "word2=!word1!"
echo,!word2! >>"%%~ni_tmp.txt"
)
del "%%i" /f /q
ren "%%~ni_tmp.txt" "%%i"
)
pause
批处理批量替换txt内容指定文字,输出结果中文乱码问题,怎么解决。
答案:1 悬赏:0 手机版
解决时间 2021-02-02 22:20
- 提问者网友:呐年旧曙光
- 2021-02-02 06:32
最佳答案
- 五星知识达人网友:酒者煙囻
- 2021-02-02 06:44
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /b "*.txt"') do (
set aCode=UTF-8
set bCode=GB2312
call :CreatConvertTool
ConvertTool.vbs "%%~i" "%%~i.ansi"
for /f "skip=2 delims=" %%a in ('find /v "%%" "%%~i.ansi"') do (
set "word1=%%a"
set "word1=!word1:你好=替换后!"
set "word2=!word1!"
echo,!word2! >>"%%~ni.tmp"
)
del "%%~i" "%%~i.ansi" /f /q
set aCode=GB2312
set bCode=UTF-8
call :CreatConvertTool
ConvertTool.vbs "%%~ni.tmp" "%%~i"
del "%%~ni.tmp" /f /q
)
pause
exit
:CreatConvertTool
(echo aCode = "%aCode%"
echo bCode = "%bCode%"
echo Set objArgs = WScript.Arguments
echo.
echo FileUrlSrc = objArgs^(0^)
echo FileUrlDst = objArgs^(1^)
echo Call WriteToFile^(FileUrlDst, ReadFile^(FileUrlSrc, aCode^), bCode^)
echo.
echo Function ReadFile^(FileUrlSrc, CharSet^)
echo Dim Str
echo Set stm = CreateObject^("Adodb.Stream"^)
echo stm.Type = 2
echo stm.mode = 3
echo stm.charset = CharSet
echo stm.Open
echo stm.loadfromfile FileUrlSrc
echo Str = stm.readtext
echo stm.Close
echo Set stm = Nothing
echo ReadFile = Str
echo End Function
echo.
echo Function WriteToFile ^(FileUrlDst, Str, CharSet^)
echo Set stm = CreateObject^("Adodb.Stream"^)
echo stm.Type = 2
echo stm.mode = 3
echo stm.charset = CharSet
echo stm.Open
echo stm.WriteText Str
echo stm.SaveToFile FileUrlDst, 2
echo stm.flush
echo stm.Close
echo Set stm = Nothing
echo End Function)>ConvertTool.vbs
goto :eof
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /b "*.txt"') do (
set aCode=UTF-8
set bCode=GB2312
call :CreatConvertTool
ConvertTool.vbs "%%~i" "%%~i.ansi"
for /f "skip=2 delims=" %%a in ('find /v "%%" "%%~i.ansi"') do (
set "word1=%%a"
set "word1=!word1:你好=替换后!"
set "word2=!word1!"
echo,!word2! >>"%%~ni.tmp"
)
del "%%~i" "%%~i.ansi" /f /q
set aCode=GB2312
set bCode=UTF-8
call :CreatConvertTool
ConvertTool.vbs "%%~ni.tmp" "%%~i"
del "%%~ni.tmp" /f /q
)
pause
exit
:CreatConvertTool
(echo aCode = "%aCode%"
echo bCode = "%bCode%"
echo Set objArgs = WScript.Arguments
echo.
echo FileUrlSrc = objArgs^(0^)
echo FileUrlDst = objArgs^(1^)
echo Call WriteToFile^(FileUrlDst, ReadFile^(FileUrlSrc, aCode^), bCode^)
echo.
echo Function ReadFile^(FileUrlSrc, CharSet^)
echo Dim Str
echo Set stm = CreateObject^("Adodb.Stream"^)
echo stm.Type = 2
echo stm.mode = 3
echo stm.charset = CharSet
echo stm.Open
echo stm.loadfromfile FileUrlSrc
echo Str = stm.readtext
echo stm.Close
echo Set stm = Nothing
echo ReadFile = Str
echo End Function
echo.
echo Function WriteToFile ^(FileUrlDst, Str, CharSet^)
echo Set stm = CreateObject^("Adodb.Stream"^)
echo stm.Type = 2
echo stm.mode = 3
echo stm.charset = CharSet
echo stm.Open
echo stm.WriteText Str
echo stm.SaveToFile FileUrlDst, 2
echo stm.flush
echo stm.Close
echo Set stm = Nothing
echo End Function)>ConvertTool.vbs
goto :eof
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯