如何在批处理中将URL拆分为其组成部分?

3
我有一些长度未知的文件URL输入到我的批处理脚本中。

http://Repository.com/Stuff/Things/Repo/file1.csv

http://www.place.com/Folder/file2.xml

他们之间几乎没有任何一致性。我需要一种仅使用批处理的方法(尽管从批处理内调用PowerShell也是一种选择)来将它们分解为完整的路径和文件名。

http://Repository.com/Stuff/Things/Repo/

http://www.place.com/Folder/

file1.csv

file2.xml

我看过很多其他语言的做法,但是我只会批处理,它并不是我的强项。我尝试使用一个带有“delims=/”的远程/f循环,但是当它到达//时就会停止运行。
4个回答

4
@echo off
setlocal EnableDelayedExpansion

set "url=http://Repository.com/Stuff/Things/Repo/file1.csv"

for %%a in ("%url%") do (
   set "urlPath=!url:%%~NXa=!"
   set "urlName=%%~NXa"
)
echo URL path: "%urlPath%"
echo URL name: "%urlName%"

输出:

URL path: "http://Repository.com/Stuff/Things/Repo/"
URL name: "file1.csv"

这个完全符合我今天想要的,而且没有产生多语言混合的陈述。谢谢。 - undefined
很好的方法,@Aacini,+1!我猜如果最后一部分在URL中出现多次,比如http://repo.com/file.csv/file.csv,它可能会失败... - undefined
...一个解决方法是在URL中添加一个不允许的字符,例如|,并在for循环之后将其删除,@Aacini; - undefined
如何从URL中仅获取主机名?也就是说,在上述给定的URL中只获取"Repository.com"。 - undefined
1
@JayakumariArumugham: for /F "tokens=2 delims=/" %%a in ("%url%") do set "host=%%a" - undefined

3
在PowerShell中,您可以将URL字符串转换为System.Uri类,该类提供有关URL及其结构的广泛信息。您可能需要使用Uri.Segments属性进行操作,如下所示:
PS C:\> # get System.Uri object:
PS C:\> $uri = [uri]"http://Repository.com/Stuff/Things/Repo/file1.csv"
PS C:\> $uri


AbsolutePath   : /Stuff/Things/Repo/file1.csv
AbsoluteUri    : http://repository.com/Stuff/Things/Repo/file1.csv
LocalPath      : /Stuff/Things/Repo/file1.csv
Authority      : repository.com
HostNameType   : Dns
IsDefaultPort  : True
IsFile         : False
IsLoopback     : False
PathAndQuery   : /Stuff/Things/Repo/file1.csv
Segments       : {/, Stuff/, Things/, Repo/...}
IsUnc          : False
Host           : repository.com
Port           : 80
Query          :
Fragment       :
Scheme         : http
OriginalString : http://Repository.com/Stuff/Things/Repo/file1.csv
DnsSafeHost    : repository.com
IsAbsoluteUri  : True
UserEscaped    : False
UserInfo       :



PS C:\> # get base URL without page name and query parameters:
PS C:\> $uri.Scheme + ":/" + $uri.Authority + (-join $uri.Segments[0..($uri.Segments.Length - 2)])
http:/repository.com/Stuff/Things/Repo/
PS C:\> # get page/file name:
PS C:\> $uri.Segments[-1]
file1.csv

这在PowerShell ISE中可以工作,但我无法让它作为PowerShell命令($uri=[uri]"%url%"; $uri.Segments[-1];)正常工作。我正在尝试将其整合到批处理解决方案中,以完成其他工作。 - undefined

0

使用字符串类的splitSubString方法。

例如:

$filename = $url.split('/')[-1]
# $url.split('/') splits the url on the '/' character. [-1] takes the last part
$rest = $url.SubString(0, $url.Length - $filename.Length)
# The first parameter is the starting index of the substring, the second is the length.

0
@echo off
Set input=http://Repository.com/Stuff/Things/Repo/file1.csv
SETLOCAL ENABLEDELAYEDEXPANSION
For %%A in ("%input%") do (
    Set url= %%~pA
    set url=!url:~2!
    Set fileName=%%~nxA
)
echo.URL is: %url%
echo.File Name is: %fileName%
ENDLOCAL


输出

enter image description here


感谢您提供的第一个纯批处理解决方案,但是正斜杠被改成了反斜杠,并且第一组两个正斜杠被改成了一个。这将破坏脚本的其余部分。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接