如何用bat批处理txt中的URL编码?
123.txt文本中每行有一个链接,链接中含有中文,怎么样使用bat命令进行编码?如:处理前http://www.baidu.com/测试01.mp4http://www...
123.txt文本中每行有一个链接,链接中含有中文,怎么样使用bat命令进行编码?
如:
处理前
http://www.baidu.com/测试01.mp4
http://www.baidu.com/测试02.mp4
http://www.baidu.com/测试03.mp4
http://www.baidu.com/测试04.mp4
处理后
http://www.baidu.com%E6%B5%8B%E8%AF%9501.mp4
http://www.baidu.com/%E6%B5%8B%E8%AF%9502.mp4
http://www.baidu.com/%E6%B5%8B%E8%AF%9503.mp4
http://www.baidu.com/%E6%B5%8B%E8%AF%9504.mp4 展开
如:
处理前
http://www.baidu.com/测试01.mp4
http://www.baidu.com/测试02.mp4
http://www.baidu.com/测试03.mp4
http://www.baidu.com/测试04.mp4
处理后
http://www.baidu.com%E6%B5%8B%E8%AF%9501.mp4
http://www.baidu.com/%E6%B5%8B%E8%AF%9502.mp4
http://www.baidu.com/%E6%B5%8B%E8%AF%9503.mp4
http://www.baidu.com/%E6%B5%8B%E8%AF%9504.mp4 展开
展开全部
@echo off & title 转换为URL编码 By 依梦琴瑶
cd /d %~dp0
::设置处理的文件
set File=123.txt
for /f "tokens=1 delims=:" %%a in ('findstr /n "^::CreatUrlEncode::$" "%~0"') do more +%%a "%~0">UrlEncode.vbs
(for /f "delims=" %%a in ('type "%File%"') do (
for /f "delims=" %%i in ('cscript /nologo UrlEncode.vbs "%%~a"') do echo %%~i
))>New$
move /y New$ "%File%" >nul
del /f /q UrlEncode.vbs
pause
exit
::CreatUrlEncode::
'UrlEncode By Demon
Function UrlEncode(str)
Dim i,c,s,length
length = Len(str)
For i = 1 To length
s = Mid(str,i,1)
c = "&H" & Hex(AscW(Mid(str,i,1)))
If ( c >= AscW("A") And c <= AscW("Z") ) Or _
( c >= AscW("a") And c <= AscW("z") ) Or _
( c >= AscW("0") And c <= AscW("9") ) Or _
( c = AscW("-") Or c = AscW("_") Or c = AscW(".") ) Or _
( c = Asc(":") Or c = Asc("/") ) Then
UrlEncode = UrlEncode & s
ElseIf c = AscW(" ") Then
UrlEncode = UrlEncode & "+"
Else
If c >= &H0001 And c <= &H007F Then
UrlEncode = UrlEncode & s
ElseIf c > &H07FF Then
UrlEncode = UrlEncode & "%" & Hex(&HE0 Or (c\(2^12) And &H0F))
UrlEncode = UrlEncode & "%" & Hex(&H80 Or (c\(2^6) And &H3F))
UrlEncode = UrlEncode & "%" & Hex(&H80 Or (c\(2^0) And &H3F))
Else
UrlEncode = UrlEncode & "%" & Hex(&HC0 Or (c\(2^6) And &H1F))
UrlEncode = UrlEncode & "%" & Hex(&H80 Or (c\(2^0) And &H3F))
End If
End If
Next
End Function
WScript.Echo UrlEncode(WScript.Arguments(0))
追问
非常感谢您的回答,对我起到了很大的帮助,谢谢您!
我现在又有一个新问题,不知道您愿不愿意为我回答
还有一个问题,不知道您能不能也帮助我一下,关于txt内容换行。问题:在ok.txt文件中,1、在“http”前面换行,2、在“MP4”后换行。效果如下
追答
@echo off & title URL换行 By 依梦琴瑶
cd /d %~dp0
::设置待处理的文件
set File=123.txt
for /f "delims=" %%a in ('type "%File%"') do (
set "Str=%%~a"
call :SplitURLs "%%Str:.MP4http=.MP4" "http%%"
)
move /y New$ "%File%" >nul
pause
exit
:SplitURLs
(for %%i in (%*) do echo %%~i)>New$
goto :eof
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询