我如何分割(使用批处理脚本)下载网址,例如
[with slash]
a) http://www.someone.com/download/files/filename.exe
b) http://www.someone other.com/some files/filename 1.exe (with spaces !)
[with backslash]
c) http:\\www.someone.com\download\files\filename.exe
d) http:\\www.someone other.com\some files\filename 1.exe (with spaces !)
[mixed slash and backslash]
e) http:\\www.someone.com/download\files/filename.exe
f) http://www.someone other.com\some files/filename 1.exe (with spaces !)
and all of the above when there is no HTTP part (starting with "www.")
进入(Base-)URL部分和文件名部分。
所有变体的一个批处理脚本(以及一个循环中的多个变量)会很好: - )
类似的东西:
setlocal EnableDelayedExpansion
For %%G in (var1, var2, var3) do (
set /A COUNT+=1
... routine to split "var1", "var2" and "var3" into temporary variables BASEURL and FILENAME
set Folder!COUNT!=%BASEURL%
set Name!COUNT!=%FILENAME%
)
Output:
For /L %%A in (1,1,3) do (
echo URL: !Folder%%A! --- FIILENAME: !Name%%A!
)
答案 0 :(得分:1)
@ECHO OFF
SETLOCAL
SET download=https://www.someone.com/download/files/filename.exe&CALL :splitme
SET download=https://www.someone other.com/some files/filename 1.exe&CALL :splitme
SET download=https:\\www.someone.com\download\files\filename.exe&CALL :splitme
SET download=https:\\www.someone other.com\some files\filename 1.exe&CALL :splitme
SET download=https:\\www.someone.com/download\files/filename.exe&CALL :splitme
SET download=https://www.someone other.com\some files/filename 1.exe&CALL :splitme
SET download=http://www.someone.com/download/files/filename.exe&CALL :splitme
SET download=http://www.someone other.com/some files/filename 1.exe&CALL :splitme
SET download=http:\\www.someone.com\download\files\filename.exe&CALL :splitme
SET download=http:\\www.someone other.com\some files\filename 1.exe&CALL :splitme
SET download=http:\\www.someone.com/download\files/filename.exe&CALL :splitme
SET download=http://www.someone other.com\some files/filename 1.exe&CALL :splitme
SET download=//www.someone.com/download/files/filename.exe&CALL :splitme
SET download=//www.someone other.com/some files/filename 1.exe&CALL :splitme
SET download=\\www.someone.com\download\files\filename.exe&CALL :splitme
SET download=\\www.someone other.com\some files\filename 1.exe&CALL :splitme
SET download=\\www.someone.com/download\files/filename.exe&CALL :splitme
SET download=//www.someone other.com\some files/filename 1.exe&CALL :splitme
GOTO :eof
:splitme
IF NOT "%download%"=="%download:*:=%" SET "download=%download:*:=%"
FOR /f "tokens=1*delims=\/" %%i IN ("%download%") DO (
SET domain=%%i&SET file=%%~nxj&SET lop=//%%i&SET "pol=%%~nxj"
)
:lop
SET download=%download:~1%&set lop=%lop:~0,-1%&IF DEFINED lop GOTO lop
:pol
SET download=%download:~0,-1%&set pol=%pol:~0,-1%&IF DEFINED pol GOTO pol
ECHO + "%domain%" + "%download%" + "%file%"
GOTO :eof
产地:
解释很简单。
答案 1 :(得分:0)
@Echo OFF
FOR /F "Usebackq Tokens=*" %%@ IN ("MyURLs.txt") DO (
Call :CorrectURL "%%@"
Call :GetBase "%%URL%%"
Call :GetFile "%%URL%%"
Call Echo [+] Base: %%Base%% File: %%File%%
)
Pause&Exit
:CorrectURL
(Set "URL=%~1" & Call Set "URL=%%URL:\=/%%" & Call Set "URL=%%URL:http://=%%") & (Goto:EOF)
:GetBase
(FOR /F "Tokens=1,* Delims=/" %%B IN ('Echo %~1') DO (Set "Base=%%B")) & (Goto:EOF)
:GetFile
(Set "File=%~nx1") & (Goto:EOF)
第一个sub: CorrectURL 替换斜杠的反斜杠,然后安全从网址中删除“ http:// ”字符串。
第二个子: GetBase 使用斜杠作为分隔符来拆分字符串,只获取第一个基本网址的标记。
第三个子: GetFile 将URL视为路径,因此我们可以使用特殊的var修饰符轻松获取文件名。
<强>输出强>:
[+] Base: www.someone.com File: filename1.exe
[+] Base: www.someone other.com File: filename 2.exe
[+] Base: www.someone.com File: filename3.exe
[+] Base: www.someone other.com File: filename 4.exe
[+] Base: www.someone.com File: filename5.exe
[+] Base: www.someone other.com File: filename 6.exe
如果那不是您想要的输出,只需告诉我对代码进行更改。
答案 2 :(得分:0)
如果网址在File.txt
中,则此处可用@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in (file.txt) do (
set "var=%%~dpa"
set "var=!var:%cd%\=!"
set "var=!var:\=/!"
set "var=!var:/=/!"
echo "!var!" "%%~nxa"
)
pause
::file.txt
http://www.someone.com/download/files/filename1.exe
http://www.someone other.com/some files/filename 2.exe
http:\\www.someone.com\download\files\filename3.exe
http:\\www.someone other.com\some files\filename 4.exe
http:\\www.someone.com/download\files/filename5.exe
http://www.someone other.com\some files/filename 6.exe
www.someone.com/download/files/filename7.exe
www.someone other.com/some files/filename 8.exe
www.someone.com\download\files\filename9.exe
www.someone other.com\some files\filename A.exe
www.someone.com/download\files/filenameB.exe
www.someone other.com\some files/filename C.exe
答案 3 :(得分:0)
谢谢大家的回答。但我想拆分保存在变量中的DOWNLOAD-URL。 我忘了说我也想在FTP地址上编写脚本。 但是,借助你的答案以及网上的一些进一步研究,我找到了自己的解决方案。
@echo off
setlocal EnableExtensions
setlocal EnableDelayedExpansion
mode con:cols=120 lines=32 && color 3E
title Example for splitting Download-URL into URL and Filename
cls
set _BASEURL=ftp://ftp.rarlab.com/rar/
set _WRx32=http://www.rarlab.com/rar/wrar v4.20.exe
set _WRx64=http:\/www.rarlab.com\rar files/winrar-x64-420.exe
set _WRLNG=rarlng.rar
:SPLIT
set COUNT=0
echo:_BASEURL = %_BASEURL%
echo:_WRx32 = %_WRx32%
echo:_WRx64 = %_WRx64%
echo:_WRLNG = %_WRLNG%
echo:_____________________________________________________________________________
For %%A in ("%_BASEURL%", "%_WRx32%", "%_WRx64%", "%_WRLNG%") do (
set Folder=
set Name=
set /A COUNT+=1
if not %%~xA.==. (
set "_vartmp=%%~A" ) else (
set "_vartmp=%%~A\NO_FILE.EXT" )
for %%B in ("Z:\!_vartmp!") do set "FILE!COUNT!=%%~nxB"
for %%B in ("Z:\!_vartmp!") do set "_VAR=%%~dpB"
set "_VAR=!_VAR:Z:\=!"
if not $!_VAR!==$ (
set "_VAR=!_VAR::\=:\\!"
set "URL!COUNT!=!_VAR:~0,-1!" ) else (
set "URL!COUNT!=NO_URL" )
)
set _BASEURL=%URL1%
set COUNT=0
:LOOPOUT
set /A COUNT+=1
if not defined URL%COUNT% exit /b 1
if !URL%COUNT%!==NO_URL set "URL!COUNT!=%_BASEURL%"
echo URL%COUNT%: !URL%COUNT%!\ --- FILE%COUNT%: !FILE%COUNT%!
goto LOOPOUT
SETLOCAL DisableDelayedExpansion
goto :EOF
首先我将下载URL保存到变量中,然后将其打印出来。现在我将链接拆分为 网址和文件名。然后我将URL和文件名保存到动态变量(URL +连续编号 和FILE +相同的连续编号)。如您所见,变量“_WRLNG”没有URL,因此保存字符串“NO_URL”而不是URL。变量“_BASEURL”中的链接没有文件名,因此保存字符串“DUMMY.EXT”。必须这样做才能没有空/缺失 URL + Nr / FILE + Nr变量并获得正确的1:1相关性。
首先添加(虚拟)驱动器号(“Z:\”)然后将其从下载URL中删除,从而可以使用“参数扩展”来完成拆分。使用“~dp”命令处理器更改 所有斜杠到反斜杠 - 但它也删除了双斜杠/反斜杠所以第二个反斜杠 在必须再次添加“http:\”或“ftp:\”之后(将“:\”替换为“:\”
现在, URL1 中保存的URL被分配给变量“_BASEURL”(任何其他URL都可以 used - 它只是一个例子,因为我从INI文件中读取变量并使用此脚本 如果用户使用斜杠而不是反斜杠,则更正_BASEURL的表示法。)变量 使用“_BBASEURL”(此处仅用于输出),其中只保存文件名以及网址。
在第
行echo URL%COUNT%:!URL%COUNT%!\ --- FILE%COUNT%:!FILE%COUNT%!
“.. \”和“---...”之间有1个空格和2个制表符。插入我使用的TAB Windows记事本中的ALT + 009(在小键盘上)(保存前的最后一步)。这样做 输出应该被视为制表符分隔列表。
我希望解释是可以理解的。