0

how can i split (with a batch script) Download-URLs like

[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.")

into the (Base-)URL part and the filename part.

One batch script for all variations (and for more than one variable - in a Loop) would be nice :-)

Something like:

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!
)
4

4 回答 4

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

产生:

  • “www.someone.com”+“/download/files/”+“文件名.exe”
  • “www.someone other.com”+“/一些文件/”+“文件名 1.exe”
  • “www.someone.com”+“\download\files\”+“文件名.exe”
  • “www.someone other.com”+“\一些文件\”+“文件名 1.exe”
  • “www.someone.com”+“/download\files/”+“文件名.exe”
  • “www.someone other.com”+“\一些文件/”+“文件名 1.exe”
  • “www.someone.com”+“/download/files/”+“文件名.exe”
  • “www.someone other.com”+“/一些文件/”+“文件名 1.exe”
  • “www.someone.com”+“\download\files\”+“文件名.exe”
  • “www.someone other.com”+“\一些文件\”+“文件名 1.exe”
  • “www.someone.com”+“/download\files/”+“文件名.exe”
  • “www.someone other.com”+“\一些文件/”+“文件名 1.exe”
  • “www.someone.com”+“/download/files/”+“文件名.exe”
  • “www.someone other.com”+“/一些文件/”+“文件名 1.exe”
  • “www.someone.com”+“\download\files\”+“文件名.exe”
  • “www.someone other.com”+“\一些文件\”+“文件名 1.exe”
  • “www.someone.com”+“/download\files/”+“文件名.exe”
  • “www.someone other.com”+“\一些文件/”+“文件名 1.exe”

解释是微不足道的。

于 2013-04-23T12:39:13.727 回答
0

谢谢大家的答案。但我想拆分保存在变量中的下载 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 和文件名保存到动态变量(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 - 这只是一个示例,因为我从 INI 文件中读取变量并使用此脚本来更正 _BASEURL 的符号,如果用户使用斜杠而不是反斜杠。)使用变量“_BBASEURL”(此处仅用于输出),其中仅保存文件名而没有 url。

在行

回显 URL%COUNT%: !URL%COUNT%!\ --- FILE%COUNT%: !FILE%COUNT%!

“..\”和“---...”之间有 1 个空格和 2 个制表符。要插入 TAB,我在 Windows 记事本中使用了 ALT+009(在小键盘上)(保存前的最后一步)。这样做的输出应该被视为制表符分隔的列表。

我希望解释是可以理解的。

于 2013-04-24T15:17:12.823 回答
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将反斜杠替换为斜杠,然后安全地从 url 中删除“ http:// ”字符串。

第二个子:GetBase使用斜杠作为分隔符来分割字符串,并且只获取第一个标记,它是基本 url。

第三个子: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

如果那不是您想要的输出,请告诉我更改代码。

于 2013-04-23T11:08:25.913 回答
0

如果 URL 在 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
于 2013-04-23T12:13:10.053 回答