批处理批量替换txt内容指定文字,输出结果中文乱码问题,怎么解决。
我查了一下,好像说是编码问题,批处理默认输出ANSI编码,而我的txt文本是utf8编码的。执行替换后,文本被转成了ANSI,所以中文就乱码了。请教,以下代码该怎么修改?...
我查了一下,好像说是编码问题,批处理默认输出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 展开
附:
@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 展开
1个回答
展开全部
@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
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询