开发者

Windows batch script: substring calculation

I have a situation like this. I have a list of urls in a file.

SET str1="http://www.domain.com/dir1/dir2/dir3/dir4/dir5/file1.txt"

In the above string http://www.domain.com/dir1/dir2/dir3 is constant in all the urls. I need to extract the rest of the path in each url.

I mean, i need to get the final string from the above url is /dir4/dir5/file1开发者_高级运维.txt

Thanks


You need the %var:~start,end% notation. For example, if you run this:

@SET str1="http://www.domain.com/dir1/dir2/dir3/dir4/dir5/file1.txt"
@ECHO %str1:~37,-1%

It will print /dir4/dir5/file1.txt.


Alternatively, you can use the %variable:str_to_delete=% syntax to delete a character string from the variable. This way you don't have to rely on the character positions within the string.

Example code:

@echo off
set str1="http://www.domain.com/dir1/dir2/dir3/dir4/dir5/file1.txt"

:: remove the common part of the path
set str2=%str1:http://www.domain.com/dir1/dir2/dir3=%
:: remove the quotes
set str2=%str2:"=%

echo.%str2%

Output:

/dir4/dir5/file1.txt
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜