如何在批处理中将URL分解为其组成部分?(How do I break apart a URL to its component parts in Batch?)

我有我的批处理脚本输入未知长度的文件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 = /”使用far / f循环,但是当它到达//时退出。

I have URLs of files of unknown length input into my batch script.

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

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

There is little to no consistency with them at all. I need a way using batch only (though calling powershell from within batch is an option) to break these into the full path and file name.

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

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

file1.csv

file2.xml

I've seen ways of doing it in many other languages, but I'm limited to batch, and it's not one of my strong languages. I tried using a far /f loop with "delims=/", but it quits when it gets to //.

最满意答案

@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" @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%"

Output:

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

更多推荐